Full text search with weight in mongoose

前端 未结 4 1555
感动是毒
感动是毒 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: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.

提交回复
热议问题