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

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

问题描述

好的,所以我对此行为感到非常困惑.似乎不一致且奇怪,尤其是因为我已经阅读到Mongo不应该在全文搜索中支持部分搜索项.我正在使用Mongo DB社区服务器的3.4.7版本.我正在从Mongo shell进行这些测试.

Ok, so 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:

克雷格"

鲍勃博士".

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

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\""}})

结果:返回包含两个术语的文档,尽管一个术语是不完整的.文档中没有与博博士"匹配的内容

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"或"Bo.Dr"匹配的内容.

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允许对字符串内容进行文本搜索查询,并支持不区分大小写,定界符,停用词和词干.默认情况下,对搜索字符串中的字词进行或"运算.

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 搜索.

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

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

要验证此行为,如果您编辑文档,将Dr. Bob更改为DrBob,则以下查询将返回文档:

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天全站免登陆