admin管理员组文章数量:1399127
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 badges3 Answers
Reset to default 6You 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 "blueberri
es".
本文标签: javascriptDisable stop word filtering in a MongoDB text searchStack Overflow
版权声明:本文标题:javascript - Disable stop word filtering in a MongoDB text search - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744212548a2595488.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论