MongoDB fulltext search + workaround for partial word match

前端 未结 5 998
予麋鹿
予麋鹿 2021-02-05 03:51

Since it is not possible to find \"blueberry\" by the word \"blue\" by using a mongodb full text search, I want to help my users to complete the word \"blue\" to \"blueberry\".

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 04:40

    Don't know if this might be useful to some new people facing this problem.

    Depending on the size of your collection and how much RAM you have available, you can make a search by $regex, by creating the proper index. E.g:

    db.collection.find( {query : {$regex: /querywords/}}).sort({'criteria': -1}).limit(limit)
    

    You would need an index as follows:

    db.collection.ensureIndex( { "query": 1, "criteria" : -1 } )
    

    This could be really fast if you have enough memory.

    Hope this helps.

提交回复
热议问题