使用Elasticsearch中的NativeSearchQuery查询数据时出现问题

bjg7j2ky  于 2023-01-25  发布在  ElasticSearch
关注(0)|答案(1)|浏览(956)

我是SpringElasticsearch的新手,尝试构建原生查询,但是,我得到了以下错误消息;
2023年1月23日21:54:56.127 + 03:00错误46803---[nio-8080-exec-1]操作员身份验证程序[.[.[/].[调度程序Servlet]:路径为[]的上下文中servlet [dispatcherServlet]的Servlet. service()引发异常[请求处理失败:java. lang.非法参数异常:未处理的查询实现
org. springframework. data.ElasticSearch.客户端. erhlc.本机搜索查询]及根本原因
我创建了两个"BoolQueryBuilder"对象,如下所示。我的目标是检索值"winlog. event_data. Status"等于0x18或0xC000006A且值"winlog. event_data. TargetUserName"为USER001的记录。

BoolQueryBuilder errorQuery = QueryBuilders.boolQuery()
                .should(QueryBuilders.matchQuery("winlog.event_data.Status","0x18"))
                .should(QueryBuilders.matchQuery("winlog.event_data.Status","0xC000006A"));

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("winlog.event_data.TargetUserName","USER001"))
                .must(errorQuery);

最后,当我按如下方式运行查询时,我得到了我在本主题开头提到的错误。

NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).build();

        System.out.println(searchQuery.getQuery());

        List<SearchHit<Winlog>> articles = elasticsearchOperations
                .search(searchQuery, Winlog.class, IndexCoordinates.of("winlog"))
                .getSearchHits();

但是如果我使用下面的输出打印查询,并直接在ElasticsearchRepository接口中使用此查询,则它可以正常工作,没有任何问题。

System.out.println(searchQuery.getQuery());

所以我不明白这个问题。

@Repository
public interface WinlogRepository  extends ElasticsearchRepository<Winlog,String> {
    @Query("{\n" +
            "  \"bool\" : {\n" +
            "    \"must\" : [\n" +
            "      {\n" +
            "        \"match\" : {\n" +
            "          \"winlog.event_data.TargetUserName\" : {\n" +
            "            \"query\" : \"?0\",\n" +
            "            \"operator\" : \"OR\",\n" +
            "            \"prefix_length\" : 0,\n" +
            "            \"max_expansions\" : 50,\n" +
            "            \"fuzzy_transpositions\" : true,\n" +
            "            \"lenient\" : false,\n" +
            "            \"zero_terms_query\" : \"NONE\",\n" +
            "            \"auto_generate_synonyms_phrase_query\" : true,\n" +
            "            \"boost\" : 1.0\n" +
            "          }\n" +
            "        }\n" +
            "      },\n" +
            "      {\n" +
            "        \"bool\" : {\n" +
            "          \"should\" : [\n" +
            "            {\n" +
            "              \"match\" : {\n" +
            "                \"winlog.event_data.Status\" : {\n" +
            "                  \"query\" : \"0x18\",\n" +
            "                  \"operator\" : \"OR\",\n" +
            "                  \"prefix_length\" : 0,\n" +
            "                  \"max_expansions\" : 50,\n" +
            "                  \"fuzzy_transpositions\" : true,\n" +
            "                  \"lenient\" : false,\n" +
            "                  \"zero_terms_query\" : \"NONE\",\n" +
            "                  \"auto_generate_synonyms_phrase_query\" : true,\n" +
            "                  \"boost\" : 1.0\n" +
            "                }\n" +
            "              }\n" +
            "            },\n" +
            "            {\n" +
            "              \"match\" : {\n" +
            "                \"winlog.event_data.Status\" : {\n" +
            "                  \"query\" : \"0xC000006A\",\n" +
            "                  \"operator\" : \"OR\",\n" +
            "                  \"prefix_length\" : 0,\n" +
            "                  \"max_expansions\" : 50,\n" +
            "                  \"fuzzy_transpositions\" : true,\n" +
            "                  \"lenient\" : false,\n" +
            "                  \"zero_terms_query\" : \"NONE\",\n" +
            "                  \"auto_generate_synonyms_phrase_query\" : true,\n" +
            "                  \"boost\" : 1.0\n" +
            "                }\n" +
            "              }\n" +
            "            }\n" +
            "          ],\n" +
            "          \"adjust_pure_negative\" : true,\n" +
            "          \"boost\" : 1.0\n" +
            "        }\n" +
            "      }\n" +
            "    ],\n" +
            "    \"adjust_pure_negative\" : true,\n" +
            "    \"boost\" : 1.0\n" +
            "  }\n" +
            "}\n")
        Page<Winlog> getAll(String name,Pageable pageable);

}

我不明白为什么我会在使用“NativeSearchQuery”时出错。
错误堆栈跟踪;

2023-01-23T21:54:56.127+03:00 ERROR 46803 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalArgumentException: unhandled Query implementation org.springframework.data.elasticsearch.client.erhlc.NativeSearchQuery] with root cause

java.lang.IllegalArgumentException: unhandled Query implementation org.springframework.data.elasticsearch.client.erhlc.NativeSearchQuery
    at org.springframework.data.elasticsearch.client.elc.RequestConverter.getQuery(RequestConverter.java:1488) ~[spring-data-elasticsearch-5.0.1.jar:5.0.1]
    at org.springframework.data.elasticsearch.client.elc.RequestConverter.searchRequest(RequestConverter.java:1059) ~[spring-data-elasticsearch-5.0.1.jar:5.0.1]
    at org.springframework.data.elasticsearch.client.elc.RequestConverter.searchRequest(RequestConverter.java:1041) ~[spring-data-elasticsearch-5.0.1.jar:5.0.1]
    at org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate.search(ElasticsearchTemplate.java:322) ~[spring-data-elasticsearch-5.0.1.jar:5.0.1]
    at com.test.lab.adldapels.controller.main.retrieve(main.java:51) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:207) ~[spring-web-6.0.4.jar:6.0.4]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:152) ~[spring-web-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1080) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:973) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:705) ~[tomcat-embed-core-10.1.5.jar:6.0]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:814) ~[tomcat-embed-core-10.1.5.jar:6.0]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:223) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-6.0.4.jar:6.0.4]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-6.0.4.jar:6.0.4]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-6.0.4.jar:6.0.4]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:119) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:400) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:859) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1734) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

. xml文件;

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test.lab.adldapels</groupId>
    <artifactId>adldap-els</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>adldap-els</name>
    <description>adldap-els</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.17.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>

                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
0sgqnhkj

0sgqnhkj1#

java. lang.非法参数异常:未经处理的查询实现组织。Spring框架。数据。ElasticSearch。客户端。erhlc。本机搜索查询在组织。Spring框架。数据。ElasticSearch。客户端。elc。请求转换器。getQuery(请求转换器。java:1488)~[Spring-数据-ElasticSearch-5. 0. 1. jar:5. 0. 1]
您使用的是Spring Boot 3和Spring Data Elasticsearch 5,后者使用了新的Elasticsearch客户端,为此您必须使用org.springframework.data.elasticsearch.client.elc.NativeQuery类。
NativeSearchQuery类来自旧的实现,该实现基于Elasticsearch中旧的已弃用RestHighLevelClient
请参阅文档:
在使用NativeSearchQuery类时,您需要切换到NativeQuery类,该类可以接受来自新的Elasticsearch客户端库的Query示例。

相关问题