0

I m tring to use the matchQuery of java api elasticsearch but the curl return 0 documents.

Here my code: webservice:

@RequestMapping(path = "/blog/search/{text}", method = RequestMethod.GET)
public List<PostDocument> search(
    @PathVariable String text, 
    @RequestParam("from") int from, 
    @RequestParam("size") int size
) {
    return this.searchService.search(text, from, size);
}

searchService.search:

public List<PostDocument> search(String fulltextQuery, int from, int size) {
    SearchRequestBuilder searchRequestBuilder = this.client.prepareSearch(indexConfig.getIndexAlias());
    System.out.println(fulltextQuery);
    searchRequestBuilder.setQuery(this.createQuery(fulltextQuery));
    searchRequestBuilder.setFrom(from);
    searchRequestBuilder.setSize(size);

    SearchResponse response = searchRequestBuilder.get();

    return this.parseResponse(response);
}

private QueryBuilder createQuery(String fulltextQuery) {
    return QueryBuilders.matchQuery("title",fulltextQuery);
}

my curl using postman

localhost:8080/blog/search/post?from=1&size=10

result:

[]

Someone help me please, i have an error in my code or in my curl.

I am sure that i have data in my elasticsearch

curl -XGET "localhost:9200/blog/_search?pretty" -d '{"query" : {"match" : {"title" : "blog"}}}'
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.25811607,
    "hits" : [
      {
        "_index" : "blog",
        "_type" : "post",
        "_id" : "1",
        "_score" : 0.25811607,
        "_source" : {
          "title" : "Blog post",
          "content" : "My super blog post"
        }
      }
    ]
  }
}

I printed the response, it return that he find a 2 docs but the list of hits is empty

{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":2,"max_score":0.96669346,"hits":[]}}

Here the console log after setting from = 0

2017-04-26 09:23:48.756 ERROR 26327 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at fr.oss.es.devbasics.search.blog.controller.SearchService.parseResponse2(SearchService.java:115) ~[classes/:na]
    at fr.oss.es.devbasics.search.blog.controller.SearchService.searchtest(SearchService.java:43) ~[classes/:na]
    at fr.oss.es.devbasics.search.blog.controller.SearchController.search(SearchController.java:30) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]

Here my code: parseResponse2

private List<PostDocument> parseResponse2(SearchResponse response) {
        List<PostDocument> docs = new ArrayList<PostDocument>();

        SearchHit[] hits = response.getHits().getHits();

        for(SearchHit hit: hits) {
           Post post = new Post(hit.sourceAsMap().get("title").toString(), hit.sourceAsMap().get("content").toString());
           post.setId(Long.parseLong(hit.sourceAsMap().get("id").toString()));
           docs.add(new PostDocument(hit.index(), post));
        }

        return docs;
    }

Regards

4
  • Can you print out the query that's being generated by your code as well? Commented Apr 26, 2017 at 4:22
  • stackoverflow.com/questions/43601775/… probably this might help you :) and I guess answer from @Val will help you even further :) Commented Apr 26, 2017 at 5:00
  • Here the query: { "multi_match" : { "query" : "post", "fields" : [ "content^1.0", "title^1.0" ], "type" : "best_fields", "operator" : "OR", "slop" : 0, "prefix_length" : 0, "max_expansions" : 50, "lenient" : false, "zero_terms_query" : "NONE", "boost" : 1.0 } } Commented Apr 26, 2017 at 5:48
  • How come your query contains a multi_match while your Java code uses match? Are you sure this is the query being generated? Commented Apr 26, 2017 at 6:43

2 Answers 2

1

The problem is that you have one document, and from should start from 0 but you input 1 instead.

Try this URL and it should work:

localhost:8080/blog/search/post?from=0&size=10
                                     ^
                                     |
                                change this
Sign up to request clarification or add additional context in comments.

10 Comments

I get this error { "timestamp": 1493191428761, "status": 500, "error": "Internal Server Error", "exception": "java.lang.NullPointerException", "message": "No message available", "path": "/blog/search/post" }
Can you also share your ES server logs around that time?
Ok, then the issue is in your code. Do you have more info about that NullPointerException from your application log?
According to your stack trace, parseResponse2() is throwing that null pointer exception. Do you mind to show the code? (update your question please)
private List<PostDocument> parseResponse2(SearchResponse response) { List<PostDocument> docs = new ArrayList<PostDocument>(); SearchHit[] hits = response.getHits().getHits(); for(SearchHit hit: hits) { Post post = new Post(hit.sourceAsMap().get("title").toString(), hit.sourceAsMap().get("content").toString()); post.setId(Long.parseLong(hit.sourceAsMap().get("id").toString())); docs.add(new PostDocument(hit.index(), post)); } return docs; }
|
0

try with localhost:8080/blog/post/_search/?from=1&size=10

Try with hardcoding index name and type name

JAVA API code :-

QueryBuilder searchQuery = QueryBuilders.matchQuery("title", "");

SearchRequestBuilder countRequestBuilder = esClient.prepareSearch("INDEXNAME").setTypes("TYPE").setQuery(searchQuery);
SearchResponse response = countRequestBuilder.execute().actionGet("ACTION_TIMEOUT"), TimeUnit.SECONDS);

SearchHit[] seacrhHits = response.getHits().getHits();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.