我在 mongo db 中创建了一个集合,如下所示
db.articles.insert([
{ _id: 1, subject: "one", author: "abc", views: 50 },
{ _id: 2, subject: "lastone", author: "abc", views: 5 },
{ _id: 3, subject: "firstone", author: "abc", views: 90 },
{ _id: 4, subject: "everyone", author: "abc", views: 100 },
{ _id: 5, subject: "allone", author: "efg", views: 100 },
{ _id: 6, subject: "noone", author: "efg", views: 100 },
{ _id: 7, subject: "nothing", author: "abc", views: 100 }])
之后,我为字段主题和作者提供了文本索引。
db.articles.createIndex(
{subject: "text",
author: "text"})
现在我正在尝试在索引字段中搜索带有“one”的单词。当我执行查询时...
db.articles.count({$text: {$search: "\"one\""}})
...结果是 1
。
问题是,当我想要单词“one”、“abc”的组合时......
db.articles.count({$text: {$search: "\"one\" \"abc\""}}
... 它给出的结果为 4
。包括包含主题名称为“lastone”、“firstone”、“everyone”、“one”的记录作为结果。
所以我的问题是,为什么第一个查询没有获取 4 条记录?我如何编写一个查询来获取 4 条包含单词“one”的记录?
最佳答案
这个命令...
db.articles.count({$text: {$search: "\"one\""}})
... 将对具有确切短语"one"
的文档进行计数。只有一个这样的文档,因此结果是 1
。
用值“one”查询应该只返回文档,因为只有一个文档包含“one”或某个以“one”为词干的值。来自 the docs :
For case insensitive and diacritic insensitive text searches, the
$text
operator matches on the complete stemmed word. So if a document field contains the word blueberry, a search on the term blue will not match. However, blueberry or blueberries will match.
查看您问题中的文档...
one
不是everyone
的词干
one
不是lastone
的词干
one
不是allone
的词干
one
不是firstone
的词干
one
不是noone
的词干
... 所以这些文档都不会与值 one
匹配。
当然,您可以使用多个值进行查询。例如:
docs建议将其计算为
one 或 abc
并正确返回 5:db.articles.count({$text: {$search: "one abc"}})
docs建议将其计算为
"abc"AND ("abc"or "one")
并正确返回 5:db.articles.count({$text: {$search: "\"abc\" one"}})
docs建议这应该被评估为
"one"AND ("one"or "abc")
但它以某种方式返回 4:db.articles.count({$text: {$search: "\"one\" abc"}})
在最后一个示例中,MongoDB 包含主题为“one”、“lastone”、“firstone”、“everyone”的文档,但排除主题为“nothing”的文档。这表明它以某种方式将“one”视为“lastone”、“firstone”和“everyone”的词干,但是在执行 count({$text: {$search: "one"}})
它返回 1
,这清楚地表明 one
不被视为“lastone”、“firstone”和“everyone”的词干。
我怀疑这可能是一个错误,可能值得 raising with MongoDB .
FWIW,您真正想要的可能是部分字符串搜索,在这种情况下 $regex
可能有效。以下查询 ...
db.articles.count({ subject: { $regex: /one$/ }, author: { $regex: /abc$/ } })
... 意思是 count where subject like '%one%' and author like '%abc%'
以及返回 4
的文档,即文档其中 subject
是“one”、“lastone”、“firstone”、“allone”、“everyone”、“noone”之一,author
是“abc”。
关于database - MongoDB 文本索引搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49884312/