我有一个嵌套的对象映射,示例数据:
{ "_index": "simpleindex", "_type": "games", "_id": "AU_eC-Uzt6KxlUliF68N", "_score": 1, "_source": { "continents": [ { "name": "Asia", "countries": [ { "name": "India", "states": [ { "name": "TN", "game": "soccor", "wins": 1 } ] }, { "name": "India", "states": [ { "name": "KA", "game": "soccor", "wins": 1 } ] } ] } ] } }, { "_index": "simpleindex", "_type": "games", "_id": "AU_eCf5dt6KxlUliF637", "_score": 1, "_source": { "continents": [ { "name": "Asia", "countries": [ { "name": "India", "states": [ { "name": "TN", "game": "soccor", "wins": 1 } ] } ] } ] } }, { "_index": "simpleindex", "_type": "games", "_id": "AU_eDIdXt6KxlUliF69i", "_score": 1, "_source": { "continents": [ { "name": "Asia", "countries": [ { "name": "India", "states": [ { "name": "TN", "game": "soccor", "wins": 1 } ] }, { "name": "India", "states": [ { "name": "KA", "game": "soccor", "wins": 1 } ] }, { "name": "Pak", "states": [ { "name": "NA", "game": "soccor", "wins": 1 } ] } ] } ] } }
这是我的过滤汇总,可返回与过滤条件匹配的文档(即,大陆应为“亚洲”,国家应为“印度”):
{ "aggs": { "DocumentSet": { "filter": { "and": { "filters": [ { "nested": { "path": "continents", "query": { "match": { "continents.name": "asia" } } } }, { "nested": { "path": "continents.countries", "query": { "match": { "continents.countries.name": "india" } } } } ] } }, "aggs": { "continents": { "nested": { "path": "continents" }, "aggs": { "countries": { "nested": { "path": "continents.countries" }, "aggs": { "states": { "nested": { "path": "continents.countries.states" }, "aggs": { "count": { "value_count": { "field": "continents.countries.states.wins" } } } } } } } } } }}}
这是结果(副本仅将聚合粘贴到此处):
"aggregations": { "DocumentSet": { "doc_count": 3, "continents": { "doc_count": 3, "countries": { "doc_count": 6, "states": { "doc_count": 6, "count": { "value": 6 } } } } } }
我的意图是仅从中获得“胜利” continents.name=asia AND countries.name=india。过滤器可以按预期工作,但是我只需要将聚合范围缩小到countries.name=india; 本质上是Filter聚合返回的文档的作用域的另一个级别,因此叶聚合计数为5而不是6。
continents.name=asia AND countries.name=india
countries.name=india
尝试以下聚合:
{ "aggs": { "continents": { "nested": { "path": "continents" }, "aggs": { "asia_continent": { "filter": { "query": { "match": { "continents.name": "asia" } } }, "aggs": { "countries": { "nested": { "path": "continents.countries" }, "aggs": { "india_country": { "filter": { "query": { "match": { "continents.countries.name": "india" } } }, "aggs": { "states": { "nested": { "path": "continents.countries.states" }, "aggs": { "count": { "value_count": { "field": "continents.countries.states.wins" } } } } } } } } } } } } } }