首页
学习
活动
专区
圈层
工具
发布

Java API 代码操作ElasticSearch7.6.1(1)

干货走一波,结合上篇的SpringBoot集成ES之后,来完成一些索引的操作

创建测试类,然后运行,通过Head插件观察索引的情况变更

代码语言:javascript
复制
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

本文来源于网络,只做技术分享,一概不负任何责任

下一篇
举报
领券