fix: code cleanup

pull/6038/head
Nicolas Giard 4 years ago committed by GitHub
parent eea6ee229a
commit cf41b97d2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,7 +12,7 @@ module.exports = {
if (WIKI.config.db.type !== 'sqlite') { if (WIKI.config.db.type !== 'sqlite') {
throw new WIKI.Error.SearchActivationFailed('Must use Sqlite3 database to activate this engine!') throw new WIKI.Error.SearchActivationFailed('Must use Sqlite3 database to activate this engine!')
} }
let opts = await WIKI.models.knex.schema.raw('PRAGMA compile_options') const opts = await WIKI.models.knex.schema.raw('PRAGMA compile_options')
if (!_.find(opts, { compile_options: 'ENABLE_FTS5' })) { if (!_.find(opts, { compile_options: 'ENABLE_FTS5' })) {
throw new WIKI.Error.SearchActivationFailed('Sqlite3 must have FTS5 module!') throw new WIKI.Error.SearchActivationFailed('Sqlite3 must have FTS5 module!')
} }
@ -32,7 +32,7 @@ module.exports = {
const indexExists = await WIKI.models.knex.schema.hasTable('fts5_pages_vector') const indexExists = await WIKI.models.knex.schema.hasTable('fts5_pages_vector')
if (!indexExists) { if (!indexExists) {
WIKI.logger.info(`(SEARCH/SQLITE3) Creating Pages Vector table...`) WIKI.logger.info(`(SEARCH/SQLITE3) Creating Pages Vector table...`)
await WIKI.models.knex.raw('create virtual table fts5_pages_vector using fts5(tokenize=unicode61, path, locale, title,description,content)') await WIKI.models.knex.raw('CREATE VIRTUAL TABLE fts5_pages_vector USING fts5(tokenize=unicode61, path, locale, title, description, content)')
} }
WIKI.logger.info(`(SEARCH/SQLITE3) Initialization completed.`) WIKI.logger.info(`(SEARCH/SQLITE3) Initialization completed.`)
}, },
@ -44,26 +44,26 @@ module.exports = {
*/ */
async query(q, opts) { async query(q, opts) {
try { try {
let qry = ` const qry = `
SELECT rowid as id, path, locale, title, description SELECT rowid AS id, path, locale, title, description
FROM "fts5_pages_vector"` FROM "fts5_pages_vector"`
let qryEnd = 'ORDER BY rank' const qryEnd = 'ORDER BY rank'
let qryWhere = 'WHERE fts5_pages_vector MATCH ?' let qryWhere = 'WHERE fts5_pages_vector MATCH ?'
let o = matchquery.parse(q) const o = matchquery.parse(q)
if (o.negated) if (o.negated) {
qryWhere = 'WHERE rowid not in (select rowid from fts5_pages_vector where fts5_pages_vector MATCH ?)' qryWhere = 'WHERE rowid NOT IN (SELECT rowid FROM fts5_pages_vector WHERE fts5_pages_vector MATCH ?)'
let qryParams = [ o.str ] }
const results = await WIKI.models.knex.raw(` const results = await WIKI.models.knex.raw(`
${qry} ${qry}
${qryWhere} ${qryWhere}
${qryEnd} ${qryEnd}
` , qryParams) `, [o.str])
return { return {
results, results,
suggestions: [], suggestions: [],
totalHits: results.length, totalHits: results.length
} }
} catch (err) { } catch (err) {
WIKI.logger.warn('Search Engine Error:') WIKI.logger.warn('Search Engine Error:')
@ -130,8 +130,7 @@ module.exports = {
await WIKI.models.knex.raw(` await WIKI.models.knex.raw(`
INSERT INTO "fts5_pages_vector" (path, locale, title, description, content) INSERT INTO "fts5_pages_vector" (path, locale, title, description, content)
SELECT path, localeCode, title, description, content from pages`) SELECT path, localeCode, title, description, content FROM pages`
)
} }
} }

Loading…
Cancel
Save