esClient.close();
}
//查询索引
public static void Index_Search() throws Exception{
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
);
// 查询索引
GetIndexRequest request = new GetIndexRequest(“user”);
GetIndexResponse getIndexResponse = esClient.indices().get(request, RequestOptions.DEFAULT);
//信息打印
System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());
//关闭ES客户端
esClient.close();
}
//删除索引
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();
}