【问题标题】:Getting aggregated value using ElasticsearchTemplate and aggregators使用 ElasticsearchTemplate 和聚合器获取聚合值
【发布时间】:2018-07-10 12:27:24
【问题描述】:

我在提取聚合值时遇到问题。

配置是 spring-boot-starter-data-elasticsearch。

在数据库中多次索引的文档用户。

我想返回字段“cmetsCnt”的总和

@Autowired
ElasticsearchTemplate elasticsearchTemplate;

SearchQuery searchQuery = new NativeSearchQueryBuilder()
    .withIndices("comment")             
    .withQuery(matchQuery("user", userName))       
    .addAggregation(AggregationBuilders.sum("sum_of_comments").field("commentsCnt"))
    .build();

Aggregations aggregations = elasticsearchTemplate.query(searchQuery, 
        new ResultsExtractor<Aggregations>() {
            @Override
            public Aggregations extract(SearchResponse response) {
                return response.getAggregations();
        }
});

Aggregation ret = aggregations.get("sum_of_comments");

如何提取价值?也许有更好的方法?

【问题讨论】:

    标签: java spring-boot elasticsearch


    【解决方案1】:
        for (Aggregation aggs : aggregations) {
    
             Sum sum = (Sum) aggs;
             double sumValue = sum.getValue();
              System.out.println("sumValue=" + sumValue);
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-21
      • 2021-11-07
      • 2019-01-15
      • 2014-10-26
      • 2011-01-16
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多