javaAPI操作ES分组聚合
Posted shuaidong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaAPI操作ES分组聚合相关的知识,希望对你有一定的参考价值。
连接es的客户端使用的 TransportClient
SearchRequestBuilder requestBuilder = transportClient.prepareSearch(indies).setTypes(TYPE_NAME);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
AggregationBuilder groupByType = AggregationBuilders.terms("分组别名").field("分组字段");
AggregationBuilder sumDownOutputSum = AggregationBuilders.sum("聚合别名").field("聚合字段");
//sub是子聚合
requestBuilder.setQuery(boolQueryBuilder).addAggregation(groupByType.subAggregation(sumDownOutputSum));
SearchResponse response = requestBuilder.get();
Terms terms = response.getAggregations().get("groupByType");
for (Terms.Bucket bucket : terms.getBuckets()) {
Map<String, Object> resMap = new HashMap<>();
Sum downOutputSum = bucket.getAggregations().get("downOutputSum");
double value = downOutputSum.getValue();
Object key = bucket.getKey();
//key 是分组字段
//value是聚合value
}
以上是关于javaAPI操作ES分组聚合的主要内容,如果未能解决你的问题,请参考以下文章
ElasticSearch聚合查询Restful语法和JavaApi详解(基于ES7.6)