feat: search + basic engine (wip)

This commit is contained in:
Nick
2019-03-09 00:51:02 -05:00
parent 6eb6f669e6
commit b5db531234
25 changed files with 766 additions and 108 deletions

View File

@@ -1,26 +1,121 @@
const _ = require('lodash')
module.exports = {
activate() {
// not used
},
deactivate() {
// not used
},
query() {
/**
* INIT
*/
init() {
// not used
},
created() {
/**
* SUGGEST
*
* @param {String} q Query
* @param {Object} opts Additional options
*/
async suggest(q, opts) {
const results = await WIKI.models.pages.query()
.column('title')
.where(builder => {
builder.where('isPublished', true)
if (opts.locale) {
builder.andWhere('locale', opts.locale)
}
if (opts.path) {
builder.andWhere('path', 'like', `${opts.path}%`)
}
builder.andWhere('title', 'like', `%${q}%`)
})
.limit(10)
return _.uniq(_.filter(_.flatten(results.map(r => r.title.split(' '))), w => w.indexOf(q) >= 0))
},
updated() {
/**
* QUERY
*
* @param {String} q Query
* @param {Object} opts Additional options
*/
async query(q, opts) {
const results = await WIKI.models.pages.query()
.column('id', 'title', 'description', 'path', 'localeCode as locale')
.where(builder => {
builder.where('isPublished', true)
if (opts.locale) {
builder.andWhere('localeCode', opts.locale)
}
if (opts.path) {
builder.andWhere('path', 'like', `${opts.path}%`)
}
// TODO: Add user permissions filtering
builder.andWhere(builder => {
switch(WIKI.config.db.type) {
case 'postgres':
builder.where('title', 'ILIKE', `%${q}%`)
builder.orWhere('description', 'ILIKE', `%${q}%`)
break
case 'mysql':
case 'mariadb':
builder.whereRaw(`title LIKE '%?%' COLLATE utf8_general_ci`, [q])
builder.orWhereRaw(`description LIKE '%?%' COLLATE utf8_general_ci`, [q])
break
// TODO: MSSQL handling
default:
builder.where('title', 'LIKE', `%${q}%`)
builder.orWhere('description', 'LIKE', `%${q}%`)
break
}
})
})
.limit(WIKI.config.search.maxHits)
return {
results,
suggestions: [],
totalHits: results.length
}
},
deleted() {
/**
* CREATE
*
* @param {Object} page Page to create
*/
async created(page) {
// not used
},
renamed() {
/**
* UPDATE
*
* @param {Object} page Page to update
*/
async updated(page) {
// not used
},
rebuild() {
/**
* DELETE
*
* @param {Object} page Page to delete
*/
async deleted(page) {
// not used
},
/**
* RENAME
*
* @param {Object} page Page to rename
*/
async renamed(page) {
// not used
},
/**
* REBUILD INDEX
*/
async rebuild() {
// not used
}
}