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

有些话、适合烂在心里 提交于 2021-01-29 19:56:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!