java 如何从Springboot的QueryBuilder中删除Elasticsearch中match_phrase_prefix中的zero_terms_query?

lp0sw83n  于 2023-03-06  发布在  Java
关注(0)|答案(2)|浏览(296)

我尝试在Springboot中实现Elasticsearch的match_phrase_prefix。我尝试在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_terms_query设置为NONE、ALL、NULL,但仍然得到相同的异常。

ztigrdn8

ztigrdn81#

正如我在评论中提到的,您可以使用spring-boot-starter-data-elasticsearch2.4.13版本,因为它是使用弹性Java高级客户端与7.9.3版本。
如果您使用的spring-boot-starter-data-elasticsearch使用的是高于7.10版本的Elastic高级客户端版本,那么您将面临这个问题。由于match_phrase_prefix支持Elasticsearch 7.10版本后的zero_terms_query,如前所述here

    • 更新**

我已经按照我的建议在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生成的查询输出(无zero_terms_query参数)

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

roejwanj2#

对于那些正在处理现有代码库并且不能修改弹性版本的人来说,这里有一些技巧来实现弹性版本〈7.10的match_phrase_prefix查询
我们可以使用phrase_prefix类型实现MultimatchQuery,并且只在单个字段上运行

MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder(query,fields);
multiMatchQueryBuilder.type(MultiMatchQueryBuilder.Type.PHRASE_PREFIX);

相关问题