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

导读:本篇文章讲解 Elasticsearch — 聚合查询,先分组后排序 ,分页,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

需求
对明细数据先按 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());
    

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/15561.html

(1)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!