Aye*_*azo 68 javascript mongoose mongodb node.js express
我在Google上搜索了几天,我尝试了很多东西,但我仍然无法在我的用户集上进行良好的全文搜索.
我试过ElasticSearch但是查询和分页几乎是不可能的......
我为Mongoose尝试了许多插件,如ElMongo,mongoose-full-text,Mongoosastic等等...每个人都记录不好,我不知道如何进行良好的全文搜索.
所以,我的收藏是一个普通的收藏:
user = {
name: String,
email: String,
profile: {
something: String,
somethingElse: String
}
}
Run Code Online (Sandbox Code Playgroud)
我在页面中有一个简单的搜索输入POST,如果我输入hello world我需要的是在整个集合字段中搜索我的搜索查询的匹配单词并获得结果.
如果有选项可以处理每页10个项目的分页,那将是非常好的...
实现这一目标的最佳解决方案是什么?我正在使用MongoDB 2.6.*与Mongoose,NodeJS和ExpressJS.
谢谢.
Joh*_*yHK 142
您可以向Mongoose架构定义添加文本索引,以便$text在find查询中使用运算符来搜索文本索引中包含的所有字段.
要创建索引以支持文本搜索,比如说name和profile.something:
var schema = new Schema({
name: String,
email: String,
profile: {
something: String,
somethingElse: String
}
});
schema.index({name: 'text', 'profile.something': 'text'});
Run Code Online (Sandbox Code Playgroud)
或者,如果要在索引中包含所有字符串字段,请使用'$**'通配符:
schema.index({'$**': 'text'});
Run Code Online (Sandbox Code Playgroud)
这将使您能够执行分页文本搜索查询,如:
MyModel.find({$text: {$search: searchString}})
.skip(20)
.limit(10)
.exec(function(err, docs) { ... });
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请阅读完整的MongoDB文本索引文档.
| 归档时间: |
|
| 查看次数: |
34526 次 |
| 最近记录: |