简体   繁体   中英

Spring-Data elastic search repo count with custom @Query annotation not working “Expected 1 but found 30 results”

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM