简体   繁体   English

如何在Elasticsearch中按文档数组中的多个对象字段进行查询?

[英]How to query by multiple fields of object in array of a document in Elasticsearch?

Hi I am newbie of Elasticsearch.大家好,我是 Elasticsearch 的新手。 And I want to query documents by the value of its field (type:array).我想通过其字段(类型:数组)的值来查询文档。 Example like this:像这样的例子:

docx=people:[
{id:1,role:admin},
{id:2,role:other}
]
docy=people:[
{id:1,role:other},
{id:2,role:admin}
]

My query is people.id:1 AND people.role:admin .我的查询是people.id:1 AND people.role:admin My expected result is only docx, but ES also returns docy.我的预期结果只是 docx,但 ES 也返回 docy。 I know it's wrong but how can I do it in ES.我知道这是错误的,但我怎么能在 ES 中做到这一点。 So how a query string can be to filter only document like docx.那么查询字符串如何才能仅过滤像 docx 这样的文档。 Thanks!谢谢!

You need to use the nested data-type in your case, as object data-type is flattened and treated separately which is causing both the documents to match the result, will add a working example.您需要在您的情况下使用嵌套数据类型,因为对象数据类型被展平并单独处理,这导致两个文档都匹配结果,将添加一个工作示例。

But please be aware that nested data-type becomes costly when you have large data-set and a high number of concurrent queries as explained in go-jek's medium blog.但是请注意,当您拥有大型数据集和大量并发查询时,嵌套数据类型会变得昂贵,如go-jek 的中型博客中所述。

Please follow this blog which highlights your issue in detail.请关注博客,其中详细介绍了您的问题。

Working example with proper mapping具有正确映射的工作示例

Index mapping索引映射

{
    "mappings": {
        "properties": {
            "name" : {
             "type" :   "text"
            },
            "people": {
                "type": "nested"
            }
        }
    }
}

index sample doc索引示例文档

{
    "name": "bar",
    "people": 
    [
        {
            "id": 1,
            "role": "other"
        },
        {
            "id": 2,
            "role": "admin"
        }
    ]
}

And second sample doc和第二个示例文档

{
    "name": "foo",
    "people": 
    [
        {
            "id": 1,
            "role": "admin"
        },
        {
            "id": 2,
            "role": "other"
        }
    ]
}

Search query搜索查询

{
    "query": {
        "nested": {
            "path": "people",
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "people.id": 1
                            }
                        },
                        {
                            "match": {
                                "people.role": "admin"
                            }
                        }
                    ]
                }
            }
        }
    }
}

And expected search result和预期的搜索结果

"hits": [
            {
                "_index": "matchphrase",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.6931472,
                "_source": {
                    "name": "foo",
                    "people": [
                        {
                            "id": 1,
                            "role": "admin"
                        },
                        {
                            "id": 2,
                            "role": "other"
                        }
                    ]
                }
            }
        ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM