//删除索引
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();
}
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
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,