How can I use an annotation to do an aggragation like @Query(value = \"{\"query\":\"\"}\") with spring-data-elasticsearch?
You cannot do it with the @Query
annotation whose only purpose is to send a query, not aggregations.
The only way to achieve this with Spring Data Elasticsearch is to leverage NativeSearchQueryBuilder
and ElasticsearchTemplate
:
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchAll())
.withSearchType(COUNT)
.withIndices("your_index")
.withTypes("your_type")
.addAggregation(AggregationBuilders.terms("tags").field("tag"));
elasticsearchTemplate.queryForPage(searchQuery, YourEntity.class);