SpringBoot集成ElasticSearch 7

本文基于Java High Level REST Client 7.12

一、引入依赖

<!-- elasticsearch版本,保持与本地版本一致 -->
<elasticsearch.version>7.12.1</elasticsearch.version>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

二、配置 RestHighLevelClient

/**
 * 配置 RestHighLevelClient
 */
@Configuration
public class ElasticSearchConfig {
   
    @Bean
    public RestHighLevelClient restHighLevelClient() {
   
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
        return client;
    }
}

可以去配置文件中直接设置

三、实例

本文没有使用 ElasticSearchTemplate、ElasticSearchRestTemplate

索引操作

@Autowired
private RestHighLevelClient restHighLevelClient;

/**
* 创建索引
*/
@Test
public void createIndex() throws IOException {
   
    // 创建索引请求
    CreateIndexRequest indexRequest = new CreateIndexRequest("test4");
    // 客户端执行请求,请求后获得响应
    CreateIndexResponse indexResponse = restHighLevelClient.indices().create(indexRequest, RequestOptions.DEFAULT);
        System.out.println(indexResponse.index()); //输出:test4
    }

/**
 * 获取索引,只能判断索引存不存在
 */
@Test
public void existIndex() throws IOException {
   
    // 创建获取索引请求
    GetIndexRequest request = new GetIndexRequest("test4");
    boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
    System.out.println(exists);
}

/**
 * 删除索引
 */
@Test
public void deleteIndexTest() throws IOException {
   
    DeleteIndexR
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值