How can i use a annotation to do an aggragation like @Query(value = “{”query“:”“}”)

后端 未结 1 478
无人及你
无人及你 2021-01-24 16:05

How can I use an annotation to do an aggragation like @Query(value = \"{\"query\":\"\"}\") with spring-data-elasticsearch?

相关标签:
1条回答
  • 2021-01-24 16:45

    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);
    
    0 讨论(0)
提交回复
热议问题