【问题标题】:Parsing aggregation in ElasticSearch response from Kibana visualize解析来自 Kibana 的 ElasticSearch 响应中的聚合可视化
【发布时间】:2020-06-12 04:40:51
【问题描述】:

我有一个kibana可视化的响应,聚合里面有两层,我想用java解析,得到第二层bucket中的数据。

我现在可以 SearchResponse 对象,我宁愿不将其转换为字符串并使用 json 库来解析对象。

我想像 Aggregations aggregationLv1 = response.getAggregations().get("2");... 这样编码 有人可以帮忙吗?

{
  "took": 1901,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1764055,
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "2": {
      "buckets": [
        {
          "3": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 246323,
            "buckets": [
              {
                "key": 128,
                "doc_count": 25730
              },
              {
                "key": 97,
                "doc_count": 24638
              },
              {
                "key": 234,
                "doc_count": 19059
              },
              {
                "key": 14,
                "doc_count": 17702
              },
              {
                "key": 224,
                "doc_count": 15525
              }
            ]
          },
          "key_as_string": "2019-11-01T00:00:00.000+08:00",
          "key": 1572537600000,
          "doc_count": 348977
        },
        {
          "3": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 375938,
            "buckets": [
              {
                "key": 97,
                "doc_count": 34796
              },
              {
                "key": 234,
                "doc_count": 30293
              },
              {
                "key": 14,
                "doc_count": 29452
              },
              {
                "key": 128,
                "doc_count": 28964
              },
              {
                "key": 107,
                "doc_count": 22982
              }
            ]
          },
          "key_as_string": "2019-12-01T00:00:00.000+08:00",
          "key": 1575129600000,
          "doc_count": 522425
        },
        {
          "3": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 328907,
            "buckets": [
              {
                "key": 234,
                "doc_count": 31528
              },
              {
                "key": 14,
                "doc_count": 31434
              },
              {
                "key": 97,
                "doc_count": 30190
              },
              {
                "key": 128,
                "doc_count": 26213
              },
              {
                "key": 107,
                "doc_count": 25116
              }
            ]
          },
          "key_as_string": "2020-01-01T00:00:00.000+08:00",
          "key": 1577808000000,
          "doc_count": 473388
        },
        {
          "3": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 300432,
            "buckets": [
              {
                "key": 97,
                "doc_count": 25303
              },
              {
                "key": 234,
                "doc_count": 24435
              },
              {
                "key": 107,
                "doc_count": 23829
              },
              {
                "key": 128,
                "doc_count": 23058
              },
              {
                "key": 17,
                "doc_count": 22208
              }
            ]
          },
          "key_as_string": "2020-02-01T00:00:00.000+08:00",
          "key": 1580486400000,
          "doc_count": 419265
        }
      ]
    }
  },
  "status": 200
}

【问题讨论】:

    标签: java elasticsearch kibana elasticsearch-aggregation


    【解决方案1】:

    希望对你有帮助。

        //1.query result
        Aggregations aggregations = template.query(query, SearchResponse::getAggregations);
    
        //2.the first aggregation
        Terms terms1 = aggregations.get("2");
        List<? extends Terms.Bucket> buckets = terms1.getBuckets();
        for (Terms.Bucket bucket : buckets) {
            //3.content from bucket
            long docCount = bucket.getDocCount();
            Number number = bucket.getKeyAsNumber();
    
            //4.the second aggregation(If you want to go to the next)
            Aggregations aggregations2 = bucket.getAggregations();
            Terms terms2_0 = aggregations2.get("0");
            Terms terms2_1 = aggregations2.get("1");
            Terms terms2_2 = aggregations2.get("2");
            Terms terms2_3 = aggregations2.get("3");
        }
    

    【讨论】:

      猜你喜欢
      • 2019-01-24
      • 2019-08-25
      • 2020-02-12
      • 2018-01-11
      • 2020-09-15
      • 2019-05-18
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多