关于java API的学习都会在测试类中进行完成。

1.创建索引

  1. @SpringBootTest
  2. class EsApiApplicationTests {
  3. @Autowired
  4. private RestHighLevelClient restHighLevelClient;
  5. //索引的创建 request
  6. @Test
  7. void testCreateIndex() throws IOException {
  8. //1.创建索引请求
  9. CreateIndexRequest request = new CreateIndexRequest("kuan_index");
  10. //2.执行请求
  11. CreateIndexResponse createIndexResponse =
  12. restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
  13. System.out.println(createIndexResponse);
  14. }
  15. }

image.png

image.png

2.判断索引是否存在

  1. //判断索引是否存在
  2. @Test
  3. void testExistIndex()throws IOException{
  4. //1.创建索引请求
  5. GetIndexRequest request = new GetIndexRequest("kuan_index");
  6. //2.执行请求
  7. boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
  8. System.out.println(exists);
  9. }

image.png

3.删除索引

  1. //删除索引
  2. @Test
  3. void testDeleteIndex() throws IOException {
  4. DeleteIndexRequest request = new DeleteIndexRequest("kuan_index");
  5. //删除
  6. AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
  7. System.out.println(delete);
  8. }

image.png