ElasticSearch怎么同时对两个字段做sum聚合查询
发布网友
发布时间:2022-04-20 16:43
我来回答
共1个回答
热心网友
时间:2023-06-26 18:45
SearchRequestBuilder srb = client.prepareSearch(indexName)
.setTypes(typeName)
.setSearchType(SearchType.QUERY_THEN_FETCH);
...
TermsBuilder field1Terms = AggregationBuilders.terms("group_by_field1").field("field1").size(0);
SumBuilder field2Sum = AggregationBuilders.sum("sum_field2").field("field2");
SumBuilder field3Sum = AggregationBuilders.sum("sum_field3").field("field3");
field1Terms.subAggregation(field2Sum ).subAggregation(field3Sum );
...
srb.addAggregation(pIdTerms);
...
SearchResponse sr = srb.execute().actionGet();
...