Add endpoint for full-text search (#6)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user