今天在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