Spring boot 2.3.12集成ElasticSearch7.6.2并进行CRUD

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介: Spring boot 2.3.12集成ElasticSearch7.6.2并进行CRUD

Spring boot 2.3.12集成ElasticSearch7.6.2并进行CRUD

前言

本篇博客主要讲解Spring boot 2.3.12集成ElasticSearch7.6.2并进行CRUD操作。其它版本的spring boot集成ElasticSearch类似,只需要具体各自的版本是否匹配。通过本篇博客能够成功集成ElasticSearch并进行CRUD操作,适合刚接触ElasticSearch需要进行简单CRUD操作的读者。

ElasticSearch与Mysql的对应关系

在集成ElasticSearch之前需要明确一下ElasticSearch与Mysql的对应关系看,更便于之后对ElasticSearch的CRUD。

ElasticSearch Mysql
索引库(indices) Database 数据库
类型(type) Table 数据表
文档(Document) Row 行
域字段(Field) Columns 列
映射配置(mappings) 每个列的约束(类型、长度)

ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型) ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型) ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型)ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型)

ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型)

Spring boot 集成 ElasticSearch

确定集成的版本号

Spring boot集成ElasticSearch首先需要确定各自的版本号,如果各自版本号不匹配会出现版本不兼容问题,以及对应功能使用不了。

查询spring boot 匹配ElasticSearch的版本:版本匹配查询




spring boot项目中添加依赖

在pom.xml文件中添加以下内容:

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

初始化

一个RestHighLevelClient实例需要一个REST底层客户端构建器

咱们新建一个配置类:ElasticSearchConfig

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;
/**
 * @author : [WangWei]
 * @version : [v1.0]
 * @className : ElasticSearchConfig
 * @description : [ElasticSearch配置类]
 * @createTime : [2022/10/14 14:36]
 * @updateUser : [WangWei]
 * @updateTime : [2022/10/14 14:36]
 * @updateRemark : [描述说明本次修改内容]
 */
@Configuration
public class ElasticSearchConfig {
    /*
     * @version V1.0 
     * Title: restHighLevelClient
     * @author Wangwei 
     * @description 创建构造器
     * @createTime  2022/10/17 16:35
     * @param [] 
     * @return org.elasticsearch.client.RestHighLevelClient
     */
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("127.0.0.0", 9200, "http")));
        return client;
    }
}
}

CRUD操作

rest-high-level 7.6.2 API

在对应的业务类中注入RestHighLevelClient

@Autowired
    private RestHighLevelClient restHighLevelClient;
 /*
    * @version V1.0
    * Title: testCreatIndex
    * @author Wangwei
    * @description 创建索引
    * @createTime  2022/10/17 17:15
    * @param []
    * @return void
    */
    public void testCreatIndex() throws IOException {
        //创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("test_index");
        //客户端执行请求IndicesClient,请求后获得相应
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
    }
    /*
     * @version V1.0
     * Title: testDeleteIndex
     * @author Wangwei
     * @description 删除索引
     * @createTime  2022/10/17 17:15
     * @param []
     * @return void
     */
    public  void testDeleteIndex() throws IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("test_index");
        //执行删除索引的方法
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        //查看是否删除成功
        System.out.println(delete.isAcknowledged());
    }
    /*
     * @version V1.0
     * Title: testAddDocument
     * @author Wangwei
     * @description 添加文档
     * @createTime  2022/10/17 17:24
     * @param []
     * @return void
     */
    public void testAddDocument() throws IOException {
        //创建对象
        ElasticSearchWordModel elasticSearchWordModel=new ElasticSearchWordModel;
        elasticSearchWordModel.setText("David");
        elasticSearchWordModel.setAnalyzer("ICU分词器");
        //创建请求
        IndexRequest request = new IndexRequest("test_index");
        //设置这条文档的id为1
        request.id("1");
        //将数据放入请求
        request.source(JSON.toJSONString(elasticSearchWordModel), XContentType.JSON);
        //向客户端发送请求,获取相应的结果
        IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        //输出信息和状态
        System.out.println(indexResponse.toString());
        System.out.println(indexResponse.status());
    }
    /*
     * @version V1.0
     * Title: testGetDocument
     * @author Wangwei
     * @description 获取文档信息
     * @createTime  2022/10/17 17:25
     * @param []
     * @return void
     */
    public void testGetDocument() throws IOException {
        //构造条件,这选择的是查询
        GetRequest test_index = new GetRequest("test_index","1");
        GetResponse documentFields = restHighLevelClient.get(test_index, RequestOptions.DEFAULT);
        //打印文档的内容
        System.out.println(documentFields.getSourceAsString());
        //返回全部内容
        System.out.println(documentFields);
    }

如果博主的文章对您有所帮助,可以评论、点赞、收藏,支持一下博主!!!


ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型) ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型) ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型) ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型) ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型) ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型) ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型) ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型) ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型) ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型) ps:一个索引库下可以有不同类型的索引(目前6.X以后的版本只能有一个类型)
相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
目录
相关文章
存储 JSON Java
580 0
|
10月前
|
NoSQL Java API
微服务——SpringBoot使用归纳——Spring Boot 中集成Redis——Spring Boot 集成 Redis
本文介绍了在Spring Boot中集成Redis的方法,包括依赖导入、Redis配置及常用API的使用。通过导入`spring-boot-starter-data-redis`依赖和配置`application.yml`文件,可轻松实现Redis集成。文中详细讲解了StringRedisTemplate的使用,适用于字符串操作,并结合FastJSON将实体类转换为JSON存储。还展示了Redis的string、hash和list类型的操作示例。最后总结了Redis在缓存和高并发场景中的应用价值,并提供课程源代码下载链接。
2197 0
|
10月前
|
人工智能 自然语言处理 Java
对话即服务:Spring Boot整合MCP让你的CRUD系统秒变AI助手
本文介绍了如何通过Model Context Protocol (MCP) 协议将传统Spring Boot服务改造为支持AI交互的智能系统。MCP作为“万能适配器”,让AI以统一方式与多种服务和数据源交互,降低开发复杂度。文章以图书管理服务为例,详细说明了引入依赖、配置MCP服务器、改造服务方法(注解方式或函数Bean方式)及接口测试的全流程。最终实现用户通过自然语言查询数据库的功能,展示了MCP在简化AI集成、提升系统易用性方面的价值。未来,“对话即服务”有望成为主流开发范式。
6913 7
|
11月前
|
人工智能 运维 自然语言处理
Elasticsearch AI Assistant 集成 DeepSeek,1分钟搭建智能运维助手
Elasticsearch 新支持 DeepSeek 系列模型,使用 AI 助手,通过自然语言交互,为可观测性分析、安全运维管理及数据智能处理提供一站式解决方案。
1219 3
Elasticsearch AI Assistant 集成 DeepSeek,1分钟搭建智能运维助手
|
12月前
|
存储 安全 Java
Spring Boot 3 集成Spring AOP实现系统日志记录
本文介绍了如何在Spring Boot 3中集成Spring AOP实现系统日志记录功能。通过定义`SysLog`注解和配置相应的AOP切面,可以在方法执行前后自动记录日志信息,包括操作的开始时间、结束时间、请求参数、返回结果、异常信息等,并将这些信息保存到数据库中。此外,还使用了`ThreadLocal`变量来存储每个线程独立的日志数据,确保线程安全。文中还展示了项目实战中的部分代码片段,以及基于Spring Boot 3 + Vue 3构建的快速开发框架的简介与内置功能列表。此框架结合了当前主流技术栈,提供了用户管理、权限控制、接口文档自动生成等多项实用特性。
890 8
|
消息中间件 监控 Java
您是否已集成 Spring Boot 与 ActiveMQ?
您是否已集成 Spring Boot 与 ActiveMQ?
415 0
|
JSON Java API
springboot集成ElasticSearch使用completion实现补全功能
springboot集成ElasticSearch使用completion实现补全功能
242 1
|
开发框架 监控 搜索推荐
GoFly快速开发框架集成ZincSearch全文搜索引擎 - Elasticsearch轻量级替代为ZincSearch全文搜索引擎
本文介绍了在项目开发中使用ZincSearch作为全文搜索引擎的优势,包括其轻量级、易于安装和使用、资源占用低等特点,以及如何在GoFly快速开发框架中集成和使用ZincSearch,提供了详细的开发文档和实例代码,帮助开发者高效地实现搜索功能。
782 0
|
Web App开发 JavaScript Java
elasticsearch学习五:springboot整合 rest 操作elasticsearch的 实际案例操作,编写搜索的前后端,爬取京东数据到elasticsearch中。
这篇文章是关于如何使用Spring Boot整合Elasticsearch,并通过REST客户端操作Elasticsearch,实现一个简单的搜索前后端,以及如何爬取京东数据到Elasticsearch的案例教程。
880 0
elasticsearch学习五:springboot整合 rest 操作elasticsearch的 实际案例操作,编写搜索的前后端,爬取京东数据到elasticsearch中。
|
8月前
|
JSON 安全 数据可视化
Elasticsearch(es)在Windows系统上的安装与部署(含Kibana)
Kibana 是 Elastic Stack(原 ELK Stack)中的核心数据可视化工具,主要与 Elasticsearch 配合使用,提供强大的数据探索、分析和展示功能。elasticsearch安装在windows上一般是zip文件,解压到对应目录。文件,elasticsearch8.x以上版本是自动开启安全认证的。kibana安装在windows上一般是zip文件,解压到对应目录。elasticsearch的默认端口是9200,访问。默认用户是elastic,密码需要重置。
3939 0