在MongoDB和Mongoose中执行全文搜索的最佳方式

时间:2022-12-02 02:34:46

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.

我在谷歌上搜索了好几天,我尝试了很多东西,但是我仍然不能在我的用户集合上执行一个好的全文搜索。

I tried ElasticSearch but was pretty impossible to query and paginate...

我尝试了弹框搜索,但几乎不可能查询和分页……

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.

我尝试了许多像ElMongo, Mongoose - all -text, Mongoosastic等的Mongoose插件。每个人的文档都很糟糕,我不知道如何执行一个好的全文搜索。

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 world what I need is to search on the entire collection fields the matching words of my search query and get the results.

我有一个搜索输入,在一个页面中有一个简单的帖子,如果我输入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.

谢谢。

2 个解决方案

#1


80  

You can add a text index to your Mongoose schema definition that lets you use the $text operator in your find queries to search all fields included in the text index.

您可以向您的Mongoose模式定义添加一个文本索引,该定义允许您在查找查询中使用$text操作符来搜索包含在文本索引中的所有字段。

To create an index to support text search on, say, name and 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 Indexes documentation.

有关详细信息,请阅读完整的MongoDB文本索引文档。

#2


-9  

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中,您可以创建自己的自定义搜索查询,以搜索任何特定的文本和字符串,并在指定的时间内进行实时搜索。

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服务器连接起来。然后使用弹性搜索的河流功能,你必须在你的数据的弹性搜索服务器上做索引,然后只有你可以让你的定制查询进行搜索。

#1


80  

You can add a text index to your Mongoose schema definition that lets you use the $text operator in your find queries to search all fields included in the text index.

您可以向您的Mongoose模式定义添加一个文本索引,该定义允许您在查找查询中使用$text操作符来搜索包含在文本索引中的所有字段。

To create an index to support text search on, say, name and 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 Indexes documentation.

有关详细信息,请阅读完整的MongoDB文本索引文档。

#2


-9  

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中,您可以创建自己的自定义搜索查询,以搜索任何特定的文本和字符串,并在指定的时间内进行实时搜索。

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服务器连接起来。然后使用弹性搜索的河流功能,你必须在你的数据的弹性搜索服务器上做索引,然后只有你可以让你的定制查询进行搜索。