elasticsearch中多個字段聚合及java實現

引言

  假設須要將a,b,c三個字段進行聚合(作笛卡兒積)的話,有兩種方法:app

  一、大桶套小桶,經過terms一層層聚合
    這個方法適用於須要統計每一項的數據,好比a中有多少種b
   此方法再次不表elasticsearch

  二、函數擴展(script)聚合
    這個方法適用於直接統計有多少種組合ide

  博主新人一個,只談方法不深刻,未來有研究再更新函數

  es版本5.6ui

參考

  https://blog.csdn.net/qq_28988969/article/details/84337405spa

  https://blog.csdn.net/weixin_41279060/article/details/78852704.net

es查詢語句

{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "app": {
      "terms": {
     //聚合的key用####分隔
        "script": "doc['appInfo.appName'].values +'####'+doc['appInfo.appVersion'].values+'####'+doc['device'].values",
        "size": 5
      }
    }
  }
}

結果

   

代碼版:code

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 650,
        "max_score": 0,
        "hits": [ ]
    },
    "aggregations": {
        "app": {
            "doc_count_error_upper_bound": 5,
            "sum_other_doc_count": 190,
            "buckets": [
            {
                "key": "[sdklib]####[1.0]####[Android]",
                "doc_count": 173
            }
            ,
            {
                "key": "[開源中國]####[2.4]####[Android]",
                "doc_count": 150
            }
            ,
            {
                "key": "[sss]####[1.0]####[Android]",
                "doc_count": 77
            }
            ,
            {
                "key": "[ProguardTest]####[1.0]####[Android]",
                "doc_count": 34
            }
            ,
            {
                "key": "[My Application]####[1.0]####[Android]",
                "doc_count": 26
            }
            ]
        }
    }
}
View Code

JavaAPI

Script script = new Script("doc['anrType'].values +'####'+ doc['anrMessage'].values");

TermsAggregationBuilder app = AggregationBuilders.terms("app").script(script).size(10000);
//用於統計每一項詳細數據

CardinalityAggregationBuilder app = AggregationBuilders.cardinality("app").script(script).precisionThreshold(10000);
//用於統計有多少項
相關文章
相關標籤/搜索