【ElasticSearch】java

聚合查询

分类

参考

Elasticsearch笔记(七):聚合查询_subaggregation-CSDN博客

示例

AggregationBuilders.terms 相当于sql中的group by,其中 name值自定义,id为需要分组的key

AggregationBuilders.terms("name").field("id");

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值

date_histogram 聚合

参考:

elasticsearch 聚合之 date_histogram 聚合 - huan1993 - 博客园 (cnblogs.com)

你可能感兴趣的:(应用,elasticsearch)