多字段分组并求和;实现sql:select field1, field2, sum(field3) from table_name group by field1, field2;
多字段分组并求和;实现sql:select field1, field2, top(field3) from table_name group by field1, field2;
ES java 样例:
/**
* 多字段分组并求和;实现sql:select field1, field2, sum(field3) from table_name group by field1, field2;
*/
@Test
public void sumAfterTwiceAgg() {
SearchResponse response = client.prepareSearch("test_index").setTypes("tweet")
.addAggregation(AggregationBuilders.terms("userAgg").field("user.keyword")
.subAggregation(AggregationBuilders.terms("sexAgg").field("sex.keyword")
.subAggregation(AggregationBuilders.sum("sumAgg").field("age")))) //求和要放到最内层的分组语句里面
.get();
Terms userAgg = response.getAggregations().get("userAgg");
for (Terms.Bucket entry : userAgg.getBuckets()) {
Terms sexAgg = entry.getAggregations().get("sexAgg");
for (Terms.Bucket entry2 : sexAgg.getBuckets()) {
Sum sum = entry2.getAggregations().get("sumAgg");
System.out.println("user:" + entry.getKey()+"-------sex:"+entry2.getKey() + "----------sum:" + sum.getValue());
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/14062.html