Elasticsearch — 聚合查询,先分组后排序 ,分页

需求
对明细数据先按 waybillId 分组,再按 eventTime 降序,取最新一条数据。

桶聚合(bucket)
Elasticsearch 桶聚合,目的就是数据分组,先将数据按指定的条件分成多个组,然后对每一个组进行统计。

  1. ES SQL 写法

     {
       "query": {
         "bool": {
           "must": [
             {
               "match_all": {}
             }
           ]
         }
       },
       "aggs": {
         "waybillIdAgg": {
           "terms": {
             "field": "waybillId",
             "size": 1000,
             "min_doc_count": 1
           },
           "aggs": {
             "top1": {
               "top_hits": {
                 "size": 1,
                 "sort": [
                   {
                     "eventTime": {
                       "order": "desc"
                     }
                   }
                 ]
               }
             }
           }
         }
       }
     }
    
  2. Java Elasticsearch 写法及结果解析

     // 查询条件
     BoolQueryBuilder queryBool = QueryBuilders.boolQuery();
    
     BoolQueryBuilder inFilter = new BoolQueryBuilder();
     waybillIds.forEach(
         waybillId -> inFilter.should(QueryBuilders.termQuery("waybillId", waybillId)));
     queryBool.must(inFilter);
    
     // 桶聚合(bucket),按waybillId分组
     TermsAggregationBuilder termsAggregationBuilder =
         AggregationBuilders.terms("waybillIdAgg").field("waybillId").size(1000).minDocCount(1);
     // 嵌套桶,再按时间倒序取第一条数据
     TopHitsAggregationBuilder sort =
         AggregationBuilders.topHits("top1").size(1).sort("eventTime", SortOrder.DESC);
     termsAggregationBuilder.subAggregation(sort);
     // 分页
         SearchSourceBuilder searchSourceBuilder =
             SearchSourceBuilder.searchSource().query(queryBool).aggregation(termsAggregationBuilder)
          	.from(pageIndex <= 1 ? 0 : ((pageIndex - 1) * pageSize)).size(pageSize);
         // 查询请求
     SearchRequest searchRequest = new SearchRequest(esIndexConfig.getIndexNameTrackingDetail());
     searchRequest.source(searchSourceBuilder);
    
     // 执行查询
     SearchResponse searchResponse =
         restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
         
     // 处理聚合查询结果
     Aggregations aggregations = searchResponse.getAggregations();
     Terms terms = aggregations.get("waybillIdAgg");
     List<TrackingDetail> trackingDetails =
         terms.getBuckets().stream()
             .map(
                 t -> {
                   Aggregation top1 = t.getAggregations().get("top1");
                   Optional<SearchHit> first =
                       Arrays.stream(((ParsedTopHits) top1).getHits().getHits()).findFirst();
                   if (first.isPresent()) {
                     Map<String, Object> trackingDetailMap = first.get().getSourceAsMap();
                     return BeanUtil.fillBeanWithMap(trackingDetailMap, new TrackingDetail(), false);
                   }
                   return null;
                 })
             .collect(Collectors.toList());
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值