MongoDB - Logical OR when searching for words and

2020-06-21 07:21发布

I asked a related question previously, and as suggested by the poster there have created this new question as a follow up:

MongoDB full text search - matching words and exact phrases

I was having some problems with unexpected results when using the full text search functionality in MongoDB, specifically when searching for a mixture of words and phrases.

Using this helpful example provided by the poster in the previous question...

> db.test.drop()
> db.test.insert({ "t" : "I'm on time, not late or delayed" })
> db.test.insert({ "t" : "I'm either late or delayed" })
> db.test.insert({ "t" : "Time flies like a banana" })
> db.test.ensureIndex({ "t" : "text" })

> db.test.find({ "$text" : { "$search" : "time late delay" } }, { "_id" : 0 })
{ "t" : "I'm on time, not late or delayed" }
{ "t" : "Time flies like a banana" }
{ "t" : "I'm either late or delayed" }

> db.test.find({ "$text" : { "$search" : "late delay" } }, { "_id" : 0 })
{ "t" : "I'm on time, not late or delayed" }
{ "t" : "I'm either late or delayed" }

> db.test.find({ "$text" : { "$search" : "late delay \"on time\"" } }, { "_id" : 0 })
{ "t" : "I'm on time, not late or delayed" }

The first two queries behave as I would expect, the first searching for "time OR late OR delay" and the second for "late OR delay".

I now understand from reading this section of the documentation http://docs.mongodb.org/manual/reference/operator/query/text/#phrases that the third query, which includes a phrase will search for "late OR delay AND ("on time")".

My question is, is it possible to search for "late OR delay OR ("on time")" in one text query?

1条回答
▲ chillily
2楼-- · 2020-06-21 08:05

I combed the docs on text search, and I'm afraid I don't think this is possible as of MongoDB 2.6. MongoDB's text search support is simply not as complete as a bona fide full text search engine (e.g. Solr/things built with the Lucene text search library). Right now, there's no support for boolean operators in text queries, so you cannot change the meaning of "late delay \"on time\"" from "(late OR delay) AND (\"on time\")" to "late OR delay OR \"on time\"". There might be some workarounds involving storing an array of tokens instead of or in addition to the text, or synchronizing with a full text search engine like ElasticSearch, but I'd rather know a bit more about the use case for the query before recommending any solutions.

查看更多
登录 后发表回答