Include code snippets in 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:
Garrett Mills 2020-10-13 09:24:28 -05:00
parent 34586b3b90
commit 604753b3ff
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
4 changed files with 31 additions and 3 deletions

View File

@ -43,8 +43,10 @@ class IonicUnit extends Unit {
const databases = this.scaffold.collection('api_db_Database')
await databases.createIndex({ Name: 'text' })
}
const codiums = this.scaffold.collection('api_Codium')
await codiums.createIndex({ code: 'text' })
}
}
module.exports = exports = IonicUnit

View File

@ -66,7 +66,7 @@ class FormCode extends Controller {
if ( !code ) return res.status(404).message('Unable to find code with that ID.').api({})
code.code = req.body.code
code.language = req.body.language
code.Language = req.body.Language
code.NodeId = node.UUID
code.PageId = page.UUID
await code.save()

View File

@ -32,6 +32,7 @@ class Misc extends Controller {
const Page = this.models.get('api:Page')
const Node = this.models.get('api:Node')
const Codium = this.models.get('api:Codium')
const all_user_page_ids = (await req.user.get_accessible_pages()).map(x => x.UUID)
const results = []
@ -73,6 +74,27 @@ class Misc extends Controller {
})
}
const matching_codiums = await Codium.find({
PageId: { $in: all_user_page_ids },
$text: { $search: query },
});
for ( const codium of matching_codiums ) {
const page = await codium.page
results.push({
title: codium.code,
short_title: `${codium.code.slice(0, snip_length)}${codium.code.length > snip_length ? '...' : ''}`,
type: 'code',
id: codium.NodeId,
associated: {
title: page.Name,
short_title: `${page.Name.slice(0, snip_length)}${page.Name.length > snip_length ? '...' : ''}`,
type: 'page',
id: page.UUID,
}
})
}
return res.api({results})
}
}

View File

@ -10,7 +10,7 @@ class Codium extends Model {
static get schema() {
// Return a flitter-orm schema here.
return {
language: {type: String, default: 'javascript'},
Language: {type: String, default: 'javascript'},
NodeId: String,
PageId: String,
code: String,
@ -19,6 +19,10 @@ class Codium extends Model {
}
// Static and instance methods can go here
get page() {
const Page = require('./Page.model')
return this.belongs_to_one(Page, 'PageId', 'UUID')
}
}
module.exports = exports = Codium