Elastic Stack:es JavaApi聚合搜索入门
一.基本查询
需求一:按照颜色分组,计算每个颜色卖出的个数
@Test public void testAggs() throws IOException { SearchRequest request = new SearchRequest("tvs"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.size(0); searchSourceBuilder.query(QueryBuilders.matchAllQuery()); searchSourceBuilder.aggregation(AggregationBuilders.terms("group_by_colors").field("color")); request.source(searchSourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); Aggregations aggregations = response.getAggregations(); Terms aggregation = aggregations.get("group_by_colors"); List<? extends Terms.Bucket> buckets = aggregation.getBuckets(); for (Terms.Bucket bucket : buckets) { System.out.println(bucket.getKeyAsString()); System.out.println(bucket.getDocCount()); } }
需求二:按照颜色分组,计算每个颜色卖出的个数,每个颜色卖出的平均价格
@Test public void testAggs2() throws IOException { SearchRequest request = new SearchRequest("tvs"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.size(0); searchSourceBuilder.aggregation( AggregationBuilders.terms("colors") .field("color") .subAggregation(AggregationBuilders.avg("avg_price").field("price"))); request.source(searchSourceBuilder); SearchResponse search = client.search(request, RequestOptions.DEFAULT); Aggregations aggregations = search.getAggregations(); Terms aggregation = aggregations.get("colors"); List<? extends Terms.Bucket> buckets = aggregation.getBuckets(); for (Terms.Bucket bucket : buckets) { Aggregations bucketAggregations = bucket.getAggregations(); Avg avg_price = bucketAggregations.get("avg_price"); double value = avg_price.getValue(); System.out.println(bucket.getKeyAsString()+":"+value); } }
需求三:按照颜色分组,计算每个颜色卖出的个数,以及每个颜色卖出的平均值、最大值、最小值、总和。
SearchRequest request = new SearchRequest("tvs"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.size(0); TermsAggregationBuilder colorAggregation = AggregationBuilders.terms("colors").field("color"); //放入多个子聚合 colorAggregation.subAggregation(AggregationBuilders.avg("avg_price").field("price")); colorAggregation.subAggregation(AggregationBuilders.min("min_price").field("price")); colorAggregation.subAggregation(AggregationBuilders.max("max_price").field("price")); colorAggregation.subAggregation(AggregationBuilders.sum("sum_price").field("price")); searchSourceBuilder.aggregation(colorAggregation); request.source(searchSourceBuilder); SearchResponse search = client.search(request, RequestOptions.DEFAULT); Aggregations aggregations = search.getAggregations(); Terms aggregation = aggregations.get("colors"); List<? extends Terms.Bucket> buckets = aggregation.getBuckets(); for (Terms.Bucket bucket : buckets) { Aggregations bucketAggregations = bucket.getAggregations(); Avg avg_price = bucketAggregations.get("avg_price"); double value = avg_price.getValue(); System.out.println(bucket.getKeyAsString()+"avg:"+value); Min min_price = bucketAggregations.get("min_price"); double min_priceValue = min_price.getValue(); System.out.println(bucket.getKeyAsString()+"min:"+min_priceValue); }
需求四:按照售价每2000价格划分范围,算出每个区间的销售总额 histogram
SearchRequest request = new SearchRequest("tvs"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.size(0); searchSourceBuilder.aggregation( AggregationBuilders.histogram("price").field("price").interval(2000). subAggregation(AggregationBuilders.sum("income").field("price"))); request.source(searchSourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); Aggregations aggregations = response.getAggregations(); Histogram price = aggregations.get("price"); List<? extends Histogram.Bucket> buckets = price.getBuckets(); for (Histogram.Bucket bucket : buckets) { Aggregations aggregations1 = bucket.getAggregations(); System.out.println("string:"+bucket.getKeyAsString()); System.out.println("count:"+bucket.getDocCount()); Sum income = aggregations1.get("income"); double value = income.getValue(); System.out.println("value:"+value); }