关于java API的学习都会在测试类中进行完成。
1.创建索引
@SpringBootTestclass EsApiApplicationTests { @Autowired private RestHighLevelClient restHighLevelClient; //索引的创建 request @Test void testCreateIndex() throws IOException { //1.创建索引请求 CreateIndexRequest request = new CreateIndexRequest("kuan_index"); //2.执行请求 CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); System.out.println(createIndexResponse); }}

2.判断索引是否存在
//判断索引是否存在@Testvoid testExistIndex()throws IOException{ //1.创建索引请求 GetIndexRequest request = new GetIndexRequest("kuan_index"); //2.执行请求 boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists);}
3.删除索引
//删除索引@Testvoid testDeleteIndex() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("kuan_index"); //删除 AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete);}
