1. 程式人生 > 實用技巧 >ES 關於索引的API操縱

ES 關於索引的API操縱

上程式碼,結合上篇的SpringBoot整合ES之後,來完成一些索引的操作

建立測試類,然後執行,通過Head外掛觀察索引的情況變更

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()); } }

作者:彼岸舞

時間:2020\09\11

內容關於:ElasticSearch

本文來源於網路,只做技術分享,一概不負任何責任