美文网首页
ElasticSearch java client 查询汇总(二

ElasticSearch java client 查询汇总(二

作者: 背麻袋的袋鼠 | 来源:发表于2019-10-26 16:41 被阅读0次
    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<Terms.Bucket> 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 查询汇总(二

          本文链接:https://www.haomeiwen.com/subject/pclbhqtx.html