★☆★☆★☆🐈★☆★☆★☆

ES操作索引

package cn.itcast.hotel;

import cn.itcast.hotel.constant.EsConstant;
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.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class HotelDemoApplicationTests {
    @Autowired
    private RestHighLevelClient client;
    @Test
    void contextLoads() throws IOException {
        client.close();
    }
    /**
     * 创建索引库
     */
    @Test
    void createIndexTest() {
        //1.创建Request对象
        CreateIndexRequest request = new CreateIndexRequest(EsConstant.HOTEL_INDEX);
        //2.指定映射。请求参数,EsConstant.HOTEL_MAPPING是静态常量字符串,内容是创建索引库的DSL语句
        request.source(EsConstant.HOTEL_MAPPING, XContentType.JSON);
        //3.创建索引
        try {
            CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
            String index = response.index();
            boolean acknowledged = response.isAcknowledged();
            System.out.println(index + ":" + acknowledged);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 查询索引 查询不到抛出[hotel] ElasticsearchStatusException[Elasticsearch exception [type=index_not_found_exception, reason=no such index [hotel]]
     * @Description: HotelDemoApplicationTests
     * @Author: regular
     * @Version: 1.0
    */
    @Test
    public void getIndexTest() throws IOException {
        GetIndexRequest getIndexRequest = new GetIndexRequest(EsConstant.HOTEL_INDEX);
        GetIndexResponse getIndexResponse = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(getIndexResponse);
    }
    /**
     * 判断索引库是否存在
     * @Description: HotelDemoApplicationTests
     * @Author: regular
     * @Version: 1.0
     */
    @Test
    public void existsIndexTest() throws IOException {
        GetIndexRequest getIndexRequest = new GetIndexRequest(EsConstant.HOTEL_INDEX);
        boolean flag = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(flag);
    }
    /**
     * 删除索引
     * @Description: HotelDemoApplicationTests
     * @Author: regular
     * @Version: 1.0
     */
    @Test
    public void deleteIndexTest() throws IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(EsConstant.HOTEL_INDEX);
        AcknowledgedResponse acknowledgedResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        System.out.println(acknowledgedResponse);
    }
}

 

posted @ 2023-09-16 20:35  qd372502  阅读(34)  评论(0)    收藏  举报