首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Kibana visualize解析ElasticSearch响应中的聚合

从Kibana visualize解析ElasticSearch响应中的聚合
EN

Stack Overflow用户
提问于 2020-02-28 13:08:44
回答 1查看 774关注 0票数 0

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

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

我想编写像Aggregations aggregationLv1 = response.getAggregations().get("2");...这样的代码,有人能帮上忙吗?

代码语言:javascript
复制
{
  "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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-29 14:05:57

我希望这能对你有所帮助。

代码语言:javascript
复制
    //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");
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60445577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档