3

I am having problems extracting the aggregated value.

configuration is spring with spring-boot-starter-data-elasticsearch.

Document user indexed multiples times in database.

I want to return sum of fields 'commentsCnt'

@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");

How to extract the value? Maybe there is a better approach?

1 Answer 1

1
    for (Aggregation aggs : aggregations) {

         Sum sum = (Sum) aggs;
         double sumValue = sum.getValue();
          System.out.println("sumValue=" + sumValue);
  }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.