问题
For my application I have to perform a custom count on elastic, I want to use the @Query annotation for this in the ElasticsearchCrudRepository we use. When I use the following signature:
@Query("CUSTOM BOOL QUERY HERE")
long countItemsCustom();
This leads to a java.lang.IllegalArgumentException: Expected 1 but found 30 results
Exception since it is executed as an query instead of an count. For spring-data-cassandra we have a special @CountQuery annotation to solve this issue. Is there a similar solution in spring-data-elasticsearch ?
I could use the elastic client or template to perform a custom query and get the results, but I prefer using the existing repository interface for this if possible.
回答1:
No, that's not possible at the moment. The ElasticsearchStringQuery
class checks the return type of the method that is annotated with the @Query
annotation and then executes the ElasticsearchOperations.queryFor...()
method that is appropriate for the method's return type. These are queries for data and not for the count.
I created an issue in the Spring Data Elasticsearch Jira to have this feature added.
来源:https://stackoverflow.com/questions/56259293/spring-data-elastic-search-repo-count-with-custom-query-annotation-not-working