AggregationBuilders.terms 相当于sql中的group by
AggregationBuilders.terms("name").field("id");
其中 name值自定义,id为需要分组的key
ValueCountAggregationBuilder countByFloor = AggregationBuilders.count("count").field("buildId.keyword");
TermsAggregationBuilder gourpByFloorId = AggregationBuilders.terms("buildId").field("buildId.keyword")
.subAggregation(countByFloor)
根据建筑id分组,并统计每栋建筑下总共有多少个分户。可在分组是加上要统计count ,即 .subAggregation(countByFloor);
SumBucketPipelineAggregationBuilder houseSum = PipelineAggregatorBuilders.sumBucket("houseSum", "buildId.count");
ValueCountAggregationBuilder countByFloor = AggregationBuilders.count("count").field("buildId.keyword");
TermsAggregationBuilder gourpByFloorId = AggregationBuilders.terms("buildId").field("buildId.keyword")
.subAggregation(countByFloor);
TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("cata").field("useAndClassName.keyword")
.subAggregation(houseSum)
.subAggregation(gourpByFloorId);
根据buildId 聚合后,需要根据聚合后的值进行二次计算,则需要使用 PipelineAggregatorBuilders。
PipelineAggregatorBuilders.sumBucket(“houseSum”, “buildId.count”);
其中houseSum为自定义的值,buildId.count则必须是,聚合时的统计的key值