Text search query for text “other” always returns

2020-04-18 07:38发布

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?

3条回答
成全新的幸福
2楼-- · 2020-04-18 07:54

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" } })
查看更多
▲ chillily
3楼-- · 2020-04-18 08:06

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

查看更多
时光不老,我们不散
4楼-- · 2020-04-18 08:09

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()
查看更多
登录 后发表回答