欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Javaelasticsearch-rest-high-level-client学习笔记2--聚合

时间:2023-05-05

准备数据来自elasticsearch系统学习笔记9-聚合分析 Aggregations(补充1)

基本流程

RestHighLevelClient restHighLevelClient = getRestHighLevelClient();try { // 索引名 String indexName = "cars_transactions"; // 构建聚合语句 // 创建terms桶聚合 , 聚合名字 colors_agg , 根据 color 分组 TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("colors_agg").field("color"); // 嵌套聚合 设置 avg 指标聚合,聚合名字 avg_price, 字段 price,计算平均价格 aggregationBuilder.subAggregation(AggregationBuilders.avg("avg_price").field("price")); // 构建 SearchSourceBuilder SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.matchAllQuery()); sourceBuilder.size(0); sourceBuilder.aggregation(aggregationBuilder); // 聚合 // 构建 searchRequest SearchRequest searchRequest = new SearchRequest(); searchRequest.source(sourceBuilder); searchRequest.indices(indexName); // 执行请求 SearchResponse searchResponse = restHighLevelClient.search(searchRequest); // 遍历查询结果 listAggregations(searchResponse);} catch (Exception e) { e.printStackTrace();} finally { if (restHighLevelClient != null) { restHighLevelClient.close(); }}

public static void listAggregations(SearchResponse searchResponse) { // 处理聚合查询结果 Aggregations aggregations = searchResponse.getAggregations(); // 根据by_shop名字查询terms聚合结果 Terms byShopAggregation = aggregations.get("colors_agg"); // 遍历terms聚合结果 for (Terms.Bucket bucket : byShopAggregation.getBuckets()) { // 因为是根据shop_id分组,因此可以直接将桶的key转换成int类型 String color = bucket.getKeyAsString(); long colorCount = bucket.getDocCount(); System.out.print("颜色 " + color + "n数量 : " + colorCount); // 根据avg_price聚合名字,获取嵌套聚合结果 Avg avg = bucket.getAggregations().get("avg_price"); // 获取平均价格 double avgPrice = avg.getValue(); System.out.println(" ; 平均价格 : " + avgPrice); }}

指标 总数和总和

ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count("count_orders").field("order_id");SumAggregationBuilder sumAggregationBuilder = AggregationBuilders.sum("total_sale").field("price");// 设置聚合查询,可以设置多个聚合查询条件,只要聚合查询命名不同就行sourceBuilder.aggregation(valueCountAggregationBuilder);sourceBuilder.aggregation(sumAggregationBuilder);// 处理结果ValueCount valueCount = aggregations.get("count_orders");Sum totalPrice = aggregations.get("total_sale");

最大最小值

MaxAggregationBuilder maxAggregationBuilder = AggregationBuilders.max("max_price").field("price");MinAggregationBuilder minAggregationBuilder = AggregationBuilders.min("min_price").field("price");Max maxPrice = aggregations.get("max_price");Min minPrice = aggregations.get("min_price");

桶 terms 分桶

TermsAggregationBuilder byShop = AggregationBuilders.terms("by_shop").field("shop_id");

等等

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。