上代碼,結合上篇的SpringBoot集成ES以後,來完成一些索引的操做java
建立測試類,而後運行,經過Head插件觀察索引的狀況變動spring
package com.dance.danceesapi.test; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import java.io.IOException; /** * 關於索引的API的操做 */ @SpringBootTest public class TestIndex { @Autowired @Qualifier("restHighLevelClient") RestHighLevelClient restHighLevelClient; /** * 測試索引的建立 * @throws IOException */ @Test void createIndex() throws IOException { // 建立索引請求,並指定索引名稱 CreateIndexRequest flower = new CreateIndexRequest("flower"); // 執行請求 CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(flower, RequestOptions.DEFAULT); System.out.println(createIndexResponse); } /** * 測試索引是否存在 * @throws IOException */ @Test void existIndex() throws IOException { // 獲取索引請求,並指定索引名稱 GetIndexRequest flower = new GetIndexRequest("flower"); // 執行請求 boolean exists = restHighLevelClient.indices().exists(flower, RequestOptions.DEFAULT); System.out.println(exists); } /** * 測試索引的刪除 * @throws IOException */ @Test void deleteIndex() throws IOException { // 刪除索引請求,並指定索引名稱 DeleteIndexRequest flower = new DeleteIndexRequest("flower"); // 執行請求 AcknowledgedResponse delete = restHighLevelClient.indices().delete(flower, RequestOptions.DEFAULT); System.out.println(delete.isAcknowledged()); } }
做者:彼岸舞api
時間:2020\09\11網絡
內容關於:ElasticSearchelasticsearch
本文來源於網絡,只作技術分享,一律不負任何責任測試