• ElasticSearch High Level REST API【7】聚合


    获取平均值聚合示例,最大值、最小值、求和类似

    public void aggregation(){
           RestHighLevelClient client = elasticClient.getRestHighLevelClient();
           SearchRequest searchRequest = new SearchRequest();
           SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
           TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_sex")
                   .field("sex.keyword");   //text类型不能用于索引或排序,必须转成keyword类型
           aggregation.subAggregation(AggregationBuilders.avg("avg_age")
                   .field("age"));  //avg_age 为子聚合名称,名称可随意
           searchSourceBuilder.aggregation(aggregation);
           searchRequest.source(searchSourceBuilder);
           SearchResponse searchResponse = null;
           try {
               searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
           } catch (IOException e) {
               e.printStackTrace();
           }
           Aggregations aggregations = searchResponse.getAggregations();
           Terms byCompanyAggregation = aggregations.get("by_sex");
           Terms.Bucket elasticBucket = byCompanyAggregation.getBucketByKey("女性");
           Avg averageAge = elasticBucket.getAggregations().get("avg_age");
           double avg = averageAge.getValue();
           System.out.println("女性平均年龄:"+avg);
    }

    Stats统计

    public void stats(){
           RestHighLevelClient client = elasticClient.getRestHighLevelClient();
           SearchRequest searchRequest = new SearchRequest();
           SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
           TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_sex")
                   .field("sex.keyword");  //by_sex为聚合名称,名称可随意
           aggregation.subAggregation(AggregationBuilders.stats("stat_age")
                   .field("age"));  //avg_age 为子聚合名称,名称可随意
           searchSourceBuilder.aggregation(aggregation);
           searchRequest.source(searchSourceBuilder);
           SearchResponse searchResponse = null;
           try {
               searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
           } catch (IOException e) {
               e.printStackTrace();
           }
           Aggregations aggregations = searchResponse.getAggregations();
           Terms byCompanyAggregation = aggregations.get("by_sex");
           List<? extends Terms.Bucket> buckets = byCompanyAggregation.getBuckets();
           for (Terms.Bucket bucket : buckets) {
               Stats statAge = bucket.getAggregations().get("stat_age");
               System.out.println(bucket.getKey()); //获取分组名称
               System.out.println("平均值:"+statAge.getAvg());
               System.out.println("总数:"+statAge.getSum());
               System.out.println("最大值:"+statAge.getMaxAsString());
               System.out.println("最小值:"+statAge.getMin());
           }
       }
  • 相关阅读:
    PHP中simpleXML递归实现XML文件与数组的相互转化(原创)
    关于本地服务器localhost请求Forbidden解决办法
    PHP中XPATH 实现xml及html文件快速解析(附xml做小型数据库实现六级单词快速查询实例)
    win8忘记开机密码解决方法汇总
    HTML5的FileAPI实现文件的读取及超大文件的上传
    FormData实现form表单的数据打包
    Ajax_iframe文件上传
    深入浅出JSONP--解决ajax跨域问题
    2017ACM暑期多校联合训练
    2017ACM暑期多校联合训练
  • 原文地址:https://www.cnblogs.com/chentop/p/10313164.html
Copyright © 2020-2023  润新知