ElasticSearch java client 查询汇总(二)

1 分组聚合
SearchResponse searchResponse = 
        client.prepareSearch("product")
            .setTypes("digital")
            .setQuery(QueryBuilders.matchAllQuery())
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            //根据name分组,并分别计算总价
           .addAggregation(
                 AggregationBuilders.terms("group_age").field("name")
         .subAggregation(AggregationBuilders.sum("sum_price").field("price")))
            .get();
    Terms terms = searchResponse.getAggregations().get("group_age");
    List buckets = terms.getBuckets();
    for (Terms.Bucket bucket : buckets) {
        Sum sum  = bucket.getAggregations().get("sum_price");
        System.out.println(bucket.getKey() + " " + bucket.getDocCount() + "  " + sum.getValue());
    }
2

你可能感兴趣的:(ElasticSearch java client 查询汇总(二))