Today I made an aggregation on ES, filtering a nested object and then aggregating the other domain, but filtering doesn't seem to work
{ "size": 0,"Filter":{ "nested":{ "Path":"Nna_risks", "Filter":{ "exists":{ "Field":"nna_risks.ina_id" } } } }, "Aggs":{ "level0":{ "Terms":{ "Script":"doc[' Inp_type '].value" } } }}
The results are as follows:
Took:47timed_out:false? _shards{}total:25Successful:25failed:0? Hits{}total:31470max_score:0 hits[]? aggregations{}? level0{}doc_count_error_upper_bound:0sum_other_doc_count:0? Buckets[]key: "2"Doc_count:147617Key:"5"Doc_count:139434Key:"3"Doc_count:47220Key:"1"Doc_count:24580Key:"4"Doc_count:10148
From the results, the aggregated number is not equal to the hits, and after analysis, it is found that the aggregated data is not from the filter, but the whole data set;
Modify the method, do not filter, but use query filtering
{ "size": 0,"Query":{ "Filtered": {"Filter": {"nested": {"Path":"Nna_risks","Filter": {"exists": {"Field":"nna_risks.ina_id"}}}}
}, "Aggs":{ "level0":{ "Terms":{ "Script":"doc[' Inp_type '].value" } } }}
Result set:
Took:14timed_out:false? _shards{}total:25Successful:25failed:0? Hits{}total:31617max_score:0 hits[]? aggregations{}? level0{}doc_count_error_upper_bound:0sum_other_doc_count:0? Buckets[]key: "3"Doc_count:18634Key:"1"Doc_count:7464Key:"2"Doc_count:2845Key:"5"Doc_count:1738Key:"4"Doc_count:936
or filter in the polymer:
{ "size": 0,"Aggs":{ "level0":{ "Filter":{ "nested":{ "Path":"Nna_risks", "Filter":{ "exists":{ "Field":"nna_risks.ina_id" } } } }, "Aggs":{ "Level1":{ "Terms":{ "Script":"doc[' Inp_type '].value" } } } } }}
The result is:
Took:36timed_out:false? _shards{}total:25Successful:25failed:0? Hits{}total:375035max_score:0 hits[]? aggregations{}? Level0{}doc_count:31836level1{}doc_count_error_upper_bound:0sum_other_doc_count:0? Buckets[]key:"3"Doc_count:18727Key:"1"Doc_count:7525Key:"2"Doc_count:2878Key:"5"Doc_count:1743Key:"4"Doc_count:963
Filter and Aggs in Elasticsearch