javascript - Disable stop word filtering in a MongoDB text search - Stack Overflow

I am wondering if it would be possible to disable only the stop word filtering in the MongoDB text sear

I am wondering if it would be possible to disable only the stop word filtering in the MongoDB text search. Sometimes I just want to search for words like "you", "I", "was", etc. I would still like to take advantage of the stemming. Just not the stop word filtering.

db.collection.find({$text: {$search: "you"}})

The above would not return any results.

But a traditional approach like

db.collection.find({shortDescription: new RegExp(".*you.*",'i')}) would give me what I want.

So, how can I have the text search but also be able to search these words (stop words).

I am wondering if it would be possible to disable only the stop word filtering in the MongoDB text search. Sometimes I just want to search for words like "you", "I", "was", etc. I would still like to take advantage of the stemming. Just not the stop word filtering.

db.collection.find({$text: {$search: "you"}})

The above would not return any results.

But a traditional approach like

db.collection.find({shortDescription: new RegExp(".*you.*",'i')}) would give me what I want.

So, how can I have the text search but also be able to search these words (stop words).

Share Improve this question edited Feb 28, 2021 at 8:26 chrizonline 4,97917 gold badges67 silver badges106 bronze badges asked Mar 8, 2018 at 11:10 Vignesh PTVignesh PT 64412 silver badges28 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You can disable stop words by changing the language value of your text index when you create it. From the MongoDB documentation:

If you specify a language value of "none", then the text search uses simple tokenization with no list of stop words and no stemming [source].

So create your index using:

db.collection.createIndex(
   { content : "text" },
   { default_language: "none" }
)

[code source]

when you are inserting any text for text-indexed field. The index values are created after filtering the text. So when you are searching for any stop words it's not present in the index value list. That's the reason its never going to search out the stop words. It's by design and probably non-editable.You have to use Regex for such criteria. I hope there is no other way available.

Since you want the stemming, I assume there will never be just stop words, but always at least one "normal" word too. On top of that, I hope you know exactly which stop words you want.

If that's the case, I suggest to put the stop words into quotes. As the docs say, if there are phrases present "the search performs a logical AND of the phrase with the individual terms in the search string." And thankfully, it appears that stop words are not stripped from phrases.

For example, assume a collection with the following documents:

{"text": "I love blueberries"},
{"text": "She loves blueberries"},
{"text": "She loved the last blueberry most."}

Searching for blueberry, blueberry I or blueberries she each time returns all three collections. But searching for blueberries "she" only returns the last two collections, i.e. stemming is considered and the existence of the stop word enforced.


Sadly, this won't work if you're searching for just stop words, i.e. searching for "she" will return nothing. Also, you can't OR several stop words: If you add "and me" to each of the first two documents so that they bee "I love blueberries and me" and "She loves blueberries and me" respectively, searching for blueberry "she" "me" will only return the second document.

However, beware of extremely short stop words that may be part of other words: On my test database, searching for blueberry "I" returned both the first and second documents -- I assume due to the i in "blueberries".

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744212548a4563403.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信