java操作es聚合操作并显示其他字段_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 resMap = new HashMap<>();

Sum downOutputSum = bucket.getAggregations().get("downOutputSum");

double value = downOutputSum.getValue();

Object key = bucket.getKey();

//key 是分组字段

//value是聚合value

}

标签:聚合,get,downOutputSum,groupByType,分组,javaAPI,terms,ES

来源: https://www.cnblogs.com/shuaidong/p/11930033.html

你可能感兴趣的:(java操作es聚合操作并显示其他字段_javaAPI操作ES分组聚合)