首页 > 解决方案 > 解析来自 Kibana 的 ElasticSearch 响应中的聚合可视化

问题描述

我有一个来自kibana 可视化的响应,聚合内部有两个级别,我想用java 解析它,并获取第二级存储桶中的数据。

我现在可以 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
}

标签: javaelasticsearchkibanaelasticsearch-aggregation

解决方案


我希望这可以帮助你。

    //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");
    }

推荐阅读