es java 聚合查询

  1. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    for (String key : filter.keySet()) {
        if ("rootOrderNo".equals(key)) {
            boolQueryBuilder.must(QueryBuilders.termQuery("rootOrderNo.keyword", filter.get(key).toString()));
            continue;
        }
        if ("path".equals(key)) {
            boolQueryBuilder.must(QueryBuilders.termQuery("path", filter.get(key).toString()));
            continue;
        }
        if ("orderTypes".equals(key)) {
            JSONArray array = JSONArray.parseArray(filter.get(key).toString());
            List orderTypes = JSONObject.parseArray(array.toJSONString(), Integer.class);
            boolQueryBuilder.must(QueryBuilders.termsQuery("orderType", orderTypes));
            continue;
        }
        if ("orderNos".equals(key)) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("orderNo.keyword", (List) filter.get(key)));
            continue;
        }
        boolQueryBuilder.must(QueryBuilders.termQuery("deleteFlag", false));
    }
    //aggregation 聚合查询字段amount
    SumAggregationBuilder aggregation = AggregationBuilders.sum("amount").field("amount");
    //aggregation 聚合查询字段transactionAmount
    SumAggregationBuilder aggregation1 = AggregationBuilders.sum("transactionAmount").field("transactionAmount");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(boolQueryBuilder).explain(true).aggregation(aggregation).aggregation(aggregation1);
    SearchRequest searchRequest = new SearchRequest(ORDER_INDEX);
    searchRequest.source(searchSourceBuilder);
    SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    ParsedSum parsedSum = response.getAggregations().get("amount");
    ParsedSum tParsedSum = response.getAggregations().get("transactionAmount");
    Map map = new HashMap<>();
    map.put("amount", parsedSum.getValue());
    map.put("transactionAmount", tParsedSum.getValue());
    return Result.getInstance(map);

你可能感兴趣的:(Elasticsearch,es)