How can I use an annotation to do an aggragation like @Query(value = "{"query":""}") with spring-data-elasticsearch?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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);