元素数组中的数组上的MongoDB全文 [英] MongoDB Full Text on an Array within an Array of Elements

查看:228
本文介绍了元素数组中的数组上的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" }

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

as documented at http://docs.mongodb.org/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全文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆