Reputation: 54
I have a response from kibana visualize, the aggregation has two level inside, I want to parse it with java, and get the data in the second level bucket.
I can the SearchResponse object now, I rather not to convert it to string and use a json library to parse the object.
I want to code like Aggregations aggregationLv1 = response.getAggregations().get("2");...
can any one help?
{
"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
}
Upvotes: 0
Views: 1531
Reputation: 146
I hope this can help you.
//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");
}
Upvotes: 1