Elasticsearch(二)集成Spring Boot 基本的API操作

目录

一、集成Spring Boot

 1、创建项目

2、pom文件 查看springboot集成的依赖

3、增加es的config类

二、索引相关API

1、创建索引

2、获取索引,判断其是否存在

3、删除索引

三、文档相关API

1、添加文档

2、获取文档,判断是否存在

3、获取文档信息

4、更新文档信息

5、删除文档信息

6、同文档批量导入数据

7、条件查询文档信息


一、集成Spring Boot

Java使用对应的rest风格调用ES是通过client依赖包进行操作的

配置需要的 maven 依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.17.24</version>
</dependency>

 1、创建项目

如果创建项目后拉取不到对应依赖,springboot 可以选用低一些的稳定版本例如 2.3.2.RELEASE 版本

2、pom文件 查看springboot集成的依赖

(当然也可自定义ES版本)

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

3、增加es的config类

package com.example.elasticsearch_springboot_demo.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http"),
                        new HttpHost("localhost", 9201, "http")));
        return client;
    }
}

二、索引相关API

1、创建索引

@SpringBootTest
class ElasticSearchSpringbootDemoApplicationTests {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    /**
     * 【索引相关】索引创建
     */
    @Test
    void testCreateIndex() throws IOException {
        // 1、创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("m_index");
        // 2、客户端执行请求 IndicesClient,执行创建请求 并获得响应结果
        CreateIndexResponse createIndexResponse = client.indices().create
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值