SpringBoot 3整合Elasticsearch 8
这里写自定义目录标题
- 版本说明
- spring boot POM依赖
- application.yml配置
- 新建模型映射
- Repository
- 简单测试
- 完整项目文件目录结构
- windows下elasticsearch安装配置
版本说明
官网说明

本文使用最新的版本
springboot: 3.2.3
spring-data elasticsearch: 5.2.3
elasticsearch: 8.11.4
elasticsearch下载链接:https://www.elastic.co/cn/downloads/past-releases#elasticsearch
最新版可能不兼容,以spring官网为准
spring boot POM依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo-es</artifactId><version>0.0.1-SNAPSHOT</version><name>demo-es</name><description>demo-es</description><properties><java.version>17</java.version></properties><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.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>
</project>
application.yml配置
使用https必须配置username 和 password
spring:elasticsearch:uris: https://localhost:9200username: elasticpassword: 123456
新建模型映射
package com.example.demoes.es.model;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "user") // user 是elasticsearch的索引名称(新版本的elasticsearch没有了type的概念)
public class UserModel { // 每一个UserModel对应一个elasticsearch的文档@Id@Field(name = "id", type = FieldType.Integer)Integer id;// FieldType.Keyword 不可分词@Field(name = "name", type = FieldType.Keyword)String name;// index = false 不建立索引@Field(name = "age", type = FieldType.Integer, index = false)Integer age;// FieldType.Text 可分词,ik_smart,ik_max_word 是ik分词器,对中文分词友好,需要另外安装@Field(name = "address", type = FieldType.Text, searchAnalyzer = "ik_smart", analyzer = "ik_max_word")String address;}
Repository
spring data的repository方便操作,类似jpa的操作
继承ElasticsearchRepository自带一些基础的操作方法
package com.example.demoes.es.repo;import com.example.demoes.es.model.UserModel;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;// UserModel 模型映射 Integer ID的类型
public interface ESUserRepository extends ElasticsearchRepository<UserModel, Integer> {}
简单测试
package com.example.demoes;import com.example.demoes.es.model.UserModel;
import com.example.demoes.es.repo.ESUserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.*;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;@SpringBootTest
class DemoEsApplicationTests {@AutowiredESUserRepository esUserRepository;// 以下三个是 spring-boot-starter-data-elasticsearch 自动配置的 elasticsearch 操作 Bean// 1. DocumentOperations 文档操作@AutowiredDocumentOperations documentOperations;// 2. SearchOperations 查询操作@AutowiredSearchOperations searchOperations;// 3. ElasticsearchOperations elasticsearch 通用的操作,包括DocumentOperations和SearchOperations@AutowiredElasticsearchOperations elasticsearchOperations;@Testvoid contextLoads() {}@Testpublic void testIndex() {// 获取索引操作IndexOperations indexOperations = elasticsearchOperations.indexOps(UserModel.class);// 查看索引映射关系System.out.println(indexOperations.getMapping());// 输出索引名称System.out.println(indexOperations.getIndexCoordinates().getIndexName());}/*** 添加文档*/@Testpublic void testAdd() {esUserRepository.save(new UserModel(1, "张三", 18, "北京朝阳"));esUserRepository.save(new UserModel(2, "李四", 19, "北京朝阳"));esUserRepository.save(new UserModel(3, "王五", 20, "北京朝阳"));esUserRepository.save(new UserModel(4, "赵六", 21, "北京朝阳"));esUserRepository.save(new UserModel(5, "马六", 22, "北京朝阳"));esUserRepository.save(new UserModel(6, "孙七", 23, "北京朝阳"));esUserRepository.save(new UserModel(7, "吴八", 24, "北京朝阳"));esUserRepository.save(new UserModel(8, "郑九", 25, "北京朝阳"));// 查询所有esUserRepository.findAll().forEach(System.out::println);}/*** 更新文档*/@Testpublic void testUpdate() {// 按id更新IndexCoordinates indexCoordinates = elasticsearchOperations.indexOps(UserModel.class).getIndexCoordinates();documentOperations.update(new UserModel(1, "张三", 60, "北京朝阳"), indexCoordinates);}/*** 删除文档*/@Testpublic void testDelete() {documentOperations.delete(String.valueOf(8), UserModel.class);}/*** 查询文档*/@Testpublic void testSearch() {CriteriaQuery query = new CriteriaQuery(new Criteria("id").is(2));SearchHits<UserModel> searchHits = searchOperations.search(query, UserModel.class);for (SearchHit searchHit : searchHits.getSearchHits()){UserModel user = (UserModel) searchHit.getContent();System.out.println(user);}}}
完整项目文件目录结构

windows下elasticsearch安装配置
直接解压修改配置文件解压目录/config/elasticsearch.yml
# 集群名称
cluster.name: el-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
# 节点名称
node.name: el-node-1# 数据和日志存储路径,默认安装位置path.data: D:/module/elasticsearch-8.11.4/datapath.logs: D:/module/elasticsearch-8.11.4/logs# 访问限制,0.0.0.0代表所有IP都可以访问,localhost也可以
network.host: 0.0.0.0
# 访问端口 默认9200
http.port: 9200# 安全配置,以下的配置第一次启动时自动生成,也可以不配置
#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically
# generated to configure Elasticsearch security features on 21-03-2024 01:32:15
#
# --------------------------------------------------------------------------------# Enable security features 不使用https时设为false
xpack.security.enabled: truexpack.security.enrollment.enabled: true# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents 不使用https时设为false
xpack.security.http.ssl:enabled: truekeystore.path: certs/http.p12# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:enabled: trueverification_mode: certificatekeystore.path: certs/transport.p12truststore.path: certs/transport.p12
# Create a new cluster with the current node only
# Additional nodes can still join the cluster later
cluster.initial_master_nodes: ["el-node-1"]#----------------------- END SECURITY AUTO CONFIGURATION -------------------------
第一次启动会在控制台打印密码,用户名默认elastic
修改密码的话不要关闭控制台,另外开启一个控制台,进入elastic search安装目录下的bin目录,使用以下命令修改
-i 是交互式的意思,没有的话会随机生成密码,无法自定义。
输入命令回车然后输入两次密码就行了
elasticsearch-reset-password --username elastic -i
使用keytool工具将ca证书导入到jdk。
keytool是jdk自带的工具,使用以下命令
keytool -importcert -cacerts -alias "es_http_ca" -file "elasticsearch安装路径\config\certs\http_ca.crt"
es_http_ca 是证书别名
相关文章:
SpringBoot 3整合Elasticsearch 8
这里写自定义目录标题 版本说明spring boot POM依赖application.yml配置新建模型映射Repository简单测试完整项目文件目录结构windows下elasticsearch安装配置 版本说明 官网说明 本文使用最新的版本 springboot: 3.2.3 spring-data elasticsearch: 5.2.3 elasticsearch: 8.1…...
突破编程_C++_查找算法(分块查找)
1 算法题 :使用分块算法在有序数组中查找指定元素 1.1 题目含义 在给定一个有序数组的情况下,使用分块查找算法来查找数组中是否包含指定的元素。分块查找算法是一种结合了顺序查找和二分查找思想的算法,它将有序数组划分为若干个块&#x…...
学习java第二十二天
IOC 容器具有依赖注入功能的容器,它可以创建对象,IOC 容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。通常new一个实例,控制权由程序员控制,而"控制反转"是指new实例工作不由程序员来做而是交给Sp…...
每天学习一个Linux命令之systemctl
每天学习一个Linux命令之systemctl 介绍 在Linux系统中,systemctl命令是Systemd初始化系统的核心管理工具之一。systemd是用来启动、管理和监控运行在Linux上的系统的第一个进程(PID 1),它提供了一整套强大的工具和功能…...
【机器学习入门】人工神经网络(二)卷积和池化
系列文章目录 第1章 专家系统 第2章 决策树 第3章 神经元和感知机 识别手写数字——感知机 第4章 线性回归 第5章 逻辑斯蒂回归和分类 第5章 支持向量机 第6章 人工神经网络(一) 文章目录 系列文章目录前言一、卷积神经连接的局部性平移不变性 二、卷积处理图像的效果代码二、…...
公司内部局域网怎么适用飞书?
随着数字化办公的普及,企业对于内部沟通和文件传输的需求日益增长。飞书作为一款集成了即时通讯、云文档、日程管理、视频会议等多种功能的智能协作平台,已经成为许多企业提高工作效率的首选工具。本文将详细介绍如何在公司内部局域网中应用飞书…...
JVM的知识
什么是JVM 1.JVM: JVM其实就是运行在 操作系统之上的一个特殊的软件。 2.JVM的内部结构: (1)因为栈会将执行的程序弹出栈。 (2)垃圾99%的都是在堆和方法区中产生的。 类加载器:加载class文件。…...
大模型日报2024-03-24
利用LLMs评分及解释K-12科学答案 摘要: 本文研究了在K-12级科学教育中使用大型语言模型(LLMs)对短答案评分及解释。研究采用GPT-4结合少量样本学习和活跃学习,通过人机协作提供有意义的评估反馈。 MathVerse:多模态LLM解数学题效果…...
Android kotlin全局悬浮窗全屏功能和锁屏页面全屏悬浮窗功能一
1.前言 在进行app应用开发中,在实现某些功能中要求实现悬浮窗功能,分为应用内悬浮窗 ,全局悬浮窗和 锁屏页面悬浮窗功能 等,接下来就来实现这些悬浮窗全屏功能,首选看下第一部分功能实现 2.kotlin全局悬浮窗全屏功能和锁屏页面全屏悬浮窗功能一分析 悬浮窗是属于Androi…...
图像识别在安防领域的应用
图像识别技术在安防领域有着广泛的应用,它通过分析和理解图像中的视觉信息,为安防系统提供了强大的辅助功能。以下是一些主要的应用领域: 人脸识别:人脸识别技术是安防领域中最常见的应用之一。它可以帮助系统识别和验证个人身份…...
前端面试集中复习 - http篇
1. http请求方式 HTTP请求方式有哪些:GET POST PUT DELETE OPTIONS 1) GET POST 的区别? 场景上: GET 用于获取资源而不对服务器资源做更改提交的请求,多次执行结果一致。用于获取静态数据,幂等。 POST࿱…...
C++ - 类和对象(上)
目录 一、类的定义 二、访问限定符 public(公有) protected(保护) private(私有) 三、类声明和定义分离 四、外部变量和成员变量的区别与注意 五、类的实例化 六、类对象的模型 七、类的this指针…...
mysql基础4sql优化
SQL优化 插入数据优化 如果我们需要一次性往数据库表中插入多条记录,可以从以下三个方面进行优化。 insert into tb_test values(1,tom); insert into tb_test values(2,cat); insert into tb_test values(3,jerry);-- 优化方案一:批量插入数据 Inser…...
实现Spring Web MVC中的文件上传功能,并处理大文件和多文件上传
实现Spring Web MVC中的文件上传功能,并处理大文件和多文件上传 在Spring Web MVC中实现文件上传功能并处理大文件和多文件上传是一项常见的任务。下面是一个示例,演示如何在Spring Boot应用程序中实现这一功能: 添加Spring Web依赖&#x…...
搭建vite项目
文章目录 Vite 是一个基于 Webpack 的开发服务器,用于开发 Vue 3 和 Vite 应用程序 一、创建一个vite项目二、集成Vue Router1.安装 vue-routernext插件2.在 src 目录下创建一个名为 router 的文件夹,并在其中创建一个名为 index.js 的文件。在这个文件中…...
Docker 安装mysql 主从复制
目录 1 MySql主从复制简介 1.1 主从复制的概念 1.2 主从复制的作用 2. 搭建主从复制 2.1 pull mysql 镜像 2.2 新建主服务器容器实例 3307 2.2.1 master创建 my.cnf 2.2.2 重启master 2.2.3 进入mysql 容器,创建同步用户 2.3 新建从服务器容器实例 3308…...
GPT每日面试题—如何实现二分查找
充分利用ChatGPT的优势,帮助我们快速准备前端面试。今日问题:如何实现二分查找? Q:如果在前端面试中,被问到如何实现二分查找,如果回答比较好,给出必要的代码示例 A:当被问到如何实…...
机器学习神经网络由哪些构成?
机器学习神经网络通常由以下几个主要组件构成: 1. **输入层(Input Layer)**:输入层接受来自数据源(例如图像、文本等)的原始输入数据。每个输入特征通常表示为输入层中的一个节点。 2. **隐藏层ÿ…...
代码随想录算法训练营day19 | 二叉树阶段性总结
各个部分题目的代码题解都在我往日的二叉树的博客中。 (day14到day22) 目录 二叉树理论基础二叉树的遍历方式深度优先遍历广度优先遍历 求二叉树的属性二叉树的修改与制造求二叉搜索树的属性二叉树公共最先问题二叉搜索树的修改与构造总结 二叉树理论基础 二叉树的理论基础参…...
数据库引论:3、中级SQL
一些更复杂的查询表达 3.1 连接表达式 拼接多张表的几种方式 3.1.1 自然连接 natural join,自动连接在所有共同属性上相同的元组 join… using( A 1 , A 2 , ⋯ A_1,A_2,\cdots A1,A2,⋯):使用括号里的属性进行自然连接,除了这些属性之外的共同…...
KubeSphere 容器平台高可用:环境搭建与可视化操作指南
Linux_k8s篇 欢迎来到Linux的世界,看笔记好好学多敲多打,每个人都是大神! 题目:KubeSphere 容器平台高可用:环境搭建与可视化操作指南 版本号: 1.0,0 作者: 老王要学习 日期: 2025.06.05 适用环境: Ubuntu22 文档说…...
RestClient
什么是RestClient RestClient 是 Elasticsearch 官方提供的 Java 低级 REST 客户端,它允许HTTP与Elasticsearch 集群通信,而无需处理 JSON 序列化/反序列化等底层细节。它是 Elasticsearch Java API 客户端的基础。 RestClient 主要特点 轻量级ÿ…...
springboot 百货中心供应链管理系统小程序
一、前言 随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,百货中心供应链管理系统被用户普遍使用,为方…...
关于iview组件中使用 table , 绑定序号分页后序号从1开始的解决方案
问题描述:iview使用table 中type: "index",分页之后 ,索引还是从1开始,试过绑定后台返回数据的id, 这种方法可行,就是后台返回数据的每个页面id都不完全是按照从1开始的升序,因此百度了下,找到了…...
uniapp中使用aixos 报错
问题: 在uniapp中使用aixos,运行后报如下错误: AxiosError: There is no suitable adapter to dispatch the request since : - adapter xhr is not supported by the environment - adapter http is not available in the build 解决方案&…...
让回归模型不再被异常值“带跑偏“,MSE和Cauchy损失函数在噪声数据环境下的实战对比
在机器学习的回归分析中,损失函数的选择对模型性能具有决定性影响。均方误差(MSE)作为经典的损失函数,在处理干净数据时表现优异,但在面对包含异常值的噪声数据时,其对大误差的二次惩罚机制往往导致模型参数…...
Redis:现代应用开发的高效内存数据存储利器
一、Redis的起源与发展 Redis最初由意大利程序员Salvatore Sanfilippo在2009年开发,其初衷是为了满足他自己的一个项目需求,即需要一个高性能的键值存储系统来解决传统数据库在高并发场景下的性能瓶颈。随着项目的开源,Redis凭借其简单易用、…...
基于Java+VUE+MariaDB实现(Web)仿小米商城
仿小米商城 环境安装 nodejs maven JDK11 运行 mvn clean install -DskipTestscd adminmvn spring-boot:runcd ../webmvn spring-boot:runcd ../xiaomi-store-admin-vuenpm installnpm run servecd ../xiaomi-store-vuenpm installnpm run serve 注意:运行前…...
数据结构:递归的种类(Types of Recursion)
目录 尾递归(Tail Recursion) 什么是 Loop(循环)? 复杂度分析 头递归(Head Recursion) 树形递归(Tree Recursion) 线性递归(Linear Recursion)…...
uni-app学习笔记三十五--扩展组件的安装和使用
由于内置组件不能满足日常开发需要,uniapp官方也提供了众多的扩展组件供我们使用。由于不是内置组件,需要安装才能使用。 一、安装扩展插件 安装方法: 1.访问uniapp官方文档组件部分:组件使用的入门教程 | uni-app官网 点击左侧…...
