我们正在Spring Boot中开发弹性搜索应用程序。我们不能使用弹性搜索提供的Java API或Java REST客户端API。取而代之的是,我们需要使用Spring Rest模板在弹性中进行操作,但是Elastic似乎并没有接受REST客户端的索引请求。我们获得了"不可接受的"响应。我真的很感激是否有人给我们一些提示或信息。
弹性版本:5.6
尝试一下。它对我使用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();