elasticsearch中的filter与aggs
今天在ES上做了一个聚合,先过滤一个嵌套对象,再对另一个域做聚合,但是过滤似乎没有起作用
{ "size":0, "filter":{ "nested":{ "path":"nna_risks", "filter":{ "exists":{ "field":"nna_risks.ina_id" } } } }, "aggs":{ "level0":{ "terms":{ "script":"doc['inp_type'].value" } } } }
结果如下:
took: 47 timed_out: false ▾ _shards{} total: 25 successful: 25 failed: 0 ▾ hits{} total: 31470 max_score: 0 ▾ hits[] ▾ aggregations{} ▾ level0{} doc_count_error_upper_bound: 0 sum_other_doc_count: 0 ▾ buckets[] key: "2" doc_count: 147617 key: "5" doc_count: 139434 key: "3" doc_count: 47220 key: "1" doc_count: 24580 key: "4" doc_count: 10148
从结果上看,聚合到的数目相加与Hits不相等;分析后发现,聚合的数据不是来源于过滤后,而是整个数据集;
修改方法, 不用过滤,而是用查询过滤
{ "size":0, "query":{ "filtered":{ "filter":{ "nested":{ "path":"nna_risks", "filter":{ "exists":{ "field":"nna_risks.ina_id" } } } } }
}, "aggs":{ "level0":{ "terms":{ "script":"doc['inp_type'].value" } } } }
结果集:
took: 14 timed_out: false ▾ _shards{} total: 25 successful: 25 failed: 0 ▾ hits{} total: 31617 max_score: 0 ▾ hits[] ▾ aggregations{} ▾ level0{} doc_count_error_upper_bound: 0 sum_other_doc_count: 0 ▾ buckets[] key: "3" doc_count: 18634 key: "1" doc_count: 7464 key: "2" doc_count: 2845 key: "5" doc_count: 1738 key: "4" doc_count: 936
或者在聚合体中过滤:
{ "size":0, "aggs":{ "level0":{ "filter":{ "nested":{ "path":"nna_risks", "filter":{ "exists":{ "field":"nna_risks.ina_id" } } } }, "aggs":{ "level1":{ "terms":{ "script":"doc['inp_type'].value" } } } } } }
结果为:
took: 36 timed_out: false ▾ _shards{} total: 25 successful: 25 failed: 0 ▾ hits{} total: 375035 max_score: 0 ▾ hits[] ▾ aggregations{} ▾ level0{} doc_count: 31836 ▾ level1{} doc_count_error_upper_bound: 0 sum_other_doc_count: 0 ▾ buckets[] key: "3" doc_count: 18727 key: "1" doc_count: 7525 key: "2" doc_count: 2878 key: "5" doc_count: 1743 key: "4" doc_count: 963