Add endpoint for full-text search (#6)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
2020-10-12 21:46:50 -05:00
parent 273460b126
commit e55720e3bb
7 changed files with 100 additions and 7 deletions

View File

@@ -20,6 +20,57 @@ class Misc extends Controller {
return res.api(token.token)
}
async get_search(req, res, next) {
if ( !req.query.query ) {
return res.status(400)
.message('Missing query.')
.api()
}
const query = String(req.query.query)
const Page = this.models.get('api:Page')
const Node = this.models.get('api:Node')
const all_user_page_ids = (await req.user.get_accessible_pages()).map(x => x.UUID)
const results = []
const matching_pages = await Page.find({
UUID: { $in: all_user_page_ids },
Active: true,
$text: { $search: query },
})
for ( const page of matching_pages ) {
results.push({
title: page.Name,
type: 'page',
id: page.UUID,
})
}
const matching_nodes = await Node.find({
PageId: { $in: all_user_page_ids },
'Value.Mode': 'norm',
$text: { $search: query }
})
for ( const node of matching_nodes ) {
const page = await node.page
results.push({
title: node.Value.Value,
type: 'node',
id: node.UUID,
associated: {
title: page.Name,
type: 'page',
id: page.UUID,
}
})
}
return res.api({results})
}
}
module.exports = exports = Misc