Javascript 在 MongoDB 和 Mongoose 中执行全文搜索的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28775051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to perform a full text search in MongoDB and Mongoose
提问by Ayeye Brazo
I'm searching on Google since days and I tried many things but I still can not perform a good full text search on my user collection.
几天以来,我一直在 Google 上进行搜索,并且尝试了很多方法,但仍然无法对我的用户集合进行良好的全文搜索。
I tried ElasticSearch but was pretty impossible to query and paginate...
我尝试了 ElasticSearch,但几乎不可能查询和分页...
I tried many plugins for Mongoose like ElMongo, mongoose-full-text, Mongoosastic, etc... everyone are really bad documented and I don't know how to perform a good full text search.
我为 Mongoose 尝试了许多插件,如 ElMongo、mongoose-full-text、Mongoosastic 等……每个人的文档都非常糟糕,我不知道如何进行良好的全文搜索。
So, my collection is a normal collection:
所以,我的收藏是一个普通的收藏:
user = {
name: String,
email: String,
profile: {
something: String,
somethingElse: String
}
}
I have a search input in a page with a simple POST, if I type hello worldwhat I need is to search on the entire collection fields the matching words of my search query and get the results.
我在一个简单的页面中有一个搜索输入POST,如果我输入hello world我需要的是在整个集合字段上搜索我的搜索查询的匹配词并获得结果。
It will be really nice also to have options to handle a pagination like 10 items per page or something...
也可以选择处理分页,例如每页 10 个项目或其他内容……
What is the best solution to achieve this? I'm using MongoDB 2.6.* with Mongoose, NodeJS and ExpressJS.
实现这一目标的最佳解决方案是什么?我将 MongoDB 2.6.* 与 Mongoose、NodeJS 和 ExpressJS 一起使用。
Thanks.
谢谢。
回答by JohnnyHK
You can add a text indexto your Mongoose schema definition that lets you use the $textoperator in your findqueries to search all fields included in the text index.
您可以将文本索引添加到您的 Mongoose 架构定义中,以便您$text在find查询中使用运算符来搜索文本索引中包含的所有字段。
To create an index to support text search on, say, nameand profile.something:
要创建索引以支持文本搜索,例如,name和profile.something:
var schema = new Schema({
name: String,
email: String,
profile: {
something: String,
somethingElse: String
}
});
schema.index({name: 'text', 'profile.something': 'text'});
Or if you want to include all string fields in the index, use the '$**'wildcard:
或者,如果您想在索引中包含所有字符串字段,请使用'$**'通配符:
schema.index({'$**': 'text'});
This would enable you to performed a paged text search query like:
这将使您能够执行分页文本搜索查询,例如:
MyModel.find({$text: {$search: searchString}})
.skip(20)
.limit(10)
.exec(function(err, docs) { ... });
For more details, read the full MongoDB Text Indexesdocumentation.
有关更多详细信息,请阅读完整的MongoDB 文本索引文档。
回答by shailendra pathak
For search query you can use Elasticsearch plugin. In Elasticsearch you can make your own custom search query for searching any particular text and string with the specified time duration in the real time.
对于搜索查询,您可以使用 Elasticsearch 插件。在 Elasticsearch 中,您可以创建自己的自定义搜索查询,以实时搜索具有指定持续时间的任何特定文本和字符串。
But before that you have to make indexing of your json docs residing in MongoDB. You have to connect MongoDB with Elasticsearch server.then using the river functionality of Elasticsearch you have to make the indexing in Elasticsearch server of your data, then only you can make your custom query for searching.
但在此之前,您必须对驻留在 MongoDB 中的 json 文档进行索引。您必须将 MongoDB 与 Elasticsearch 服务器连接。然后使用 Elasticsearch 的河流功能,您必须在 Elasticsearch 服务器中对数据进行索引,然后才能进行自定义查询以进行搜索。

