Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const contentSchema = new mongoose.Schema({
title: { type: String, required: true },
body: { type: String, required: true },
url: { type: String, required: true },
version: { type: String }
version: { type: String },
versionNumber: { type: Number }
});

module.exports = async function search(context, req) {
Expand All @@ -22,27 +23,24 @@ module.exports = async function search(context, req) {
Content = conn.model('Content', contentSchema, 'Content');

const query = req.query.search.toString();
const version = req.query.version;
const version = req.query.version ? +req.query.version.replace(/\.x$/, '') : null;
let results = await Content.aggregate([
{
$search: {
index: 'mongoose-content',
text: {
query,
path: { wildcard: '*' },
fuzzy: {}
compound: {
must: [
...(version ? [{
equals: {
path: 'versionNumber',
value: version
}
}] : []),
{ text: { query, path: { wildcard: '*' }, fuzzy: {} } }
]
}
}
},
{ $match: { version } },
{
$addFields: {
score: {
$meta: 'searchScore'
}
}
},
{ $sort: { score: -1 } },
{ $limit: 10 }
]);

Expand Down