ES RestHighLevelClient 简单使用

本文将介绍如何在SpringBoot项目中集成Elasticsearch的RestHighLevelClient,包括新建项目、添加依赖、配置YAML文件、编写ES配置类以及使用JUnit5进行测试的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 新建springboot项目,引入依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2. 配置 yaml

elasticsearch:
  host: 112.124.7.63
  port: 8092
  index-name: index-test

3. 编写 ES配置类

@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
public class RestClientConfig extends AbstractElasticsearchConfiguration {

    private String host;
    private Integer port;

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        return new RestHighLevelClient(RestClient.builder(
            new HttpHost(host,port)
        ));
    }
}

4. 使用 junit5 测试

@SpringBootTest
public class RestClientTest {

    @Value("${elasticsearch.index-name}")
    private String indexName;

    @Autowired
    private RestHighLevelClient client;

	/**
     * 判断索引是否存在
     * @throws IOException
     */
    @Test
    void getIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest(indexName);

        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    /**
     * 删除指定索引
     * @throws IOException
     */
    @Test
    void delete() throws IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName+"-"+20210812);
        AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        System.out.println(delete);
    }
	
    /**
     * 单个插入
     * @throws IOException
     */
    @Test
    void test() throws IOException {
        IndexRequest indexRequest = new IndexRequest(indexName);
        indexRequest.source(XContentType.JSON,
            "1","1",
            "2","2"
            );
        IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(index);
    }

    /**
     * 批量插入数据
     * @throws IOException
     */
    @Test
    void test() throws IOException {
        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest(indexName)
            .source(XContentType.JSON,"1","1","2","2","3","3","4","4"));
        request.add(new IndexRequest(indexName)
            .source(XContentType.JSON,"11","11","22","22","33","33","44","44"));

        request.add(new IndexRequest(indexName)
            .source(XContentType.JSON,"111","111","222","222","333","333","444","444"));

        BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println(bulk);
    }

    /**
     * 将指定的索引数据备份到另一个索引
     * @throws IOException
     */
    @Test
    void reindex() throws IOException {
        ReindexRequest reindexRequest = new ReindexRequest();
        reindexRequest.setSourceIndices(indexName");
        reindexRequest.setDestIndex(indexName+"20210811");
        BulkByScrollResponse reindex = client.reindex(reindexRequest, RequestOptions.DEFAULT);  
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值