springboot simple (13) springboot Elasticsearch(Elasticsearch8.5.1)
这里首先简单的介绍了Elasticsearch,然后实现了springboot集成Elasticsearch。
版本:
Elasticsearch:v8.5.1
Kibana:v8.5.1
springboot集成elasticsearch有两种方式。
1)rest客户端RestHingLevelClient;
2)接口ElasticSearchRepository。
这里采用第1种方式。
1 Elasticsearch简介
ElasticSearch是一个基于Apache Lucene的开源搜索引擎。
Kibana 是一个开源分析和可视化平台,旨在可视化操作 Elasticsearch。
也就是说:
ElasticSearch 只是后台程序,没有界面;
Kibana是ElasticSearch对应的前端。
主要术语:
- 索引(Index)
索引是具有某种相似特征的文档的集合。例如,客户数据索引,产品目录索引,以及订单数据索引。 - 文档(document)
文档是可以被索引的基本单位。文档使用JSON表示。
2 springboot 集成Elasticsearch
Elasticsearch已支持Index(索引)、Document(文档)、Graph(图)、Machine learning(机器学习)等。

这里介绍了Index(索引)、Document(文档)的简单使用。
第1步:pom中引入依赖的包:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId><version>3.0.2</version></dependency>
第2步:application.properties设置相关参数:
server.port =8081spring.data.elasticsearch.repositories.enabled = true
spring.data.elasticsearch.cluster-nodes = localhost:9300spring.elasticsearch.rest.username=elastic
spring.elasticsearch.rest.password=123456
spring.elasticsearch.rest.uris=localhost:9200
第3步:对应的配置类ElasticSearchConfig.java:
@Component
@Configuration
public class ElasticSearchConfig extends AbstractElasticsearchConfiguration {@Value("${spring.elasticsearch.rest.uris}")private String uris ;@Value("${spring.elasticsearch.rest.username}")private String username;@Value("${spring.elasticsearch.rest.password}")private String password ;@Override@Bean(destroyMethod = "close")public RestHighLevelClient elasticsearchClient() {final ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo(uris).withBasicAuth(username, password).build();return RestClients.create(clientConfiguration).rest();}
}
第4步:实现index的工具类IndexUtil.java:
@Component
public class IndexUtil {@Autowiredprivate RestHighLevelClient restHighLevelClient;/*** 创建索引index* @param index* @return* @throws IOException*/public boolean createIndex(String index){if (!isIndexExist(index)) {return false;}boolean isAcknowledged = false;CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);try {AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);isAcknowledged = acknowledgedResponse.isAcknowledged();} catch (IOException e) {e.printStackTrace();} finally {return isAcknowledged;}}/*** 删除索引* @param index* @return* @throws IOException*/public boolean deleteIndex(String index){if (!isIndexExist(index)) {return false;}boolean isAcknowledged = false;DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);try {AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);isAcknowledged = acknowledgedResponse.isAcknowledged();} catch (IOException e) {e.printStackTrace();} finally {return isAcknowledged;}}/*** 判断索引是否存在* @param index* @return* @throws IOException*/public boolean isIndexExist(String index) {boolean isExist = false;try {GetIndexRequest getIndexRequest = new GetIndexRequest(index);isExist = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);} catch (IOException e) {e.printStackTrace();} finally {return isExist;}}
}
第5步:实现document的工具类DocumentUtil.java:
@Component
public class DocumentUtil {@Autowiredprivate RestHighLevelClient restHighLevelClient;/*** 添加document* @param object* @param index* @param id* @return*/public void addDocument(Object object, String index,String id){//1 create IndexRequestIndexRequest indexRequest = new IndexRequest(index);indexRequest.id(id);indexRequest.timeout(TimeValue.timeValueSeconds(1));String content = JSON.toJSONString(object);indexRequest.source(content, XContentType.JSON);try {//2 send IndexRequestIndexResponse indexResponse =restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);} catch (IOException e) {e.printStackTrace();}}/*** 添加document,随机id* @param object* @param index* @return*/public void addDocument(Object object, String index){String id= "";id = UUID.randomUUID().toString().replaceAll("-","").toUpperCase();addDocument(object, index, id);}/*** get Document* @param index* @param id* @return*/public GetResponse getDocumnet(String index,String id){GetResponse getResponse = null;GetRequest getRequest = new GetRequest(index, id);try {getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);}catch (IOException e){e.printStackTrace();} finally {return getResponse;}}/*** update document* @param object* @param index* @param id*/public void updateDocument(Object object, String index,String id){//1 create UpdateRequestUpdateRequest updateRequest = new UpdateRequest(index,id);updateRequest.timeout(TimeValue.timeValueSeconds(1));String content = JSON.toJSONString(object);updateRequest.doc(content, XContentType.JSON);try {//2 send UpdateRequestUpdateResponse updateResponse =restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);} catch (IOException e) {e.printStackTrace();}}/*** 删除Document* @param index* @param id*/public void deleteDocument(String index,String id){//1 create DeleteRequestDeleteRequest deleteRequest = new DeleteRequest(index, id);try {//2 send DeleteRequestDeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);} catch (IOException e) {e.printStackTrace();}}public SearchResponse search(String index, TermQueryBuilder termQueryBuilder){//1 create SearchRequestSearchRequest searchRequest = new SearchRequest(index);//2 create SearchSourceBuilderSearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();searchSourceBuilder.query(termQueryBuilder);searchSourceBuilder.timeout(TimeValue.timeValueSeconds(60));//3 查询条件放入SearchRequestsearchRequest.source(searchSourceBuilder);//4 send SearchRequestSearchResponse searchResponse = null;try {searchResponse = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);} catch (IOException e) {e.printStackTrace();} finally {return searchResponse;}}public SearchResponse searchHighLight(String index, TermQueryBuilder termQueryBuilder){//1 create SearchRequestSearchRequest searchRequest = new SearchRequest(index);//2 create SearchSourceBuilderSearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();searchSourceBuilder.highlighter();searchSourceBuilder.query(termQueryBuilder);searchSourceBuilder.timeout(TimeValue.timeValueSeconds(60));//3 查询条件放入SearchRequestsearchRequest.source(searchSourceBuilder);//4 send SearchRequestSearchResponse searchResponse = null;try {searchResponse = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);} catch (IOException e) {e.printStackTrace();} finally {return searchResponse;}}
}
3 测试验证
前提:安装运行ElasticSearch和Kibana。
第1步:创建index,postman发送请求:

然后,Kibana中输入:
GET /_cat/indices?v
查询到index创建成功。
第2步:创建Document,postman发送请求:

然后,Kibana中输入:
GET heroic/_doc/1
查询到Document创建成功。
第3步:查询Document,postman发送请求:

第4步:修改Document,将name“关羽”改为“刘备”;
postman发送请求:

然后,Kibana中输入:
GET heroic/_doc/1
查询到Document修改成功。

第5步:删除Document,postman发送请求:

然后,Kibana中输入:
GET heroic/_doc/1
查询到Document删除成功。

代码详见:
https://gitee.com/linghufeixia/springboot-simple
chapter9-1
教程列表:
springboot simple(0) springboot简介
springboot simple(1) springboot Helloworld
springboot simple(2) springboot Starter
springboot simple(3 )springboot Web开发
springboot simple(4)springboot 数据持久化
springboot simple (5) springboot Nosql
springboot simple (6) springboot mqtt
springboot simple (7) springboot thrift
springboot simple (8) springboot kafka
springboot simple (9) springboot jpa(Hibernate)
springboot simple (10) springboot protobuf
springboot simple (11) springboot protostuff
springboot simple (12) springboot RabbitMQ
springboot simple (13) springboot Elasticsearch
相关文章:
springboot simple (13) springboot Elasticsearch(Elasticsearch8.5.1)
这里首先简单的介绍了Elasticsearch,然后实现了springboot集成Elasticsearch。 版本: Elasticsearch:v8.5.1 Kibana:v8.5.1 springboot集成elasticsearch有两种方式。 1)rest客户端RestHingLevelClient; …...
《爆肝整理》保姆级系列教程python接口自动化(十七)--Json 数据处理---一次爬坑记(详解)
简介 有些 post 的请求参数是 json 格式的,这个前面发送post 请求里面提到过,需要导入 json模块处理。现在企业公司一般常见的接口因为json数据容易处理,所以绝大多数返回数据也是 json 格式的,我们在做判断时候,往往只…...
分享111个HTML旅游交通模板,总有一款适合您
分享111个HTML旅游交通模板,总有一款适合您 111个HTML旅游交通模板下载链接:https://pan.baidu.com/s/1VHJSBVJbj4PQpPAwxysJBg?pwd8b17 提取码:8b17 Python采集代码下载链接:采集代码.zip - 蓝奏云 汽车租赁平台网页模板 汽…...
guava中ImmutableList使用示例详解
ImmutableList是一个不可变、线程安全的列表集合,它只会获取传入对象的一个副本,而不会影响到原来的变量或者对象,如下代码: int a 23;ImmutableList<Integer> list ImmutableList.of(a, 12);System.out.println(list);a …...
ASE28N50-ASEMI高压N沟道MOS管ASE28N50
编辑-Z ASE28N50在TO-247封装里的静态漏极源导通电阻(RDS(ON))为200mΩ,是一款N沟道高压MOS管。ASE28N50的最大脉冲正向电流ISM为110A,零栅极电压漏极电流(IDSS)为1uA,其工作时耐温度范围为-55~150摄氏度。ASE28N50功…...
MyBatis缓存
文章目录MyBatis的缓存1、缓存概述2、MyBatis的一级缓存2.1 一级缓存的使用2.2 一级缓存的失效3、MyBatis的二级缓存3.1 二级缓存的开启3.2 二级缓存的失效3.2 二级缓存相关配置4、系统缓存的查询顺序5、EHCache的使用5.1 EHCache基本介绍5.2 EHCache的基本使用5.3 EHCache配置…...
Linux环境下(CentOS 7)安装Java(JDK8)
Linux环境下(CentOS 7)安装Java(JDK8) 一、安装教程 1.1 首先,进入oracle官网下载jdk8的安装包,下载地址如下,这里以 jdk-8u121-linux-x64.tar.gz安装包为例。 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-21…...
基于STM32L431+Liteos的串口空闲中断加DMA循环接收
①MCU为STM32L431,使用串口2。 ②Liteos采用接管中断的方式。 STM32CubeMX配置生成串口代码: 串口DMA接收和发送配置区别是接收采用循环模式,发送为正常模式。 将生成的代码移植到liteos工程中,由于使用的接管中断的方式&#…...
BZOJ4403 序列统计
题目描述 给定三个正整数N、L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量。输出答案对106310^631063取模的结果。 输入 输入第一行包含一个整数T,表示数据组数。 第2到第T1行每行包含三个整数N、L和R,N、…...
如何正确使用 钳位二极管
在电路设计中,经常遇到需要IO保护的场景,比如ADC采样,GPIO接收电平信号等。 常见的保护方法有分压,限幅,限流等。本次我们讨论限幅方法中的 钳位二极管。 我们以BAT54S为例,它的符号是这样的, 而在很多手册里,我们可以看到,一般是这样使用的: 因此,我设计了简化…...
【C语言进阶】动态内存管理
👦个人主页:Weraphael ✍🏻作者简介:目前是C语言学习者 ✈️专栏:C语言航路 🐋 希望大家多多支持,咱一起进步!😁 如果文章对你有帮助的话 欢迎 评论💬 点赞&a…...
第一批因ChatGPT坐牢的人,已经上路了
大家好,我是 Jack。 ChatGPT 的火爆有目共睹,有人靠着它赚了第一桶金,也有人靠着它即将吃上第一顿牢饭。 任何一件东西的火爆,总会给一些聪明人带来机会。 艾尔登法环火的时候,一堆淘宝卖魂的;羊了个羊火…...
Eclipse下Maven的集成
Eclipse下Maven的集成 2.1指定本地maven环境 参考:Eclipse的Maven创建_叶书文的博客-CSDN博客_eclipse创建maven项目 指定用本地maven指定maven仓库设置和地址2.2创建maven项目 1.新建 2.目录设置 3.坐标设置(随便写就行) 4.目录结构 2.3配置…...
Elasticsearch7学习笔记(尚硅谷)
文章目录一、ElasticSearch概述1、ElasticSearch是什么2、全文搜索引擎3、ElasticSearch 和 Solr3.1 概述3.2 比较总结二、Elasticsearch入门1、Elasticsearch安装1.1 下载使用1.2 数据格式2、索引操作3、文档操作(了解)3.1 创建文档3.2 文档查询3.3 文档…...
前端学习第一阶段-第7章 品优购电商项目
7-1 品优购项目介绍及准备工作 01-品优购项目导读 02-网站制作流程 03-品优购项目规划 04-品优购项目搭建 05-品优购项目-样式的模块化开发 06-品优购项目-favicon图标制作 07-品优购项目-TDK三大标签SEO优化 7-2 首页Header区域实现 08-品优购首页-快捷导航shortcut结构搭建 0…...
cocos2dx 4.0 - cpp - pc版 环境搭建
开发环境vs2022 cocos2dx4.0 python2.7.18 cmake3.25安装教程(环境搭建)安装VS2022-Community, 勾选c进行安装安装cmake3.25, 勾选环境变量进行安装安装python2.7.18, 勾选环境变量进行安装下载cocos2dx4.0并解压配置cocos2dx:运行cmd,进入…...
剑指 Offer 53 - I. 在排序数组中查找数字 I
原题链接 难度:easy\color{Green}{easy}easy 题目描述 统计一个数字在排序数组中出现的次数。 示例 1: 输入: nums [5,7,7,8,8,10], target 8 输出: 2示例 2: 输入: nums [5,7,7,8,8,10], target 6 输出: 0提示: 0<nums.length<1050 <…...
华为OD机试 - 删除指定目录(Python) | 机试题算法思路 【2023】
最近更新的博客 华为OD机试 - 热点网络统计 | 备考思路,刷题要点,答疑 【新解法】 华为OD机试 - 查找单入口空闲区域 | 备考思路,刷题要点,答疑 【新解法】 华为OD机试 - 好朋友 | 备考思路,刷题要点,答疑 【新解法】 华为OD机试 - 找出同班小朋友 | 备考思路,刷题要点…...
PowerShell Install Office 2021 Pro Plus Viso Professional
前言 微软Office在很长一段时间内都是最常用和最受欢迎的软件。从小型创业公司到大公司,它的使用比例相当。它可以很容易地从微软的官方网站下载。但是,微软只提供安装程序,而不提供完整的软件供下载。这些安装文件通常比较小。下载并运行后,安装的文件将从后端服务器安装M…...
KubeSphere实战
文章目录一、KubeSphere平台安装1、Kubernetes上安装KubeSphere1.1 安装docker1.2 安装Kubernetes1.3 前置环境之nfs存储1.4 前置环境之metrics-server1.5 安装KubeSphere2、Linux单节点部署KubeSphere3、Linux多节点部署KubeSphere(推荐)二、KubeSphere实战1、多租户实战2、中…...
人工智能高质量数据集概述
人工智能高质量数据集,是指经过标准化采集、清洗、标注、质检、脱敏及结构化处理,能够直接用于人工智能模型开发、训练与优化,且能有效提升模型性能、保障模型泛化能力,具备高可用性、高一致性、高安全性和高适配性的结构化或非结…...
智能提取视频转文档:自动化工具提升内容处理效率
智能提取视频转文档:自动化工具提升内容处理效率 【免费下载链接】extract-video-ppt extract the ppt in the video 项目地址: https://gitcode.com/gh_mirrors/ex/extract-video-ppt 在数字化学习与办公场景中,视频内容提取已成为知识管理的重要…...
3步解锁iOS激活锁:Applera1n工具完整使用指南
3步解锁iOS激活锁:Applera1n工具完整使用指南 【免费下载链接】applera1n icloud bypass for ios 15-16 项目地址: https://gitcode.com/gh_mirrors/ap/applera1n 当你面对一部显示"激活锁"界面的iPhone,反复输入Apple ID却始终无法进入…...
AgentCPM深度研报助手10分钟快速部署教程:基于CSDN星图GPU平台
AgentCPM深度研报助手10分钟快速部署教程:基于CSDN星图GPU平台 你是不是也遇到过这种情况?面对海量的行业报告、公司财报,想快速提炼核心观点,却感觉无从下手,或者需要花费大量时间手动整理。现在,有了AI助…...
Meixiong Niannian与SpringBoot微服务架构
Meixiong Niannian与SpringBoot微服务架构 1. 引言 在当今快速发展的AI应用领域,如何将强大的画图引擎无缝集成到企业级系统中是一个关键挑战。Meixiong Niannian作为一款高性能的AI画图引擎,能够生成高质量的图像内容,而SpringBoot微服务架…...
Docker 部署 Vaultwarden:轻量级自托管密码管理解决方案
1. 为什么选择Vaultwarden作为自托管密码管理方案 在这个数字时代,我们每个人平均要管理超过100个在线账户的密码。传统的密码管理方式——用同一个简单密码注册所有网站,或者把密码写在记事本上——已经远远不能满足安全需求。这就是为什么像Bitwarden这…...
前端微前端架构:别再把所有功能都放在一个应用里了
前端微前端架构:别再把所有功能都放在一个应用里了 各位前端同行,咱们今天聊聊前端微前端架构。别告诉我你还在把所有功能都放在一个应用里,那感觉就像在一个房间里放了所有家具。 为什么你需要微前端架构 最近看到一个项目,单页应…...
Windows系统卡顿?一招禁用Microsoft Compatibility Telemetry释放CPU资源(附详细截图)
Windows系统卡顿终极解决方案:彻底禁用Microsoft Compatibility Telemetry 最近帮朋友处理一台老笔记本时,遇到了典型的Windows系统卡顿问题——风扇狂转、程序响应迟缓,任务管理器里一个叫"Microsoft Compatibility Telemetry"的进…...
Audio Pixel Studio效果惊艳集锦:10类垂直场景语音生成+分离真实案例
Audio Pixel Studio效果惊艳集锦:10类垂直场景语音生成分离真实案例 1. 引言:当声音创作变得触手可及 想象一下,你正在为一个短视频项目寻找合适的旁白配音,但预算有限,专业配音师的价格让你望而却步。或者ÿ…...
C1——优化3Dtiles透明度设置以实现管线可视化
1. 为什么需要调整3Dtiles透明度? 在地理信息系统(GIS)和三维可视化项目中,我们经常会遇到多层数据叠加显示的需求。比如在城市地下管线可视化场景中,地表建筑模型(3Dtiles)和地下管线网络需要同…...
