elasticsearch嵌套的第一行和第二行查询

时间:2019-06-19 06:21:09

标签: elasticsearch-5

一个Elasticsearch DSL查询问题,索引中有一个嵌套的类型字段Person,Person有一个Country字段,索引中的Person插入了两个数据,您需要查询第一行的Country = A和Country = B第二行。

索引表结构:

http://127.0.0.1:9200/event

{
    "mappings": {
        "event": {
            "properties": {
                "Quantity": {
                    "type": "integer"
                },
                "Person": {
                    "type": "nested",
                    "properties": {
                        "CountryCode": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "ignore_above": 256,
                                    "type": "keyword"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

插入数据:

{"Quantity":10,"Person":[{"CountryCode":"A"},{"CountryCode":"B"}]}
{"Quantity":5,"Person":[{"CountryCode":"B"},{"CountryCode":"A"}]}
{"Quantity":3,"Person":[{"CountryCode":"A"},{"CountryCode":"C"}]}
{"Quantity":7,"Person":[{"CountryCode":"D"},{"CountryCode":"E"}]}

查询结果,此语句的结果无法确定第一行中的Country = A,第二行中的Country = B:

http://127.0.0.1:9200/event/event/_search 开机自检

{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "Person",
                        "query": {
                            "bool": {
                                "must": {
                                    "match": {
                                        "Person.CountryCode.keyword": "A"
                                    }
                                }
                            }
                        }
                    }
                },
                {
                    "nested": {
                        "path": "Person",
                        "query": {
                            "bool": {
                                "must": {
                                    "match": {
                                        "Person.CountryCode.keyword": "B"
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
}

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2.4079456,
        "hits": [
            {
                "_index": "event",
                "_type": "event",
                "_id": "2",
                "_score": 2.4079456,
                "_source": {
                    "Quantity": 5,
                    "Person": [
                        {
                            "CountryCode": "B"
                        },
                        {
                            "CountryCode": "A"
                        }
                    ]
                }
            },
            {
                "_index": "event",
                "_type": "event",
                "_id": "1",
                "_score": 1.3862944,
                "_source": {
                    "Quantity": 10,
                    "Person": [
                        {
                            "CountryCode": "A"
                        },
                        {
                            "CountryCode": "B"
                        }
                    ]
                }
            }
        ]
    }
}

0 个答案:

没有答案