Text search query for text “other” always returns no results?

允我心安 提交于 2019-12-20 04:15:04

问题


My data looks like this:

[{"id" : 1, "question" : "Other specified dermatomycoses", ... },
 {"id" : 6, "question" : "Other specified disorders of joint, site unspecified", ... }]

plus a few other records.

If I run

db.questions.find({$text:{$search:'other'}}).count()

I always get 0. But if I run

db.questions.find({$text:{$search:'specified'}}).count()

I get the 2 that I expect. Most searches work properly, but not the word "other". Any ideas?


回答1:


This is a commonplace occurance in "text search" operations on many engines, where "stop words" are always omitted from the words that are tokenized and therefore searchable.

Common words are "the", "and", "then" etc. But the full listings can be viewed in the source tree. stop_words_[language].txt.

English list here

If your intent is to match words such as listed there, then use a $regex search instead:

db.questions.find({ "question": { "$regex": "other" } })

This is not really a MongoDB thing, but it happens with most text search engines, and is "by design".




回答2:


Blakes said it all, as an added tip; you can use $language operator with value none to ignore stop words and stemming. Here is an example how to use it :

db.questions.find({ $text: { $search: "other", $language: "none" } })



回答3:


When creating a text index in MongoDB, if you do not specify a language value it will use english by default and its stop words. If you want to be able to search by the stop words you will have to set the default language value of your text index to "none".

Create your index like this:

db.questions.createIndex({ theSearchField : 'text' }, { default_language: 'none' })

Then you should be able to run your query

db.questions.find({$text:{$search:'other'}}).count()


来源:https://stackoverflow.com/questions/35122049/text-search-query-for-text-other-always-returns-no-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!