gpt4 book ai didi

元素数组中的数组上的 MongoDB 全文

转载 作者:IT老高 更新时间:2023-10-28 13:26:40 29 4
gpt4 key购买 nike

当元素数组中的数组包含应与我的搜索匹配的文本时,我无法检索文档。

这里有两个示例文档:

{
_id: ...,
'foo': [
{
'name': 'Thing1',
'data': {
'text': ['X', 'X']
}
},{
'name': 'Thing2',
'data': {
'text': ['X', 'Y']
}
}
]
}

{
_id: ...,
'foo': [
{
'name': 'Thing3',
'data': {
'text': ['X', 'X']
}
},{
'name': 'Thing4',
'data': {
'text': ['X', 'Y']
}
}
]
}

通过使用以下查询,我能够返回两个文档:db.collection.find({'foo.data.text': {'$in': ['Y']}}

但是,我无法使用全文命令/索引返回这些结果:db.collection.runCommand("text", {search""Y"})

我确信全文搜索正在工作,因为对“Thing1”发出搜索的同一命令将返回第一个文档,而“Thing3”返回第二个文档。

我确定 foo.data.text 和 foo.name 在使用 db.collection.getIndexes() 时都在文本索引中。

我使用以下方法创建了索引:db.collection.ensureIndex({'foo.name': 'text', 'foo.data.text': 'text'})。以下是上述命令显示的索引:

    {
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"ns" : "testing.collection",
"background" : true,
"name" : "my_text_search",
"weights" : {
"foo.data.text" : 1,
"foo.name" : 1,
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 1
}

关于如何使用 mongo's full text search 的任何建议?

最佳答案

文本搜索目前不支持嵌套数组的索引字段(至少没有明确指定的)。 “foo.name”上的索引工作正常,因为它只有一个数组深,但文本搜索不会通过“foo.data.text”处的子数组递归。请注意,此行为可能会在 2.6 版本中发生变化。

但不要担心,同时嵌套数组可以是文本索引的,只是不能单独指定的字段。您可以使用通配符说明符 $** 递归索引集合中的所有字符串字段,即

db.collection.ensureIndex({"$**": "text" }

http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/ 中所述.不过要小心,因为这将索引每个字符串字段,并且可能会对存储和性能产生负面影响。您描述的简单文档结构应该可以正常工作。希望对您有所帮助。

关于元素数组中的数组上的 MongoDB 全文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18770185/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com