Springboot整合ElasticSearch7.6.x

本文详细介绍了如何将ElasticSearch 7.6.x与Spring Boot进行整合,包括导入依赖、配置类编写、使用高级ES客户端和Spring Data ES的CRUD操作。还展示了实战例子,创建了一个简单的搜索引擎,涵盖了数据爬取、存储和前端展示。最后提供了源码链接。
摘要由CSDN通过智能技术生成

前言

关于ElasticSearch的安装,原理和基本使用可以参考之前写的文章。
ElasticSearch7.6.x 学习笔记
接下来是与springboot的整合。

导入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.54</version>
        </dependency>

注意版本要和你安装的es版本一致

    <properties>
        <!--        统一版本-->
        <elasticsearch.version>7.6.1</elasticsearch.version>
    </properties>

编写配置类

EsClientConfig

/**
 * @author CHEN
 * @date 2020/10/31  18:37
 * 原生elasticsearch配置类
 */
@Configuration
@EnableElasticsearchRepositories
public class EsClientConfig {

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

调用es的API

高级es客户端

注入之前配置的高级客户端对象

 @Autowired
  RestHighLevelClient restHighLevelClient;

创建索引

void testCreateIndex() throws IOException {
        //1. 创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("test_index");
        //2. 客户端执行创建请求 IndicesClient 请求后获得响应
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);

        System.out.println(createIndexResponse);
    }

判断索引是否存在

 void testExists() throws IOException {
        GetIndexRequest request = new GetIndexRequest("test_index");
        boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

删除索引

 void testDeleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("test_index");
        //删除
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }

创建文档

 void testAddDocument() throws IOException {
        //创建对象
        Student student = new Student("test", 3);
        //创建请求
        IndexRequest request = new IndexRequest("test_index");
        //规则 put /test_index/_doc/1
        request.id("1");
        request.timeout(TimeValue.timeValueSeconds(1));
        request.timeout("1s");
        //将我们的数据放入请求 json
        request.source(JSON.toJSONString(student), XContentType.JSON);
        //客户端发送请求,获取响应的结果
        IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.out.println(indexResponse.toString());
        //对应我们命令返回的状态 CREATED
        System.out.println(indexResponse.status());
    }

获取文档,判断是否存在

 void testIsExists() throws IOException {
        GetRequest request = new GetRequest("test_index", "1");
        //不获取返回的 _source 的上下文了
        request.fetchSourceContext(new FetchSourceContext(false));
        request.storedFields("_none_");

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

获得文档的信息

void testGetDocument() throws IOException {
        GetRequest request = new GetRequest("test_index", "1");
        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        //打印文档的内容
        System.out.println(response.getSourceAsString());
        //返回的全部内容和命令是一样的
        System.out.println(response);
    }

更新文档的信息

 void testUpdateDocument() throws IOException {
        UpdateRequest request = new UpdateRequest("test_index", "1");
        request.timeout("1s");
        Student student = new Student("test", 3);
        request.doc(JSON.toJSONString(student), XContentType.JSON);
        UpdateResponse updateResponse = restHighLevelClient.update(request, RequestOptions.DEFAULT);
        System.out.println(updateResponse.status());
    }

删除文档的记录

void testDeleteDocument() throws IOException {
        DeleteRequest request = new DeleteRequest("test_index", "1");
        request.timeout("1s");

        DeleteResponse
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值