Home > OS >  Issue with querying data with NativeSearchQuery from Elasticsearch
Issue with querying data with NativeSearchQuery from Elasticsearch

Time:01-25

I'm new to Spring Elasticsearch and trying to build native query, however, I get following error message ;

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

I created two "BoolQueryBuilder" objects like below, My goal is to retrieve records with value "winlog.event_data.Status" equal to 0x18 or 0xC000006A and value of "winlog.event_data.TargetUserName" is 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);

Finally, when I run the queries as follows, I get the error I mentioned at the beginning of the topic.

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

but if I print the query with the following output and use this query directly in the ElasticsearchRepository interface, it works without any problems.

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

so i couldn't understand the problem.

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

}

I couldn't understand why I'm getting error with "NativeSearchQuery".

stacktrace of error ;

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]

pom.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>

CodePudding user response:

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]

You are using Spring Boot 3 and so Spring Data Elasticsearch 5 which uses the new Elasticsearch client. You have to use the org.springframework.data.elasticsearch.client.elc.NativeQuery class for this.

The NativeSearchQuery class was from the old implementation that was based on the old deprecated RestHighLevelClient from Elasticsearch.

See the documentation:

When working with the NativeSearchQuery class, you’ll need to switch to the NativeQuery class, which can take a Query instance coming from the new Elasticsearch client libraries.

  • Related