I am using Mongoose to provide a search facility within a web app.
I am scraping some text elsewhere, and placing it in a SearchDoc collection. I am then wishing to do a fulltext search on these documents.
Suppose the user is looking for 'biomarkers' in the text.
The issue is that partial queries don't seem to return results. For example:
- User searches for 'bio' : no results returned
- User searches for 'biomar' : no results returned
- User searches for 'biomarker' : results are found
I would expect all the above to return results for 'biomarker' in the text.
models/search-doc.js:
let mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.Promise = require('bluebird');
let SearchDoc = new Schema({
title : {type : String},
description : {type : String},
tags : {type : Array},
body : {type : String},
thumb : { data: Buffer, contentType: String },
docType : {type : String},
docId : {type : String}
}, {strict : true});
let fields = { title: 'text', description: 'text', tags: 'text', body: 'text' };
let opts = {
name : 'textIndex',
weights : {title: 10, description: 5, tags : 3, body : 1},
default_language: 'en'
};
SearchDoc.index(fields, opts);
module.exports = mongoose.model('SearchDoc', SearchDoc);
And here is the query:
SearchDoc
.find({
$text: {
$search: q
}
}, {
score: {
$meta: 'textScore'
}
})
.sort({
score: {
$meta: 'textScore'
}
})
//.lean()
.exec((err, data) => {
if (err) {
return reject(err.toString());
}
// Convert data
let highlightedResults = this.highlightResults(q, data);
resolve(highlightedResults);
});
via bristol.james
No comments:
Post a Comment