我正在尝试使用 Mongoose 和 nodejs 设置一个简单的关键字全文搜索,这是到目前为止的代码:
var db = mongoose.createConnection('localhost', config.mongoDbName)
, ContSchema = Schema({
'idUser': String,
'title': String,
'category': String,
'slug': String,
'description': String
})
.index({
'title':'text',
'description':'text'
})
, ContModel = db.model('Cont', ContSchema);
db.on('error', function (err) {
console.error.bind(console, 'Mongoose connection error:' + err);
});
db.once('open', function () {
process.stdout.write('Connection to mongodb db done...');
});
然后我做搜索查询:
var getAllBy = function (limit, offset, keywords) {
var sort
, find
, findScore;
if (!!keywords) {
find = {'$text':{'$search':keywords}};
findScore = {'score':{'$meta':'textMatchScore'}};
sort = {'score':{'$meta':'textMatchScore'}};
} else {
sort = {'_id':'desc'};
}
return new RSVP.Promise(function (resolve, reject) {
ContModel.find(find, findScore).limit(limit).skip(offset).sort(sort).exec(function (err, data) {
if (err){
reject(err);
} else {
resolve(data);
}
});
});
}
它不起作用,它在控制台中返回此错误:MongoError: Can't canonicalize query: BadValue bad sort specification
我实际上无法找到真正的问题所在,你能帮帮我吗?
谢谢
最佳答案
您的关键字在这里不正确,请参阅 $meta
下的正确语法运算符,目前只有“textScore”:
find = {'$text':{'$search':keywords}};
findScore = {'score':{'$meta':'textScore'}};
sort = {'score': {'$meta':'textScore'} }
关于javascript - Mongoose ,nodejs,全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25989988/