use*_*059 6 mongodb mongodb-query
好的,所以我对这种行为感到很困惑.它似乎不一致和奇怪,特别是因为我读过Mongo不应该支持全文搜索中的部分搜索术语.我使用的是Mongo DB社区服务器版本3.4.7.我正在从Mongo shell进行这些测试.
所以,我有一个分配了文本索引的Mongo DB集合.我创建了这样的索引:
db.submissions.createIndex({"$**":"text"})
Run Code Online (Sandbox Code Playgroud)
此集合中有一个文档包含以下两个值:
"克雷格"
"鲍勃博士".
我的目标是对包含多个匹配术语的文档进行文本搜索.
所以,这里是我运行的测试,以及它们不一致的输出:
单期,完整
db.submissions.find({"$text":{"$search":"\"Craig\""}})
Run Code Online (Sandbox Code Playgroud)
结果:获取包含此值的文档.
单期,部分
db.submissions.find({"$text":{"$search":"\"Crai\""}})
Run Code Online (Sandbox Code Playgroud)
结果:不返回任何内容,因为此部分搜索词与文档中的任何内容都不完全匹配.
多个条款,完整
db.submissions.find({"$text":{"$search":"\"Craig\" \"Dr. Bob\""}})
Run Code Online (Sandbox Code Playgroud)
结果:返回包含这两个术语的文档.
多个条款,一个部分
db.submissions.find({"$text":{"$search":"\"Craig\" \"Dr. Bo\""}})
Run Code Online (Sandbox Code Playgroud)
结果:尽管一个术语是部分术语,但返回包含两个术语的文档.文件中没有任何内容与"博士博士"相匹配
多个术语,两个部分
db.submissions.find({"$text":{"$search":"\"Crai\" \"Dr. Bo\""}})
Run Code Online (Sandbox Code Playgroud)
结果:返回包含两个术语的文档,尽管这两个术语都是部分和不完整的.文档中没有任何内容与"Crai"或"Bo博士"相匹配.
题
所以,这一切归结为:为什么?为什么会这样,当我使用只有一个值的部分术语进行文本搜索时,不会返回任何内容.当我用两个部分术语进行文本搜索时,我得到匹配结果?它看起来很奇怪而且不一致.
谢谢.
MongoDB $text
搜索不支持部分匹配.MongoDB允许对字符串内容进行文本搜索查询,支持不区分大小写,分隔符,停用词和词干.默认情况下,搜索字符串中的术语是"或".
逐个使用(非常有用的)示例:
单期,部分
// 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\""}})
Run Code Online (Sandbox Code Playgroud)
多个条款,完整
// 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\""}})
Run Code Online (Sandbox Code Playgroud)
多个条款,一个部分
// 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\""}})
Run Code Online (Sandbox Code Playgroud)
多个术语,两个部分
// returns the document because it contains the whole word "Dr"
db.submissions.find({"$text":{"$search":"\"Crai\" \"Dr. Bo\""}})
Run Code Online (Sandbox Code Playgroud)
请记住,$search
字符串是......
MongoDB解析并用于查询文本索引的一串术语.除非指定为短语,否则MongoDB会对这些术语执行逻辑OR搜索.
因此,如果$search
字符串中至少有一个术语匹配,则MongoDB与该文档匹配.
要验证此行为,如果您将文档编辑更改Dr. Bob
为,DrBob
则以下查询将不返回任何文档:
db.submissions.find({"$text":{"$search":"\"Craig\" \"Dr. Bo\""}})
db.submissions.find({"$text":{"$search":"\"Crai\" \"Dr. Bo\""}})
Run Code Online (Sandbox Code Playgroud)
这些现在不返回匹配项,因为Dr
它不再是文本索引中的整个单词,因为它没有后跟.
分隔符.
归档时间: |
|
查看次数: |
2792 次 |
最近记录: |