0

I want to use the following elastic search query in Java to search through all my documents in my index.

{
  "query": {
    "query_string": {
      "query": "my search input"
    }
  }
}

When I use CURL it works fine and i get results back, but when i try to use this query in Java, it complains and gives me the following exception:

co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [parsing_exception] unknown query [query]

This is what my code looks like in Java:

    @GetMapping("/search")
    public void searchResume(@RequestParam(name = "search") String query) {
        Query q = new StringQuery("""
                {
                  "query": {
                    "query_string": {
                      "query": "my search input"
                    }
                  }
                }
                """);
        System.out.println(operations.search(q, Resume.class));
    }

Does someone know why it works using CURL but does not work when using Java and spring-data-elasticsearch?

I tried googling a lot and even asked chatgpt for help but I can't seem to find out why it doesn't work. Thank you in advance.

2
  • I'm pretty sure the StringQuery object is already adding the {"query":...} wrapper when converted internally to the final http request. I think the fix is to simply omit the "query" so that your final string is: ``` { "query_string": {"query": "my search input"} } ``` Commented Dec 21, 2024 at 18:13
  • Thank you so much that fixed the issue! Commented Dec 21, 2024 at 18:26

0

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.