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