SpringBoot整合Easy-es
一.什么是Easy-Es
二.为什么要使用Easy-Es
- 简化操作:EasyEs让开发者无需掌握复杂的DSL语句MySQLElasticsearch
- 提高效率:对于熟悉Mybatis-Plus
- 易于集成:EasyEs作为一款轻量级的ORM框架,对现有工程的影响小,几乎无侵入性,启动时会自动注入基本的CRUD操作,性能基本无损耗,使得开发者能够直接面向对象操作,提高了开发效率。
- 降低成本:在特定的应用场景下,如大型数据的存储和检索,EasyEs通过优化存储和索引方式,如使用ZSTD压缩功能快照备份S3存储
三.Spring整合Easy-Es
1.依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- lombok插件依赖 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency><!-- Easy-Es暂不支持SpringBoot3.X,且推荐Elasticsearch版本为7.14.0 --><dependency><groupId>cn.easy-es</groupId><artifactId>easy-es-boot-starter</artifactId><version>1.1.1</version><exclusions><exclusion><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclusion><exclusion><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId></exclusion><exclusion><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.14.0</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.14.0</version></dependency>
</dependencies>
2.配置
easy-es:enable: trueaddress : 服务器地址:9200global-config:process_index_mode: manual
3.启动类
@SpringBootApplication
@EsMapperScan("com.easyes.mapper")
public class EasyEsApplication {public static void main(String[] args) {SpringApplication.run(EasyEsApplication.class, args);}}
4.mapper
public interface DocumentMapper extends BaseEsMapper<Document> {}
5.service
package com.easyes.service;import com.easyes.entity.Document;import java.util.List;public interface IDocumentService{/*** 查询ES所有数据* @return 查询Document结果对象集合*/List<Document> findAllData();/*** 创建索引* @return 结果信息* @throws Exception*/String createIndex() throws Exception;/*** 删除索引* @return 结果信息*/String deleteIndex();/*** ES新增数据* @param document 新增数据实体类* @return 结果信息* @throws Exception*/String addData(Document document) throws Exception;/*** 根据id删除ES数据* @param id 需要删除的数据的id* @return*/String deleteDataById(String id);/*** 修改ES数据* @param document 修改数据对象*/String updateData(Document document);/*** 分词匹配查询content字段* @param value 查询内容* @return*/List<Document> findMatch(String value);/*** 根据id查询数据* @param id 查询id* @return*/Document findById(String id);
}
6.serviceImpl
package com.easyes.service.impl;import cn.easyes.common.utils.StringUtils;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.easyes.entity.Document;
import com.easyes.mapper.DocumentMapper;
import com.easyes.service.IDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.Date;
import java.util.List;@Service
public class DocumentServiceImpl implements IDocumentService {@Autowiredprivate DocumentMapper documentMapper;/*** 查询ES所有数据* @return 查询Document结果对象集合*/@Overridepublic List<Document> findAllData() {LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();wrapper.matchAllQuery();return documentMapper.selectList(wrapper);}/*** 创建索引* @return 结果信息* @throws Exception*/@Overridepublic String createIndex() throws Exception {StringBuilder msg = new StringBuilder();String indexName = Document.class.getSimpleName().toLowerCase();boolean existsIndex = documentMapper.existsIndex(indexName);if (existsIndex){throw new Exception("Document实体对应索引已存在,删除索引接口:deleteIndex");}boolean success = documentMapper.createIndex();if (success){msg.append("Document索引创建成功");}else {msg.append("索引创建失败");}return msg.toString();}/*** 删除索引* @return 结果信息*/@Overridepublic String deleteIndex() {StringBuilder msg = new StringBuilder();String indexName = Document.class.getSimpleName().toLowerCase();if (documentMapper.deleteIndex(indexName)){msg.append("删除成功");}else {msg.append("删除失败");}return msg.toString();}/*** ES新增数据* @param document 新增数据实体类* @return 结果信息* @throws Exception*/@Overridepublic String addData(Document document) throws Exception {if (StringUtils.isEmpty(document.getTitle()) || StringUtils.isEmpty(document.getContent())) {throw new Exception("请补全title及content数据");}document.setCreateTime(new Date());documentMapper.insert(document);return "Added successfully!";}/*** 根据id删除ES数据* @param id 需要删除的数据的id* @return*/@Overridepublic String deleteDataById(String id) {documentMapper.deleteById(id);return "Success";}/*** 修改ES数据* @param document 修改数据对象*/@Overridepublic String updateData(Document document) {documentMapper.updateById(document);return "Success";}/*** 分词匹配查询content字段* @param value 查询内容* @return*/@Overridepublic List<Document> findMatch(String value) {LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();wrapper.match(Document::getContent,value);wrapper.orderByDesc(Document::getCreateTime);List<Document> documents = documentMapper.selectList(wrapper);return documents;}/*** 根据id查询数据* @param id* @return*/@Overridepublic Document findById(String id) {return null;}
}
7.controller
package com.easyes.controller;import com.easyes.entity.Document;
import com.easyes.service.IDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class DocumentController {@Autowiredprivate IDocumentService iDocumentService;/*** 创建索引* @return 结果信息* @throws Exception*/@GetMapping("/createIndex")public String createIndex() throws Exception {return iDocumentService.createIndex();}/*** 删除索引* @return 结果信息*/@GetMapping("/deleteIndex")public String deleteIndex(){return iDocumentService.deleteIndex();}/*** 查询ES所有数据* @return 查询Document结果对象集合*/@GetMapping("/findAll")public List<Document> findAll(){return iDocumentService.findAllData();}/*** 根据id查询数据* @param id 查询数据的id* @return 查询Document结果对象*/@GetMapping("/findById")public Document findById(String id){return iDocumentService.findById(id);}/*** ES新增数据* @param document 新增数据对象* @return 结果信息* @throws Exception*/@GetMapping("/add")public String addData(@RequestBody Document document) throws Exception {return iDocumentService.addData(document);}/*** 修改ES数据* @param document 修改数据对象*/@GetMapping("/update")public String updateData(@RequestBody Document document){return iDocumentService.updateData(document);}/*** 根据id删除ES数据* @param id 需要删除的数据的id* @return*/@GetMapping("/delete")public String deleteData(String id){return iDocumentService.deleteDataById(id);}/*** 分词匹配查询content字段* @param value 查询内容* @return*/@GetMapping("/match")public List<Document> findMatch(String value){return iDocumentService.findMatch(value);}
}
实体类注解
// Lombok 注解,自动为类生成 getter 和 setter 方法
@Data
// 指定 Elasticsearch 索引名及分片数
@IndexName(value = "user_es", shardsNum = 3)
public class User {/*** 用户ID* 使用自定义ID类型*/@IndexId(type = IdType.CUSTOMIZE)private Integer id;/*** 用户姓名* 字段类型为 TEXT,使用 IK 最大词元分词器进行索引和搜索* 在搜索结果中,匹配的部分将被高亮显示*/@IndexField(value = "userName", fieldType = FieldType.TEXT, analyzer = Analyzer.IK_MAX_WORD, searchAnalyzer = Analyzer.IK_MAX_WORD)@HighLight(preTag = "<span style=\"color:red\">", postTag = "</span>")private String name;/*** 用户年龄* 字段类型为 INTEGER*/@IndexField(fieldType = FieldType.INTEGER)private Integer age;/*** 用户工资* 字段类型为 DOUBLE*/@IndexField(fieldType = FieldType.DOUBLE)private BigDecimal salary;/*** 用户生日* 字段类型为 DATE*/@IndexField(fieldType = FieldType.DATE)private Date birthday;
}
相关文章:
SpringBoot整合Easy-es
一.什么是Easy-Es Easy-Es(简称EE)是一款基于ElasticSearch(简称Es)官方提供的RestHighLevelClient打造的ORM开发框架,在 RestHighLevelClient 的基础上,只做增强不做改变,为简化开发、提高效率而生,您如果有用过Mybatis-Plus(简称…...
于交错的路径间:分支结构与逻辑判断的思维协奏
大家好啊,我是小象٩(๑ω๑)۶ 我的博客:Xiao Xiangζั͡ޓއއ 很高兴见到大家,希望能够和大家一起交流学习,共同进步。* 这一节内容很多,文章字数达到了史无前例的一万一,我们要来学习分支与循环结构中…...
Linux之读者写者模型与特殊锁的学习
目录 读者写者模型 特殊锁 悲观锁 自旋锁 在前几期,我们学习了多线程的生产者和消费者模型,生产者和消费者模型中,有三种关系,两个角色,一个场所,那么读者写者模型和生产者消费者模型有什么关联吗&…...
回溯专题 记录
回溯的题目按照这套模板进行; 我感觉整体逻辑还是递归,只不过有了pop_back才是回溯概念; class Solution {public:vector<int> path;vector<vector<int>> ans;void backtracking(int n,int k,int startindex){if(path.…...
使用 Python 实现自动化办公(邮件、Excel)
目录 一、Python 自动化办公的准备工作 1.1 安装必要的库 1.2 设置邮件服务 二、邮件自动化处理 2.1 发送邮件 示例代码 注意事项 2.2 接收和读取邮件 示例代码 三、Excel 自动化处理 3.1 读取和写入 Excel 文件 示例代码 3.2 数据处理和分析 示例代码 四、综合…...
贪心算法笔记
贪心算法笔记 大概内容 贪心就是对于一个问题有很多个步骤,我们在每一个步骤中都选取最优的那一个,最后得出答案。就是在一些函数中可行,但是有些比如二次函数,因为它的转折点不一定最优,就是不可行的。那么如何判断贪心呢?有这么几种 看时间复杂度,一般的就是 O ( n…...
Formality:两种等价状态consistency和equality
相关阅读 Formalityhttps://blog.csdn.net/weixin_45791458/category_12841971.html?spm1001.2014.3001.5482 背景 逻辑锥的等价性检查时,存在两种验证模式:一致(consistency)和等同(equality),要理解这两点,首先得明白综合工具…...
Java Web开发基础:HTML的深度解析与应用
文章目录 前言🌍一.B/S 软件开发架构简述🌍二.HTML 介绍❄️2.1 官方文档❄️2.2 网页的组成❄️2.3 HTML 是什么❄️2.4html基本结构 🌍三.HTML标签1.html 的标签/元素-说明2. html 标签注意事项和细节3.font 字体标签4.标题标签5.超链接标签…...
第30章 汇编语言--- 性能优化技巧
汇编语言是用于直接编程计算机硬件的低级语言,它几乎是一对一地映射到机器指令。因为汇编代码与特定处理器架构紧密相关,所以在讨论性能优化技巧时,通常需要考虑具体的CPU架构和指令集。 以下是一些通用的汇编语言性能优化技巧,并…...
HTB:Paper[WriteUP]
目录 连接至HTB服务器并启动靶机 信息收集 使用rustscan对靶机TCP端口进行开放扫描 将靶机TCP开放端口号提取并保存 使用nmap对靶机TCP开放端口进行脚本、服务扫描 使用nmap对靶机TCP开放端口进行漏洞、系统扫描 使用nmap对靶机常用UDP端口进行开放扫描 对靶机进行子域…...
数据库中的 DDL、DML 和 DCL
数据库中的 DDL、DML 和 DCL 在数据库的定义与操作中,DDL、DML 和 DCL 是三个核心概念,分别用于不同层面的数据库管理与操作。 1. DDL(Data Definition Language) - 数据定义语言 定义 DDL 用于定义和管理数据库的结构或模式。…...
OKR 极简史及理解
大家读完觉得有帮助记得点赞和关注!!! 目录 MBO SMART 和 KPI OKR 1. 什么是 OKR? 1.1 Objectives(目标) 1.2 Key Results(关键成果) KR 应当是困难的,但并非不可…...
电商项目-基于ElasticSearch实现商品搜索功能(四)
一、 高亮显示 1.1 高亮分析 高亮显示是指根据商品关键字搜索商品的时候,显示的页面对关键字给定了特殊样式,让它显示更加突出,如商品搜索中,关键字变成了红色,其实就是给定了红色样式。 1.2 高亮搜索实现步骤解析 …...
TCP封装数据帧
void *send_data(void *arg) //这是一个发送数据的线程 {int sockfd init_tcp_cli("192.168.0.148",50000) //传ip和port,port 50000是因为大概前五万都被其它服务所占用,50000后是私人ipif(sockfd < 0){return NULL;}unsigned char …...
数据结构与算法之二叉树: LeetCode 515. 在每个树行中找最大值 (Ts版)
在每个树行中找最大值 https://leetcode.cn/problems/find-largest-value-in-each-tree-row/description/ 描述 给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值 示例1 输入: root [1,3,2,5,3,null,9] 输出: [1,3,9]示例2 输入: root [1,2,3]…...
百度视频搜索架构演进
导读 随着信息技术的迅猛发展,搜索引擎作为人们获取信息的主要途径,其背后的技术架构也在不断演进。本文详细阐述了近年来视频搜索排序框架的重大变革,特别是在大模型技术需求驱动下,如何从传统的多阶段级联框架逐步演变为更加高…...
构造函数的原型原型链
代码示例 // 定义一个构造函数 Test function Test() {this.name 张三 }; //向构造函数的原型添加一个属性 age18 Test.prototype.age 18;//使用构造函数 Test 来实例化一个新对象 const test new Test();//向 Object.prototype 添加了一个名为 sex 的属性,其值…...
nginx反向代理及负载均衡
华子目录 nginx反向代理功能http反向代理反向代理配置参数proxy_pass的注意事项案例:反向代理单台后端服务器案例:反向代理实现动静分离案例:反向代理的缓存功能非缓存场景下测压准备缓存缓存场景下测压验证缓存文件 反向代理负载均衡&#x…...
单片机实物成品-011 火灾监测
火灾监测(20个版本) 版本20: oled显示温湿度烟雾浓度火焰传感器天然气浓度窗户风扇水泵排气系统声光报警语音播报按键WIFI模块 ----------------------------------------------------------------------------- https://www.bilibili.com…...
使用 Docker 在 Alpine Linux 下部署 Caddy 服务器
简介 在现代 web 开发中,选择合适的 web 服务器至关重要。Caddy 是一个功能强大的现代化 HTTP/2 服务器,支持自动 HTTPS,配置简单,适合开发和生产环境。Docker 则为我们提供了一种轻量级的容器化技术,使得应用程序的部…...
SpringBoot-17-MyBatis动态SQL标签之常用标签
文章目录 1 代码1.1 实体User.java1.2 接口UserMapper.java1.3 映射UserMapper.xml1.3.1 标签if1.3.2 标签if和where1.3.3 标签choose和when和otherwise1.4 UserController.java2 常用动态SQL标签2.1 标签set2.1.1 UserMapper.java2.1.2 UserMapper.xml2.1.3 UserController.ja…...
<6>-MySQL表的增删查改
目录 一,create(创建表) 二,retrieve(查询表) 1,select列 2,where条件 三,update(更新表) 四,delete(删除表…...
在HarmonyOS ArkTS ArkUI-X 5.0及以上版本中,手势开发全攻略:
在 HarmonyOS 应用开发中,手势交互是连接用户与设备的核心纽带。ArkTS 框架提供了丰富的手势处理能力,既支持点击、长按、拖拽等基础单一手势的精细控制,也能通过多种绑定策略解决父子组件的手势竞争问题。本文将结合官方开发文档,…...
Qt Widget类解析与代码注释
#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this); }Widget::~Widget() {delete ui; }//解释这串代码,写上注释 当然可以!这段代码是 Qt …...
2024年赣州旅游投资集团社会招聘笔试真
2024年赣州旅游投资集团社会招聘笔试真 题 ( 满 分 1 0 0 分 时 间 1 2 0 分 钟 ) 一、单选题(每题只有一个正确答案,答错、不答或多答均不得分) 1.纪要的特点不包括()。 A.概括重点 B.指导传达 C. 客观纪实 D.有言必录 【答案】: D 2.1864年,()预言了电磁波的存在,并指出…...
条件运算符
C中的三目运算符(也称条件运算符,英文:ternary operator)是一种简洁的条件选择语句,语法如下: 条件表达式 ? 表达式1 : 表达式2• 如果“条件表达式”为true,则整个表达式的结果为“表达式1”…...
04-初识css
一、css样式引入 1.1.内部样式 <div style"width: 100px;"></div>1.2.外部样式 1.2.1.外部样式1 <style>.aa {width: 100px;} </style> <div class"aa"></div>1.2.2.外部样式2 <!-- rel内表面引入的是style样…...
Spring Boot+Neo4j知识图谱实战:3步搭建智能关系网络!
一、引言 在数据驱动的背景下,知识图谱凭借其高效的信息组织能力,正逐步成为各行业应用的关键技术。本文聚焦 Spring Boot与Neo4j图数据库的技术结合,探讨知识图谱开发的实现细节,帮助读者掌握该技术栈在实际项目中的落地方法。 …...
08. C#入门系列【类的基本概念】:开启编程世界的奇妙冒险
C#入门系列【类的基本概念】:开启编程世界的奇妙冒险 嘿,各位编程小白探险家!欢迎来到 C# 的奇幻大陆!今天咱们要深入探索这片大陆上至关重要的 “建筑”—— 类!别害怕,跟着我,保准让你轻松搞…...
[ACTF2020 新生赛]Include 1(php://filter伪协议)
题目 做法 启动靶机,点进去 点进去 查看URL,有 ?fileflag.php说明存在文件包含,原理是php://filter 协议 当它与包含函数结合时,php://filter流会被当作php文件执行。 用php://filter加编码,能让PHP把文件内容…...
