当前位置: 首页 > 知识库问答 >
问题:

如何从Springboot的QueryBuilder中删除Elasticsearch中match\u phrase\u前缀中的零项查询?

丁毅庵
2023-03-14

我试图在Springboot中实现Elasticsearch的match\u phrase\u前缀。我试图在Springboot中构建以下查询:-

{
"query": {
    "match_phrase_prefix": {
        "parent_question_text": {
            "query": "what is th"
        }
    }
}

}

我得到了以下代码:-

QueryBuilders.matchPhrasePrefixQuery("parent_question_text","what is th");

但是,当我运行代码时,它会抛出以下异常:-

 ElasticsearchStatusException[Elasticsearch exception [type=parsing_exception, reason=[match_phrase_prefix] query does not support [zero_terms_query]]]
    at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:176)
    at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1933)
    at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1910)
    at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1667)
    at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1624)
    at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1594)
    at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:1110)
    at com.extramarks.questionMSA.services.QuestionElasticsearchServiceImpl.questionSearchByIdAndText(QuestionElasticsearchServiceImpl.java:112)
    at com.extramarks.questionMSA.controller.QuestionElasticsearchController.questionSearchByIdAndText(QuestionElasticsearchController.java:52)
    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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1064)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:681)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:96)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at com.extramarks.questionMSA.filters.RequestCachingFilter.doFilterInternal(RequestCachingFilter.java:30)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1726)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:829)
    Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://34.100.181.31:9200], URI [/question/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"parsing_exception","reason":"[match_phrase_prefix] query does not support [zero_terms_query]","line":1,"col":124}],"type":"parsing_exception","reason":"[match_phrase_prefix] query does not support [zero_terms_query]","line":1,"col":124},"status":400}
        at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:326)
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:296)
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:270)
        at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1654)
        ... 55 more

使用以下代码生成的查询是:-

{
"match_phrase_prefix": {
    "parent_question_text": {
        "query": "what is the",
        "slop": 0,
        "max_expansions": 50,
        "zero_terms_query": "NONE",
        "boost": 1.0
    }
}

}

我厌倦了在QueryBuilder中将zero\u terms\u query设置为NONE、ALL、NULL,但仍然得到相同的异常。

共有1个答案

席兴朝
2023-03-14

正如我在评论中提到的,您可以将spring boot starter data elasticsearch与2.4.13版本一起使用,就像使用7.9.3版本的ElasticJava高级客户端一样。

如果您使用的是spring boot starter data elasticsearch,它使用的是高于7.10版本的Elastic高级客户端版本,那么您将面临这个问题。如本文所述,在Elasticsearch 7.10版本之后,作为匹配短语前缀支持零项查询。

更新

我在pom中添加了以下依赖性。我建议的xml文件:

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>2.4.13</version>
        </dependency>
    </dependencies>

下面是Java中的查询生成:

SearchRequest searchRequest = new SearchRequest("company");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();      
        sourceBuilder.query(QueryBuilders.matchPhrasePrefixQuery("field", "sagar"));
        System.out.println(sourceBuilder.toString());

Java生成的查询输出(nozero\u terms\u query参数)

{
    "query": {
        "match_phrase_prefix": {
            "field": {
                "query": "sagar",
                "slop": 0,
                "max_expansions": 50,
                "boost": 1.0
            }
        }
    }
}

 类似资料:
  • 问题内容: 我已经做过了,但是没有用。每当我访问它时,它都会转到。代码在这里给出: *我也添加了 *更新,但没有区别。另外,让我告诉您,我正在地址栏中输入地址,然后按Enter。我说的对吗?浏览器如何发现它不需要从服务器刷新? 更新#2 理想的情况是我想要的网址为和到。现在给出404 更新#3 我正在使用nginx代理,如果有帮助的话。 更新#4 擦除的缓存。现在我可以看到,而不是,但仍给404

  • 我正在尝试删除Spring Security中的“role_”前缀。我尝试的第一件事是: 是否可以从Spring Security的hasRole检查中删除ROLE_前缀?

  • 我有三个字段-一个是整数类型(field1),两个是十进制类型(field2,field3)。我希望能够按所有字段进行查询。在我的情况下,这些单独的查询非常有效: 这个查询运行良好: 但是,如果我将它们结合起来: ]; 我得到一个错误: 未捕获的异常'Elasticsearch\Common\Exceptions\BadRequest est400Exception'...[匹配]格式错误的查询,

  • 我使用了来删除前导零,但它只在android软键盘中工作,而不是我自定义的。是零按钮。基本上,只要我按下零按钮,我的应用程序就会崩溃。 我只需要零按钮在点击时不会返回任何值的代码,以避免前导零。下面是 获取以下错误:

  • 在我的脚本中,我有一个字符串及其前缀/后缀。我需要从原始字符串中删除前缀/后缀。 例如,假设我有以下值: 如何得到以下结果?

  • 如何更改pandas数据框的列标签: 到