我们正在开发弹簧启动弹性搜索应用程序。我们不能使用弹性搜索提供的Java API或Java Rest Client API。相反,我们需要使用spring rest模板进行弹性操作,但弹性似乎不接受来自其他客户端的索引请求。我们得到了“Not Acceptable”响应。如果有人给我们提供一些提示或信息,我真的很感激。
弹性版:5.6
答案 0 :(得分:1)
试试这个。它适用于使用HttpURLConnection通过HTTP API索引文档。
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();
使用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();