Mongodb 文本搜索精确词组

Mongodb text search exact phrase

我有一个包含以下文档的集合:

{
    "_id" : ObjectId("5ad609a2ac1a8b644180936a"),
    "content" : "Coffee and cakes..."
},
{
    "_id" : ObjectId("5ad609baac1a8b644180936b"),
    "content" : "coffee shop..."
}

文本搜索查询的结果:

find({ $text: { $search: "\"coffee shop\" cakes" } })

returns 只是第二个文档,但我期待两个文档。问题是什么?

尝试使用正则表达式

db.collectionName.find( { 内容: { $regex: /^coffee/i} });


/i 将忽略大小写


求助link:https://docs.mongodb.com/manual/reference/operator/query/regex/#examples

这个...

find({ $text: { $search: "coffee shop cakes" } })

... 将搜索具有 content 属性且包含“咖啡”、“商店”或“蛋糕”中任何一项的任何文档

但是这...

find({ $text: { $search: "\"coffee shop\" cakes" } })

... 将搜索具有 content 属性且包含短语“coffee shop”的任何文档。

我认为当您提交一个词组(“咖啡店”)一个额外的搜索值(“蛋糕”)时,您会期待上述两种行为。但是,这不是 MongoDB 处理短语和附加术语组合的方式。

来自 the docs:

If the $search string includes a phrase and individual terms, text search will only match the documents that include the phrase.

基于 these docs,查询 "\"coffee shop\" cakes" 将被评估为:

"coffee shop" AND ("cakes" or "coffee" or "shop")

这只正确匹配第二个文档。

注意:text index docs 与此相矛盾,根据那些文档,查询 "\"coffee shop\" cakes" 将被评估为:"coffee shop" OR "cakes" 但您观察到的行为与 $text 上面引用的操作员文档。

感谢@RahulRaj raising this issue with MongoDB,他们的回复确认文档不正确:

As you correctly note, there is an inconsistency in the documentation between these two pages. We're tracking this fix to the documentation in DOCS-10382.

https://docs.mongodb.com/manual/reference/operator/query/text/#phrases correctly describes the current implementation of this feature.