这篇文章 将已实战的方式带你 实现es 的各种分组操作
@Autowired
RestHighLevelClient restHighLevelClient ;
private String tvIndex = "tvs" ;
@Test
public void testAgg() throws IOException {
//1. 构建请求
SearchRequest request = new SearchRequest(tvIndex);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询
// size(0) :不需要 查询的结果 , 只需要分组的结果
searchSourceBuilder.size(0) ;
searchSourceBuilder.query(QueryBuilders.matchAllQuery()) ;
// 构建Agg
TermsAggregationBuilder colorAgg = AggregationBuilders.terms("group_by_color").field("color");
// agg结合进 searchSourceBuilder
searchSourceBuilder.aggregation(colorAgg);
//结合
request.source(searchSourceBuilder);
//2. 执行
SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
//3.处理返回结果
Aggregations aggregations = searchResponse.getAggregations();
//他是按照Terms 进行聚合的 所以取出来也需要 Terms 获取运行结果
Terms colorAggRes = aggregations.get("group_by_color");
List<? extends Terms.Bucket> colorBuckets = colorAggRes.getBuckets();
// 遍历结果
for (Terms.Bucket colorBucket : colorBuckets) {
System.out.println("key: "+ colorBucket.getKeyAsString());
System.out.println("docCount: "+colorBucket.getDocCount());
System.out.println("=================");
}
}
GET /tvs/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
@Test
public void testAgg02() throws IOException {
//1. 构建请求
SearchRequest request = new SearchRequest(tvIndex);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询
// size(0) :不需要 查询的结果 , 只需要分组的结果
searchSourceBuilder.size(0) ;
searchSourceBuilder.query(QueryBuilders.matchAllQuery()) ;
// 构建Agg
TermsAggregationBuilder colorAgg = AggregationBuilders.terms("group_by_color").field("color");
colorAgg.subAggregation(AggregationBuilders.avg("avgPrice").field("price"));
// agg结合进 searchSourceBuilder
searchSourceBuilder.aggregation(colorAgg);
//结合
request.source(searchSourceBuilder);
//2. 执行
SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
//3.处理返回结果
Aggregations aggregations = searchResponse.getAggregations();
//他是按照Terms 进行聚合的 所以取出来也需要 Terms 获取运行结果
Terms colorAggRes = aggregations.get("group_by_color");
List<? extends Terms.Bucket> colorBuckets = colorAggRes.getBuckets();
// 遍历结果
for (Terms.Bucket colorBucket : colorBuckets) {
System.out.println("key: "+ colorBucket.getKeyAsString());
System.out.println("docCount: "+colorBucket.getDocCount());
//获得子聚合
Aggregations subAgg = colorBucket.getAggregations();
// 他是一个AVG 的聚合
Avg avgPrice = subAgg.get("avgPrice");
double value = avgPrice.getValue();
System.out.println("已颜色区分的话平均价格是: "+value);
System.out.println("=================");
}
}
GET /tvs/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
// 需求三:按照颜色分组,计算每个颜色卖出的个数,以及每个颜色卖出的平均值、最大值、最小值、总和。
@Test
public void testAgg03() throws IOException {
//1. 构建请求
SearchRequest request = new SearchRequest(tvIndex);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询
// size(0) :不需要 查询的结果 , 只需要分组的结果
searchSourceBuilder.size(0) ;
searchSourceBuilder.query(QueryBuilders.matchAllQuery()) ;
// 构建Agg
TermsAggregationBuilder colorAgg = AggregationBuilders.terms("group_by_color").field("color");
colorAgg.subAggregation(AggregationBuilders.avg("avgPrice").field("price"));
colorAgg.subAggregation(AggregationBuilders.max("maxPrice").field("price"));
colorAgg.subAggregation(AggregationBuilders.min("minPrice").field("price"));
// agg结合进 searchSourceBuilder
searchSourceBuilder.aggregation(colorAgg);
//结合
request.source(searchSourceBuilder);
//2. 执行
SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
//3.处理返回结果
Aggregations aggregations = searchResponse.getAggregations();
//他是按照Terms 进行聚合的 所以取出来也需要 Terms 获取运行结果
Terms colorAggRes = aggregations.get("group_by_color");
List<? extends Terms.Bucket> colorBuckets = colorAggRes.getBuckets();
// 遍历结果
for (Terms.Bucket colorBucket : colorBuckets) {
System.out.println("key: "+ colorBucket.getKeyAsString());
System.out.println("docCount: "+colorBucket.getDocCount());
//获得子聚合
Aggregations subAgg = colorBucket.getAggregations();
// 他是一个AVG 的聚合
Avg avgPrice = subAgg.get("avgPrice");
Max maxPrice = subAgg.get("maxPrice");
Min minPrice = subAgg.get("minPrice");
double value = avgPrice.getValue();
System.out.println("已颜色区分的话平均价格是: "+avgPrice.getValue());
System.out.println("已颜色区分的话最大价格是: "+maxPrice.getValue());
System.out.println("已颜色区分的话最小价格是: "+minPrice.getValue());
System.out.println("=================");
}
}
GET /tvs/_search
{
"size" : 0,
"aggs":{
"price":{
"histogram":{
"field": "price",
"interval": 2000
},
"aggs":{
"income": {
"sum": {
"field" : "price"
}
}
}
}
}
}
@Test
public void testAgg05() throws IOException {
SearchRequest request = new SearchRequest(tvIndex);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery()) ;
searchSourceBuilder.size(0) ;
// price agg
HistogramAggregationBuilder priceAgg =
AggregationBuilders.histogram("priceAggIn").field("price").interval(2000);
// price agg sum
priceAgg.subAggregation(AggregationBuilders.sum("sumPrice").field("price"));
//综合
searchSourceBuilder.aggregation(priceAgg) ;
request.source(searchSourceBuilder) ;
//执行查询
SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 获得 Agg
Aggregations aggregations = searchResponse.getAggregations();
Histogram priceAggIn = aggregations.get("priceAggIn");
for (Histogram.Bucket bucket : priceAggIn.getBuckets()) {
System.out.println("priceAggIn的总数是: "+bucket.getDocCount());
System.out.println("priceAggIn的key 是: "+bucket.getKeyAsString());
Aggregations subAgg = bucket.getAggregations();
Sum sumPrice = subAgg.get("sumPrice");
System.out.println("价格区间中的总价是: "+sumPrice.getValue());
}
}
GET /tvs/_search
{
"size" : 0,
"aggs": {
"sales": {
"date_histogram": {
"field": "sold_date",
"interval": "month",
"format": "yyyy-MM-dd",
"min_doc_count" : 0,
"extended_bounds" : {
"min" : "2019-01-01",
"max" : "2020-12-31"
}
}
}
}
}
// 按照季度进行间隔
@Test
public void t3() throws IOException {
SearchRequest searchRequest = new SearchRequest(tvIndex);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery()) ;
searchSourceBuilder.size(0) ;
// make Agg
DateHistogramAggregationBuilder dataHis = AggregationBuilders.dateHistogram("dataHistogram")
.field("sold_date")
//按照季度进行分组
.calendarInterval(DateHistogramInterval.QUARTER)
.format("yyyy-MM-dd")
// 以上的min_doc_count参数会强制返回空桶
.minDocCount(0)
.extendedBounds(new ExtendedBounds("2019-01-01","2019-01-01")) ;
//总结
searchSourceBuilder.aggregation(dataHis);
searchRequest.source(searchSourceBuilder);
//查询
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
Aggregations aggregations = searchResponse.getAggregations();
Histogram dataHistogram = aggregations.get("dataHistogram");
for (Histogram.Bucket bucket : dataHistogram.getBuckets()) {
System.out.println("key :"+bucket.getKeyAsString());
System.out.println("count :"+bucket.getDocCount());
}
}