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

编程入门 行业动态 更新时间:2024-10-25 13:27:16
本文介绍了元素数组中的数组上的MongoDB全文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am unable to retrieve documents when an array within an array of elements contains text that should match my search.

以下是两个示例文档:

{ _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']}}

By using the following query, I am able to return both documents: db.collection.find({'foo.data.text': {'$in': ['Y']}}

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

However, I am unable to return these results using the full text command/index: db.collection.runCommand("text", {search" "Y"})

我确信全文搜索是有效的,因为对"Thing1"发出搜索的同一命令将返回第一个文档,而"Thing3"返回第二个文档.

I am certain that the full text search is working, as the same command issuing a search against "Thing1" will return the first document, and "Thing3" returns the second document.

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

I am certain that both foo.data.text and foo.name are both in the text index when using db.collection.getIndexes().

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

I created my index using: db.collection.ensureIndex({'foo.name': 'text', 'foo.data.text': 'text'}). Here are the indexes as shown by the above command:

{ "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的全文的建议搜索?

Any suggestion on how to get this working with mongo's full text search?

推荐答案

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

Text search does not currently support indexed fields of nested arrays (at least not explicitly specified ones). An index on "foo.name" works fine as it is only one array deep, but the text search will not recurse through the subarray at "foo.data.text". Note that this behavior may change in the 2.6 release.

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

But fear not, in the meantime nested arrays can be text-indexed, just not with individually specified fields. You may use the wildcard specifier $** to recursively index ALL string fields in your collection, i.e.

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

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

as documented at docs.mongodb/manual/tutorial/create-text-index-on-multiple-fields/ . Be careful though as this will index EVERY string field and could have negative storage and performance consequences. The simple document structure you describe though should work fine. Hope that helps.

更多推荐

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

本文发布于:2023-11-22 15:16:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:素数   数组   组中   全文   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!