Best way to perform a full text search in MongoDB and Mongoose

前端 未结 2 2055
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 09:17

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

相关标签:
2条回答
  • 2020-12-12 09:54

    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.

    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.

    0 讨论(0)
  • 2020-12-12 10:02

    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.

    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.

    0 讨论(0)
提交回复
热议问题