带有自定义@Query 注释的 Spring-Data 弹性搜索存储库计数不起作用“预期为 1,但发现 30 个结果"; [英] Spring-Data elastic search repo count with custom @Query annotation not working "Expected 1 but found 30 results"

查看:19
本文介绍了带有自定义@Query 注释的 Spring-Data 弹性搜索存储库计数不起作用“预期为 1,但发现 30 个结果";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的应用程序,我必须对弹性执行自定义计数,我想在我们使用的 ElasticsearchCrudRepository 中为此使用 @Query 注释.当我使用以下签名时:

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();

这会导致 java.lang.IllegalArgumentException: Expected 1 but found 30 results 异常,因为它是作为查询而不是计数执行的.对于 spring-data-cassandra,我们有一个特殊的 @CountQuery 注释来解决这个问题.spring-data-elasticsearch 有没有类似的解决方案?

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.

推荐答案

不,目前不可能.ElasticsearchStringQuery 类检查使用 @Query 注解的方法的返回类型,然后执行 ElasticsearchOperations.queryFor...() 适用于方法返回类型的方法.这些是针对数据的查询,而不是针对计数的查询.

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.

我在 Spring Data Elasticsearch Jira 中创建了一个问题来添加此功能.

I created an issue in the Spring Data Elasticsearch Jira to have this feature added.

这篇关于带有自定义@Query 注释的 Spring-Data 弹性搜索存储库计数不起作用“预期为 1,但发现 30 个结果";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆