春季数据Elasticsearch:classcastException在内部*存储库方法中投掷

发布于 2025-01-28 13:33:26 字数 6777 浏览 3 评论 0原文

我在ES中有这个微不足道的索引:

{
  "dynamic": "strict",
  "properties": {
    "_class": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword"
        }
      }
    },
    "id": {
      "type": "keyword"
    },
    "reviewRequestDocumentId": {
      "type": "keyword"
    },
    "productId": {
      "type": "keyword"
    }
  }
}

要使用它,我使用弹簧数据弹性搜索,因此创建了一个实体和绑定的存储库:

@Data
@Builder(toBuilder = true)
@Document(indexName = "order_line", createIndex = false)
public class OrderLineDocument {

    @Id
    private String id;

    @NotNull
    private String reviewRequestDocumentId;

    @NotNull
    private String productId;
}

public interface OrderLineRepository extends ElasticsearchRepository<OrderLineDocument, String> {

    boolean existsByReviewRequestDocumentIdAndProductId(String reviewRequestDocumentId, String productId);

}

我面临的问题与存储库方法有关existsByReviewReviewRequeStDocumentIdandAndProductId()>从测试方法中调用它:

@Test
void existsByReviewRequestDocumentIdAndProductId() {
    String rrdId = UUID.randomUUID().toString();
    String productId = UUID.randomUUID().toString();

    OrderLineDocument orderLine = OrderLineDocument.builder()
            .productId(productId)
            .reviewRequestDocumentId(rrdId)
            .build();

    String id = repository.save(orderLine).getId();

    assertThat(repository.findById(id)).isNotEmpty();

    assertThat(repository.existsByReviewRequestDocumentIdAndProductId(rrdId, productId)).isTrue();
}

调用它会带有此堆栈跟踪的ClassCastException:

java.lang.ClassCastException: class com.yotpo.review.requests.dashboard.core.documents.orderline.OrderLineDocument cannot be cast to class java.lang.Boolean (com.yotpo.review.requests.dashboard.core.documents.orderline.OrderLineDocument is in unnamed module of loader 'app'; java.lang.Boolean is in module java.base of loader 'bootstrap')

    at com.sun.proxy.$Proxy140.existsByReviewRequestDocumentIdAndProductId(Unknown Source)
    at com.yotpo.review.requests.dashboard.componenttest.OrderLineRepositoryTest.existsByReviewRequestDocumentIdAndProductId(OrderLineRepositoryTest.java:67)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
    at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)

看起来该方法从ES查询整个实体,而不是获取简单的boolean

有人知道这样的问题吗?是一个错误还是我对某些内容进行了错误配置?

PS我也尝试过

public interface OrderLineRepository extends ElasticsearchRepository<OrderLineDocument, String> {

    boolean existsOrderLineDocumentByReviewRequestDocumentIdAndProductId(String reviewRequestDocumentId, String productId);

}

同样的例外。

I've got this trivial index in ES:

{
  "dynamic": "strict",
  "properties": {
    "_class": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword"
        }
      }
    },
    "id": {
      "type": "keyword"
    },
    "reviewRequestDocumentId": {
      "type": "keyword"
    },
    "productId": {
      "type": "keyword"
    }
  }
}

To work with it I use Spring Data Elasticsearch, so an entity and bound repository are created:

@Data
@Builder(toBuilder = true)
@Document(indexName = "order_line", createIndex = false)
public class OrderLineDocument {

    @Id
    private String id;

    @NotNull
    private String reviewRequestDocumentId;

    @NotNull
    private String productId;
}

public interface OrderLineRepository extends ElasticsearchRepository<OrderLineDocument, String> {

    boolean existsByReviewRequestDocumentIdAndProductId(String reviewRequestDocumentId, String productId);

}

The problem I'm facing is related to repository method existsByReviewRequestDocumentIdAndProductId() which is called from test method:

@Test
void existsByReviewRequestDocumentIdAndProductId() {
    String rrdId = UUID.randomUUID().toString();
    String productId = UUID.randomUUID().toString();

    OrderLineDocument orderLine = OrderLineDocument.builder()
            .productId(productId)
            .reviewRequestDocumentId(rrdId)
            .build();

    String id = repository.save(orderLine).getId();

    assertThat(repository.findById(id)).isNotEmpty();

    assertThat(repository.existsByReviewRequestDocumentIdAndProductId(rrdId, productId)).isTrue();
}

Calling it results in ClassCastException with this stack trace:

java.lang.ClassCastException: class com.yotpo.review.requests.dashboard.core.documents.orderline.OrderLineDocument cannot be cast to class java.lang.Boolean (com.yotpo.review.requests.dashboard.core.documents.orderline.OrderLineDocument is in unnamed module of loader 'app'; java.lang.Boolean is in module java.base of loader 'bootstrap')

    at com.sun.proxy.$Proxy140.existsByReviewRequestDocumentIdAndProductId(Unknown Source)
    at com.yotpo.review.requests.dashboard.componenttest.OrderLineRepositoryTest.existsByReviewRequestDocumentIdAndProductId(OrderLineRepositoryTest.java:67)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
    at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)

It looks like the method queries the whole entity from ES instead of fetching simple boolean.

Does anyone aware of such issues? Is it a bug or have I misconfigured something?

P.S. I've tried also

public interface OrderLineRepository extends ElasticsearchRepository<OrderLineDocument, String> {

    boolean existsOrderLineDocumentByReviewRequestDocumentIdAndProductId(String reviewRequestDocumentId, String productId);

}

same exception is thrown.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间海 2025-02-04 13:33:26

这是一个错误,目前未考虑它是A 的事实,并且由于返回类型否collection执行了参数的正常查询第一个结果返回。这导致您看到的错误。

当解决方法更改您的方法以进行计数时:

Long countOrderLineDocumentByReviewRequestDocumentIdAndProductId(String reviewRequestDocumentId, String productId);

并检查结果是否大于零。

感谢您报告这个问题

This is a bug, currently the fact that it is a exists method is not taken into account, and as the return type is no Collection a normal query for the parameters is executed and the first result returned. Which leads to the error you see.

As a workaround change your method to do a count instead:

Long countOrderLineDocumentByReviewRequestDocumentIdAndProductId(String reviewRequestDocumentId, String productId);

and check if the result is greater than zero.

Thank you for reporting this issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文