Reputation: 686
We are developing an elastic search application in spring boot. We cannot use the Java API or Java Rest Client API's provided by the elastic search. Instead, we need to do operations in elastic using spring rest template, but elastic doesn't seem to be accepting index requests from the rest client.We got "Not Acceptable" response back. I really appreciate if anyone gives us some hints or information.
elastic version: 5.6
Upvotes: 0
Views: 1319
Reputation: 1251
Try this. It works for me for Indexing Document through HTTP API using HttpURLConnection.
URL obj = new URL("http://localhost:9200/index/type");
String json = "{\n" +
" \"user\" : \"kimchy\",\n" +
" \"post_date\" : \"2009-11-15T14:12:12\",\n" +
" \"message\" : \"trying out Elasticsearch\"\n" +
"}";
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
osw.write(json);
osw.flush();
osw.close();
System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());
if (con != null)
con.disconnect();
Doing a simple search using HttpURLConnection.
URL obj = new URL("http://localhost:9200/index/type/_search");
String json = "{\n" +
" \"query\": {\n" +
" \"match_all\": {}\n" +
" }\n" +
"}";
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
osw.write(json);
osw.flush();
osw.close();
BufferedReader br = new BufferedReader(new InputStreamReader((con.getInputStream())));
System.out.println("Response : " + br.readLine());
System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());
if (con != null)
con.disconnect();
Upvotes: 1