当前位置:   article > 正文

ES多个字段聚合分组,在结果上执行二次统计分析_es查询结果按字段分组查询

es查询结果按字段分组查询
  • Es版本 - 7.6.2
@Test
public void groupBucket() {

    //设置要查询的索引
    SearchRequest request = new SearchRequest().indices(EdaIndex.EDaIndex.FILE_DETECTION_LOGS.value());
    //构建搜索
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    //添加搜索长度
    sourceBuilder.size(0);

    //添加搜索条件
    BoolQueryBuilder bqb = new BoolQueryBuilder();
    bqb.must(QueryBuilders.termsQuery("recordId.keyword", "16ed281df9164ce9bca8b3eefcb7be3b",
            "7291bb3e94fc402390895525156917e5",
            "4f7c23e6e7bb4630bcc463cdb77f9333",
            "14f826ac3340434a9af26c0540418226",
            "04a6d0bc146b4f4198659776fffbdaaf",
            "8d37a7c04d604dcf9ad9b71488815d03"));
    sourceBuilder.query(bqb);
    
    String[] groupFields = {"qualified", "size", "fileType", "qualityScore", "errorType", "articulationScore", "recognitionRateScore", "emptyRateScore"};
    TermsAggregationBuilder tab = AggregationBuilders.terms(groupFields[0]).field(groupFields[0].concat(".keyword"));
    TermsAggregationBuilder tabTemp;
    TermsAggregationBuilder tabSub = tab;
    for (int i = 1; i < groupFields.length; i++) {
        tabTemp = AggregationBuilders.terms(groupFields[i]).field(groupFields[i].concat(".keyword"));
        tabSub.subAggregation(tabTemp);
        tabSub = tabTemp;
    }

    //添加聚合查询
    sourceBuilder.aggregation(tab);
    //创建请求
    request.source(sourceBuilder);
    //发送请求
    SearchResponse response = null;
    try {
        response = esClient.search(request, RequestOptions.DEFAULT);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    Aggregations aggregations = response.getAggregations();
    List<Map<String, Object>> list = new ArrayList<>();
    parseData(aggregations.getAsMap(), new HashMap<>(), list);
    for (Map<String, Object> stringObjectMap : list) {
        stringObjectMap.forEach((k, v) -> System.out.print(k + "=" + v + ", "));
        System.out.println("------------------------------------");
    }
}

/**
 * 解析数据
 * @param asMap 被解析的数据
 * @param cashMap 缓存节点数据
 * @param lists 最终结果
 */
private static void parseData(Map<String, Aggregation> asMap, Map<String, Object> cashMap, List<Map<String, Object>> lists) {
    for (String key : asMap.keySet()) {
        Map<String, Object> map = new LinkedHashMap<>();
        Terms terms = (Terms) asMap.get(key);
        String name = terms.getName();
        List<? extends Terms.Bucket> termsBuckets = terms.getBuckets();
        for (Terms.Bucket termsBucket : termsBuckets) {
            String keyAsString = termsBucket.getKeyAsString();
            map.put(name, keyAsString);
            map.putAll(cashMap);
            Map<String, Aggregation> childMap = termsBucket.getAggregations().asMap();
            if (MapUtils.isNotEmpty(childMap)) {
                parseData(childMap, map, lists);
            } else {
                long count = termsBucket.getDocCount();
                cashMap.put(name, keyAsString);
                cashMap.put("count", count);
                lists.add(cashMap);
            }
        }
    }
}
  • 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
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

拿走不谢!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/68135
推荐阅读
相关标签
  

闽ICP备14008679号