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<Integer> 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<String>) 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<String, Object> map = new HashMap<>(); map.put("amount", parsedSum.getValue()); map.put("transactionAmount", tParsedSum.getValue()); return Result.getInstance(map);
es java 聚合查询
最新推荐文章于 2024-06-27 20:30:00 发布