兩種方式
1、大桶套小桶,通過terms一層層聚合
這個方法適用於需要統計每一項的數據,比如a中有多少種b
2、函數擴展(script)聚合
這個方法適用於直接統計有多少種組合
下面是方法2的具體實現:
統計:
GET ****_20190926/_search { "size": 0, "aggs": { "pre": { "terms": { "script": "doc['inChannel'].values +'####'+doc['resCode'].values", "size": 5 } } } }
結果:
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 18, "max_score": 0, "hits": [] }, "aggregations": { "pre": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "[7220101]####[0]", "doc_count": 13 }, { "key": "[]####[]", "doc_count": 2 }, { "key": "[1020201]####[]", "doc_count": 1 }, { "key": "[10202]####[]", "doc_count": 1 }, { "key": "[7220101]####[]", "doc_count": 1 } ] } } }
java代碼參考:
Script script = new Script("doc['inChannel'].values +'####'+ doc['resCode'].values"); //用於統計每一項詳細數據 TermsAggregationBuilder app = AggregationBuilders.terms("app").script(script).size(10000); //用於統計有多少項 CardinalityAggregationBuilder app = AggregationBuilders.cardinality("app").script(script).precisionThreshold(10000);