$text 搜索可以执行部分​​匹配吗 [英] Can a $text search perform a partial match

查看:21
本文介绍了$text 搜索可以执行部分​​匹配吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这种行为感到非常困惑.这似乎不一致和奇怪,特别是因为我读过 Mongo 不应该支持全文搜索中的部分搜索词.我正在使用 Mongo DB 社区服务器的 3.4.7 版.我正在从 Mongo shell 进行这些测试.

I'm very confused by this behavior. It seems inconsistent and strange, especially since I've read that Mongo isn't supposed to support partial search terms in full text search. I'm using version 3.4.7 of Mongo DB Community Server. I'm doing these tests from the Mongo shell.

所以,我有一个分配了文本索引的 Mongo DB 集合.我创建了这样的索引:

So, I have a Mongo DB collection with a text index assigned. I created the index like this:

db.submissions.createIndex({"$**":"text"})

这个集合中有一个文档包含这两个值:

There is a document in this collection that contains these two values:

克雷格"

"博士鲍勃".

"Dr. Bob".

我的目标是对包含多个匹配项的文档进行文本搜索.

My goal is to do a text search for a document that has multiple matching terms in it.

所以,这里是我运行的测试,以及它们不一致的输出:

So, here are tests I've run, and their inconsistent output:

单学期,完成

db.submissions.find({"$text":{"$search":""Craig""}})

结果:获取包含此值的文档.

Result: Gets me the document with this value in it.

单项,部分

db.submissions.find({"$text":{"$search":""Crai""}})

结果:不返回任何内容,因为此部分搜索词与文档中的任何内容都不完全匹配.

Result: Returns nothing, because this partial search term doesn't exactly match anything in the document.

多个术语,完整

db.submissions.find({"$text":{"$search":""Craig" "Dr. Bob""}})

结果:返回包含这两个术语的文档.

Result: Returns the document with both of these terms in it.

多个条款,一个部分

db.submissions.find({"$text":{"$search":""Craig" "Dr. Bo""}})

结果:返回包含两个词条的文档,尽管其中一个词条是不完整的.文档中没有任何内容与Dr.博"

Result: Returns the document with both terms in it, despite the fact that one term is partial. There is nothing in the document that matches "Dr. Bo"

多个条款,均部分

db.submissions.find({"$text":{"$search":""Crai" "Dr. Bo""}})

结果:返回包含两个词条的文档,尽管这两个词条都是不完整的和不完整的.文档中没有任何内容与Crai"或Crai"匹配.或博士.博".

Result: Returns the document with both terms in it, despite the fact that both terms are partial and incomplete. There is nothing in the document that matches either "Crai" or "Dr. Bo".

问题

所以,这一切都归结为:为什么?为什么是这样,当我使用只有一个值的部分术语进行文本搜索时,没有返回任何内容.当我使用两个部分术语进行文本搜索时,我得到了匹配的结果吗?只是看起来如此奇怪和不一致.

So, it all boils down to: why? Why is it, when I do a text search with a partial term with only a single value, nothing gets returned. When I do a text search with two partial terms, I get the matching result? It just seems so strange and inconsistent.

推荐答案

MongoDB $text 搜索不支持部分匹配.MongoDB 允许对字符串内容进行文本搜索查询,并支持不区分大小写、分隔符、停用词和词干提取.默认情况下,搜索字符串中的字词会进行 OR 运算.

MongoDB $text searches do not support partial matching. MongoDB allows text search queries on string content with support for case insensitivity, delimiters, stop words and stemming. And the terms in your search string are, by default, OR'ed.

一一举出(非常有用的:) 示例:

Taking your (very useful :) examples one by one:

单项,部分

// returns nothing because there is no world word with the value `Crai` in your
// text index and there is no whole word for which `Crai` is a recognised stem
db.submissions.find({"$text":{"$search":""Crai""}})

多个术语,完整

// returns the document because it contains all of these words
// note in the text index Dr. Bob is not a single entry since "." is a delimiter
db.submissions.find({"$text":{"$search":""Craig" "Dr. Bob""}})

多个条款,一个部分

// returns the document because it contains the whole word "Craig" and it 
// contains the whole word "Dr" 
db.submissions.find({"$text":{"$search":""Craig" "Dr. Bo""}})

多个条款,均部分

// returns the document because it contains the whole word "Dr"
db.submissions.find({"$text":{"$search":""Crai" "Dr. Bo""}})

请记住,$search 字符串是 ...

Bear in mind that the $search string is ...

MongoDB 解析并用于查询文本索引的一串术语.除非指定为短语,否则 MongoDB 会对术语执行逻辑 OR 搜索.

A string of terms that MongoDB parses and uses to query the text index. MongoDB performs a logical OR search of the terms unless specified as a phrase.

因此,如果您的 $search 字符串中至少有一个术语匹配,那么 MongoDB 会匹配该文档.

So, if at least one term in your $search string matches then MongoDB matches that document.

要验证此行为,如果您编辑文档更改 Dr.BobDrBob 然后以下查询将返回no 文档:

To verify this behaviour, if you edit your document changing Dr. Bob to DrBob then the following queries will return no documents:

db.submissions.find({"$text":{"$search":""Craig" "Dr. Bo""}})
db.submissions.find({"$text":{"$search":""Crai" "Dr. Bo""}})

这些现在不返回匹配项,因为 Dr 不再是文本索引中的整个单词,因为它后面没有 . 分隔符.

These now return no matches because Dr is no longer a whole word in your text index because it is not followed by the . delimiter.

这篇关于$text 搜索可以执行部分​​匹配吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆