Elastic Stack:es JavaApi聚合搜索入门
一.基本查询
需求一:按照颜色分组,计算每个颜色卖出的个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @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()); } } |
需求二:按照颜色分组,计算每个颜色卖出的个数,每个颜色卖出的平均价格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @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); } } |
需求三:按照颜色分组,计算每个颜色卖出的个数,以及每个颜色卖出的平均值、最大值、最小值、总和。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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); } |
标签:
Elastic Stack
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix