elasticsearch自定义聚合改造 基于5.0.2

原创
2020/05/14 14:23
阅读数 125

 

 elasticsearch自定义聚合改造 基于5.0.2

可以解决 业务方面的特殊聚合要求

  HqewCustomsAggregationBuilder hqewCustomsAggregationBuilder = new 
  HqewCustomsAggregationBuilder("brands_group",null); 
  hqewCustomsAggregationBuilder.size(30); 
  hqewCustomsAggregationBuilder.field("pproductor.keyword"); 
  hqewCustomsAggregationBuilder.inField("userID"); esClient.prepareSearch("keyword_cache").addAggregation(hqewCustomsAggregationBuilder); 

SearchResponse searchResponse = searchRequest.execute().actionGet(); 
Map aggregationMap = searchResponse.getAggregations().getAsMap(); 
HqewCustoms aggregationaBrandGroup = (HqewCustoms) aggregationMap.get("brands_group"); 
  if(aggregationaBrandGroup!=null){     
        List<HqewCustoms.Bucket> buckets = aggregationaBrandGroup.getBuckets();        
        for (HqewCustoms.Bucket bucket : buckets) {           
          String key = bucket.getKeyAsString();            
           if (StringUtils.isNotBlank(key)){
                 BrandCount brandCount = new BrandCount();                  
                 brandCount.brand = key.trim().toUpperCase();                  
                 brandCount.count = bucket.getDocCount();                 
                 list.add(brandCount); 
        } 
      }
  }

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部