@Autowired
RestClientBuilder restClientBuilder;
@Test
void elasticSearchRestClientPut() {
Employee employee = new Employee(1, "xiaoming", "xiaoming@163.com");
RestClient restClient = restClientBuilder.build();
Request request = new Request("PUT", "/com1/employee/1");
request.setJsonEntity(JSON.toJSONString(employee));
Response response = null;
try {
response = restClient.performRequest(request);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Test
void elasticSearchRestClientGet(){
RestClient restClient = restClientBuilder.build();
Request request = new Request("GET", "/com1/_search");
String query = "{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"name\" : \"xiao\"\n" +
" }\n" +
" }\n" +
"}";
request.setJsonEntity(query);
Response response = null;
try {
response = restClient.performRequest(request);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Test
void elasticSearchRestClientHead(){
RestClient restClient = restClientBuilder.build();
Request request = new Request("HEAD", "/com1/employee/1");
Response response = null;
try {
response = restClient.performRequest(request);
System.out.println(response);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Test
void elasticSearchRestClientDelete(){
RestClient restClient = restClientBuilder.build();
Request request = new Request("DELETE", "/com1/employee/3");
Response response = null;
try {
response = restClient.performRequest(request);
System.out.println(response);
} catch (IOException e) {
throw new RuntimeException(e);
}
}