Full text search with weight in mongoose

前端 未结 4 1542
感动是毒
感动是毒 2020-12-07 10:35

As I find out, since version 3.8.9, mongoose support full text search. But I can\'t find a good documentation for it!
I want to do something like:

db.col         


        
相关标签:
4条回答
  • 2020-12-07 11:04
        var searchQuery=new RegExp('dam', 'i');
        var query = { firstName : searchQuery };
        Model.find(query ...
    
    0 讨论(0)
  • 2020-12-07 11:05

    I found the following article that led me to the http://code.tutsplus.com/tutorials/full-text-search-in-mongodb--cms-24835 I dropped the index created in the top answer using the following

    db.tablename.dropIndex({"indexname_text"})  
    

    I got the list of indexes with this command

    db.tablename.getIndexes()
    

    I then used the following to create the indexes

    db.tablename.createIndex({"$**":"text"})
    

    the following commands worked in Mongoose

    model.find(
        {$text: {$search: "text you are searching for"}},
        {score: {$meta: "textScore"}})
        .sort({score:{$meta:"textScore"}}
    )
    .exec(function(err, results) {
        `enter code here`if(!err){
        console.log('results ' + results);
    }
    else
    {
        console.log(err);
    }
    });
    
    0 讨论(0)
  • 2020-12-07 11:14

    As of MongoDB 2.6, a collection can have at most one text index (documented here). Therefore, you will not be able to do what you want with the current version of MongoDB. Really, for complicated text searching problems with requirements of different weights depending on the location of the match, you should consider a full-blown text searching solution like Solr or ElasticSearch.

    As a workaround in MongoDB, you could tokenize the fields manually, store them as keyword arrays, and index them:

    animal: ["The", "quick", "brown", "fox", "jump", ..., "dog"]
    

    then a query like

    db.test.find({animal: {$in: ["brown", "shoes"]})

    mimics text search. There are a few limitations of this approach like the manual work required to set it up, the fact that there will be no stemming to, e.g., match "dreaming" with "dream", the fact that stopwords will not be removed like in a normal text index, and the absence of any weighting mechanism.

    0 讨论(0)
  • 2020-12-07 11:17

    Yes, you can use full text search in Mongoose >= 3.8.9. Firstly, a collection can have at most one text index (see docs). So, to define text index for several fields, you need compound index:

    schema.index({ animal: 'text', color: 'text', pattern: 'text', size: 'text' });
    

    Now you can use $text query operator like this:

    Model
        .find(
            { $text : { $search : "text to look for" } }, 
            { score : { $meta: "textScore" } }
        )
        .sort({ score : { $meta : 'textScore' } })
        .exec(function(err, results) {
            // callback
        });
    

    This will also sort results by relevance score.

    As for weights, you can try to pass weights options object to index() method (where you define compound index) (working at least with v4.0.1 of mongoose):

    schema.index({ animal: 'text', color: 'text', pattern: 'text', size: 'text' }, {name: 'My text index', weights: {animal: 10, color: 4, pattern: 2, size: 1}});
    
    0 讨论(0)
提交回复
热议问题