ES 关于索引的API操纵

上代码,结合上篇的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

本文来源于网络,只作技术分享,一律不负任何责任测试

相关文章
相关标签/搜索