Elaticsearch Java client output dsl

I am using Elasticsearch java Client version 8.1.
I want to output dsl by searchrequest, but i don't find any function.

I look forward hearing your reply,thanks.

You want to print the JSON document which represents your search request, that's right?

There is this feature coming:

In the meantime, you could do something like this (not tested):

StringWriter writer = new StringWriter();
response.serialize(JacksonJsonProvider.provider().createGenerator(writer), new JacksonJsonpMapper());
return writer.toString();

I think this works for a request object as well.

2 Likes

thanks ,i will try it.

Github shows that the current commit is in pull request status, so I can't use it right now, right? Is there a currently available way?

I gave a workaround in my response.

Thanks your reply,I rephrase my problem.

When I use the ElaticsearchClient.Search function, I get some exceptions. At this point, I don't get the Respon object, so I can't get the DSL with Respon.

I hope I can output a DSL that will help me figure out the problem.

What is the full stacktrace?

full stacktrace like this:

2022-03-30 09:48:27.529 [http-nio-19098-exec-1] [traceId = ] [log] ERROR o.a.c.c.C.[.[.[/api/product].[dispatcherServlet]-Servlet.service() for servlet [dispatcherServlet] in context with path [/api/product] threw exception [Request processing failed; nested exception is co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [search_phase_execution_exception] all shards failed] with root cause 
co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [search_phase_execution_exception] all shards failed
	at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:281)
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:147)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1526)
	at com.hc.product.service.service.impl.ElasticSearchServiceImpl.searchByMatchAllOrBool(ElasticSearchServiceImpl.java:949)
	at com.hc.product.service.service.impl.ElasticSearchServiceImpl.searchFreeTravel(ElasticSearchServiceImpl.java:491)
	at com.hc.product.service.service.impl.ProductServiceImpl.searchProductByCondition(ProductServiceImpl.java:562)
	at com.hc.product.service.service.impl.ProductServiceImpl.searchProduct(ProductServiceImpl.java:112)
	at com.hc.product.service.rest.controller.ProductController.searchProduct(ProductController.java:53)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
	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.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
	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 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.lang.Thread.run(Thread.java:748)

Is there any logs in Elasticsearch?

What does the code looks like?

I've fixed the bug, the reason for the problem was that the sort operation was using a field of type text instead of a type of keyword.

But what I want to express is how can I output a DSL that helps me solve problems quickly.

Could you share the code which is calling this API?

ok,thanks. mock code like this:

ElasticsearchClient elasticClient =new ElasticsearchClient();
            SearchResponse<T> searchResponse =
                    elasticClient.search(s ->
                            s.index(ElasticsearchConstant.FREE_TRAVEL_PRODUCT_INDEX_NAME)
                                    .sort(generateSortOptions(sortType))
                                    .query(q -> q.bool(boolQuery))
                                    .from(skipSize).size(pageSize), clazz);

May be you could log the value of s ?
Or maybe there's a logger configuration to print this? @swallez do we have that?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.