/**
* 通过http直接查询es
* @throws IOException
*/
public JSONObject query(RequestMethod method , String url , String jsonStr) throws IOException {
Request request = new Request(method.toString(),url);
request.setJsonEntity(jsonStr);
Response response = restHighLevelClient.getLowLevelClient().performRequest(request);
HttpEntity entity = response.getEntity();
String responses = EntityUtils.toString(entity,"utf-8");
return JSONObject.parseObject(responses);
}
/**
* 样例
* @throws IOException
*/
public void methodTest() throws IOException {
String jsonStr = "{\n" +
" \"query\": {\n" +
" \"match_all\": {}\n" +
" }\n" +
"}";
JSONObject reponse = this.query(RequestMethod.GET,"/indexname/_search" , jsonStr);
System.out.println(reponse);
}
个人感觉通过json字符串访问es,可读性好点。当然直接通过API方法访问es也是十分推荐的。