//删除索引
public static void Index_Delete() throws Exception{
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
);
// 查询索引
DeleteIndexRequest request = new DeleteIndexRequest(“user”);
AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);
//响应状态
System.out.println(response.isAcknowledged());
//关闭ES客户端
esClient.close();
}
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 插入数据
IndexRequest request = new IndexRequest();
request.index(“user”).id(“1001”);
User user = new User();
user.setName(“zhangsan”);
user.setAge(30);
user.setSex(“男”);
// 向ES插入数据,必须将数据转换位JSON格式
ObjectMapper mapper = new ObjectMapper();
String userJson = mapper.writeValueAsString(user);
request.source(userJson, XContentType.JSON);
IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
esClient.close();
}
批量插入数据:
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 批量插入数据
BulkRequest request = new BulkRequest();
request.add(new IndexRequest().index(“user”).id(“1001”).source(XContentType.JSON, “name”, “zhangsan”, “age”,30,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1002”).source(XContentType.JSON, “name”, “lisi”, “age”,30,“sex”,“女”));
request.add(new IndexRequest().index(“user”).id(“1003”).source(XContentType.JSON, “name”, “wangwu”, “age”,40,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1004”).source(XContentType.JSON, “name”, “wangwu1”, “age