Skip to content Skip to sidebar Skip to footer

Disable Stop Word Filtering In A Mongodb Text Search

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

Solution 1:

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]

Solution 2:

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.

Solution 3:

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 become "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".

Post a Comment for "Disable Stop Word Filtering In A Mongodb Text Search"