0

I am trying to do a post to elasticsearch API.

I need to do a multisearch, because I need to query in many indexes.

I am using Java8 + resttemplate to do it.

See the code below:

      StringBuilder sb = new StringBuilder();
        sb.append("{\"index\" : \"indexname\", \"type\": \"typename\"}");
        sb.append("{\"query\" : {\"match_all\" : {}}, \"from\" : 0, \"size\" : 10}");
        sb.append("{\"index\" : \"indexname\", \"type\": \"typename\"}");
        sb.append("{\"query\" : {\"match_all\" : {}}}\n");

        String query = sb.toString();

        String fullURL = "http://esHost/_msearch";

        log.debug("URL to search: {}.", fullURL);
        log.info(">>>>" + query);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-ndjson");

        HttpEntity<String> request = new HttpEntity<>(query, headers);

        Map map = restTemplate.postForObject(fullURL, request, Map.class);
...

When I do the same query to the same host using CURLS, it is ok, but using Java I can not. See the error:

{"error":{"root_cause":[{"type":"action_request_validation_exception","reason":"Validation Failed: 1: no requests added;"}],"type":"action_request_validation_exception","reason":"Validation Failed: 1: no requests added;"},"status":400}

I read that JSON needs \n, \r, I tried any possibilities but does not work.

How I build my "query value" to send for _msearch API?

javaTry
  • 1,085
  • 2
  • 18
  • 30
  • Possible duplicate of [Elasticsearch "no requests added" Bulk API Error](https://stackoverflow.com/questions/22996337/elasticsearch-no-requests-added-bulk-api-error) – Antoniossss May 02 '18 at 18:40

2 Answers2

0

I solved my problem using the follow code:

private static final String NEW_LINE = System.getProperty("line.separator");

        sb.append("{\"index\" : \"indexname\", \"type\": \"typename\"}").append(NEW_LINE)
        sb.append("{\"query\" : {\"match_all\" : {}}, \"from\" : 0, \"size\" : 10}").append(NEW_LINE)
        sb.append("{\"index\" : \"indexname\", \"type\": \"typename\"}").append(NEW_LINE)
        sb.append("{\"query\" : {\"match_all\" : {}}}\n").append(NEW_LINE);

In this case will work in any OS.

javaTry
  • 1,085
  • 2
  • 18
  • 30
0

This will be a problem on Windows systems, because the NDJSON format explicitly uses a '\n' character for the delimiter. On Windows, you will be looking for {'\r', '\n'} which will not be found unless you likely wrote it.

Jeffrey
  • 509
  • 4
  • 11