springboot整合easy-es實(shí)現(xiàn)數(shù)據(jù)的增刪改查的示例代碼
背景
目前公司的一個(gè)老項(xiàng)目,查詢賊慢,需要想辦法提升一下速度,于是就想到了ES,現(xiàn)在嘗試一下將ES整合到項(xiàng)目中來提升檢索效率。
ES是基于倒排索引實(shí)現(xiàn)的,倒排索引中一個(gè)表相當(dāng)于一個(gè)索引,表中的每條記錄都是一個(gè)文檔(JSON數(shù)據(jù)),系統(tǒng)會先對字段數(shù)據(jù)進(jìn)行分詞,然后給詞條建立索引,并映射到文檔id。在查詢的時(shí)候根據(jù)輸入進(jìn)行分詞,然后根據(jù)詞條走索引查詢文檔id,再根據(jù)文檔id查詢文檔并放入結(jié)果集,最后將結(jié)果集返回。
一般來說,ES算是難度較高的一個(gè)技術(shù)棧,需要中高級才能熟練駕馭,新手入門比較難,因而我選中了對新手更加友好的easy-es,其在ES的基礎(chǔ)上做了封裝,使得使用起來和MybatisPlus很像,簡單上手。
開始使用easy-es之前,建議先看一下避坑指南
Elastic Search下載
ES官網(wǎng)按照官方推薦下載7.x的ES,我下載了和官方demo一樣的7.14.0版本。
輸入7.14.0搜索該版本并下載
下載完成之后解壓,并去到bin目錄,雙擊elasticsearch.bat文件啟動elasticsearch。
Springboot整合ES
打開Springboot項(xiàng)目(或創(chuàng)建一個(gè)Springboot項(xiàng)目),先全局搜索elastic,看看項(xiàng)目是否已經(jīng)引入過ES,如果有,需要去掉或者更改版本為7.14.0。印象中不同版本的Springboot默認(rèn)引入的一定版本的ES。
在POM文件引入依賴
<!-- 引入easy-es最新版本的依賴--> <dependency> <groupId>org.dromara.easy-es</groupId> <artifactId>easy-es-boot-starter</artifactId> <!--這里L(fēng)atest Version是指最新版本的依賴,比如2.0.0,可以通過下面的圖片獲取--> <version>2.0.0-beta4</version> </dependency> <!-- 排除springboot中內(nèi)置的es依賴,以防和easy-es中的依賴沖突--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <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>
YAML文件增加ES配置(更多配置可以在官網(wǎng)看)
easy-es: enable: true #默認(rèn)為true,若為false則認(rèn)為不啟用本框架 address: 127.0.0.1:9200 # es的連接地址,必須含端口 若為集群,則可以用逗號隔開 例如:127.0.0.1:9200,127.0.0.2:9200 # username: elastic #若無 則可省略此行配置。因?yàn)閯傁螺d的ES默認(rèn)不用賬號密碼登錄,所以注掉 # password: WG7WVmuNMtM4GwNYkyWH #若無 則可省略此行配置
在啟動類設(shè)置ES的mapper包掃描路徑(一般應(yīng)該設(shè)置成帶*的通配格式,方便多模塊項(xiàng)目的掃描。)同時(shí)注意,該mapper的包和mybatisplus的包不能是同一個(gè),不然框架區(qū)分不開。
@EsMapperScan("org.jeecg.modules.test.esmapper") @SpringBootApplication public class JeecgSystemApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(JeecgSystemApplication.class); } public static void main(String[] args) throws UnknownHostException { ConfigurableApplicationContext application = SpringApplication.run(JeecgSystemApplication.class, args); } }
創(chuàng)建實(shí)體類(加上@IndexName注解)
package org.jeecg.modules.message.entity; import org.dromara.easyes.annotation.IndexField; import org.dromara.easyes.annotation.IndexName; import org.dromara.easyes.annotation.rely.Analyzer; import org.dromara.easyes.annotation.rely.FieldType; import org.jeecg.common.aspect.annotation.Dict; import org.jeecg.common.system.base.entity.JeecgEntity; import org.jeecgframework.poi.excel.annotation.Excel; import org.springframework.format.annotation.DateTimeFormat; import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; @Data @IndexName public class SysMessage{ /** ID */ @TableId(type = IdType.ASSIGN_ID) private java.lang.String id; /**推送內(nèi)容*/ private java.lang.String esContent; /**推送所需參數(shù)Json格式*/ private java.lang.String esParam; /**接收人*/ private java.lang.String esReceiver; /**推送失敗原因*/ private java.lang.String esResult; /**發(fā)送次數(shù)*/ private java.lang.Integer esSendNum; /**推送狀態(tài) 0未推送 1推送成功 2推送失敗*/ private java.lang.String esSendStatus; /**推送時(shí)間*/ @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") private java.util.Date esSendTime; /**消息標(biāo)題*/ private java.lang.String esTitle; /**推送方式:1短信 2郵件 3微信*/ private java.lang.String esType; /**備注*/ private java.lang.String remark; /** 創(chuàng)建人 */ private java.lang.String createBy; /** 創(chuàng)建時(shí)間 */ @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private java.util.Date createTime; /** 更新人 */ private java.lang.String updateBy; /** 更新時(shí)間 */ @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private java.util.Date updateTime; }
創(chuàng)建mapper類
package org.jeecg.modules.test.esmapper; import org.dromara.easyes.core.core.BaseEsMapper; import org.jeecg.modules.message.entity.SysMessage; public interface DocumentMapper extends BaseEsMapper<SysMessage> { }
編寫測試類
@Resource private DocumentMapper documentMapper; @GetMapping("/createIndex") @ApiOperation("創(chuàng)建索引") public Object createIndex(String a){ Boolean index = documentMapper.createIndex(); System.out.println(index); return index; } @GetMapping("/createDoc") @ApiOperation("創(chuàng)建文檔") public Object createDoc(String a,String b){ SysMessage sysMessage=new SysMessage(); sysMessage.setEsContent(a); sysMessage.setEsTitle(b); sysMessage.setEsReceiver("系統(tǒng)管理員"); sysMessage.setEsSendNum(10); Integer insert = documentMapper.insert(sysMessage); return insert; } @GetMapping("/updateDoc") @ApiOperation("updateDoc") public Object updateDoc(String a){ LambdaEsUpdateWrapper<SysMessage> wrapper=new LambdaEsUpdateWrapper<>(); wrapper.eq(SysMessage::getEsContent,a) .set(SysMessage::getEsContent,"更改后的標(biāo)題"); Integer update = documentMapper.update(null, wrapper); return update; } @GetMapping("/getDoc") @ApiOperation("查詢文檔") public Object getDoc(String a){ List<SysMessage> list = EsWrappers.lambdaChainQuery(documentMapper).like(SysMessage::getEsContent, a).list(); return list; }
瀏覽器安裝一個(gè)ES可視化插件。我安裝的是es-client
添加連接
選中創(chuàng)建的連接,目前還沒有索引。
測試
啟動Springboot項(xiàng)目,調(diào)用createIndex接口,創(chuàng)建索引 。然后回到瀏覽器插件,點(diǎn)擊刷新,可以看到創(chuàng)建了一個(gè)索引。
調(diào)用createDoc接口,創(chuàng)建一個(gè)文檔記錄
點(diǎn)擊左側(cè)的數(shù)據(jù)展示選項(xiàng),右上角選中創(chuàng)建的索引,點(diǎn)擊刷新,可以看到多了一條記錄。
調(diào)用getDoc接口,查詢記錄,成功查出。
更新的語法和MybatisPlus的wrapper差不多。先用查詢條件eq,in等去篩選要更新的記錄,然后用set去設(shè)置新的值,然后調(diào)用update方法即可。(如下,通過“測試內(nèi)容”找到記錄,并將其的標(biāo)題改成新的內(nèi)容)
在瀏覽器刷新,可以看到數(shù)據(jù)更新了。
刪除的語法比更新還簡單,也是創(chuàng)建一個(gè)esupdatewrapper,用eq、in等篩選,然后調(diào)用delete方法就可以了,就不演示了。
總結(jié)
Springboot整合ES最大可能遇到的問題就是ES版本的問題,也就是依賴沖突。如果依賴沖突,在項(xiàng)目啟動的時(shí)候會有一個(gè)ERROR日志提醒,看到了就想辦法去掉原來帶著的ES依賴或者更改依賴版本為7.14.0
關(guān)于ES的數(shù)據(jù)更新,就要去了解ES同步數(shù)據(jù)庫相關(guān)的知識了。
想要了解easy-es的更多特性,建議去看easy-es的官網(wǎng)文檔。
到此這篇關(guān)于springboot整合easy-es實(shí)現(xiàn)數(shù)據(jù)的增刪改查的示例代碼的文章就介紹到這了,更多相關(guān)springboot easy-es增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合Mybatis使用Druid數(shù)據(jù)庫連接池
這篇文章主要介紹了SpringBoot整合Mybatis使用Druid數(shù)據(jù)庫連接池,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03IDEA不識別Java文件:文件變橙色&顯示后綴名.java的解決
這篇文章主要介紹了IDEA不識別Java文件:文件變橙色&顯示后綴名.java的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03如何使用Spring Batch進(jìn)行批處理任務(wù)管理
本文介紹了如何配置Spring Batch、如何創(chuàng)建批處理任務(wù),以及如何讀取和寫入數(shù)據(jù),希望通過本文的介紹,你能更好地理解和使用Spring Batch來管理批處理任務(wù),感興趣的朋友跟隨小編一起看看吧2024-08-08