SpringBoot集成Easy-Es全過程
SpringBoot集成Easy-Es
Easy-Es(簡稱EE)是一款基于ElasticSearch(簡稱Es)官方提供的RestHighLevelClient打造的ORM開發(fā)框架,在 RestHighLevelClient 的基礎(chǔ)上,只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生
一、集成demo
1、添加依賴
<!-- 排除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> <!--引入es的坐標(biāo)--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.14.1</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.14.1</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.14.1</version> </dependency> <dependency> <groupId>cn.easy-es</groupId> <artifactId>easy-es-boot-starter</artifactId> <version>1.1.1</version> </dependency>
2、配置信息
# 默認(rèn)為true,若為false時(shí),則認(rèn)為不啟用本框架 easy-es.enable: true #填你的es連接地址 easy-es.address : 127.0.0.1:9200 # username: 有設(shè)置才填寫,非必須 easy-es.username : elastic # password: 有設(shè)置才填寫,非必須 easy-es.password : 123456
3、啟動(dòng)類中添加 @EsMapperScan 注解,掃描 Mapper 文件夾
@SpringBootApplication @EsMapperScan("com.example.elasticsearch.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
4、實(shí)體類和mapper
@Data public class Document { /** * es中的唯一id,當(dāng)您字段命名為id且類型為String時(shí),且不需要采用UUID及自定義ID類型時(shí),可省略此注解 */ @IndexId(type = IdType.NONE) private String id; /** * 文檔標(biāo)題,不指定類型默認(rèn)被創(chuàng)建為keyword類型,可進(jìn)行精確查詢 */ private String title; /** * 文檔內(nèi)容,指定了類型及存儲(chǔ)/查詢分詞器 */ @IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD) private String content; } public interface DocumentMapper extends BaseEsMapper<Document> { }
5、測試
@RestController public class EasyEsController { @Autowired private DocumentMapper documentMapper; @GetMapping("/insert") public Integer insert() { // 初始化-> 新增數(shù)據(jù) Document document = new Document(); document.setTitle("老漢"); document.setContent("推*技術(shù)過硬"); return documentMapper.insert(document); } @GetMapping("/search") public List<Document> search() { // 查詢出所有標(biāo)題為老漢的文檔列表 LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>(); wrapper.eq(Document::getTitle, "老漢"); return documentMapper.selectList(wrapper); } }
http://localhost:8080/insert(插入數(shù)據(jù))
http://localhost:8080/search(查詢數(shù)據(jù))
二、索引CRUD
首先說一下索引的托管模式,EE這里有三種托管模式
- 自動(dòng)托管之平滑模式(默認(rèn)):在此模式下,索引的創(chuàng)建更新數(shù)據(jù)遷移等全生命周期用戶均不需要任何操作即可完成
- 自動(dòng)托管之非平滑模式:在此模式下,索引額創(chuàng)建及更新由EE全自動(dòng)異步完成,但不處理數(shù)據(jù)遷移工作
- 手動(dòng)模式:在此模式下,索引的所有維護(hù)工作EE框架均不介入,由用戶自行處理,EE提供了開箱即用的索引CRUD相關(guān)API
前置條件
索引CRUD相關(guān)的API都屬于手動(dòng)擋范疇,因此我們執(zhí)行下述所有API前必須先配置開啟手動(dòng)擋,以免和自動(dòng)擋沖突
easy-es: global-config: process_index_mode: manul # 手動(dòng)擋模式
創(chuàng)建索引
@Test void createIndex01(){ // 絕大多數(shù)場景推薦使用 documentMapper.createIndex(); } @Test void createIndex02(){ // 適用于定時(shí)任務(wù)按日期創(chuàng)建索引場景 String indexName = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); documentMapper.createIndex(indexName); } @Test void createIndex03() { // 復(fù)雜場景使用 LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>(); // 此處簡單起見 索引名稱須保持和實(shí)體類名稱一致,字母小寫 后面章節(jié)會(huì)教大家更如何靈活配置和使用索引 wrapper.indexName(Document.class.getSimpleName().toLowerCase()); // 此處將文章標(biāo)題映射為keyword類型(不支持分詞),文檔內(nèi)容映射為text類型(支持分詞查詢) wrapper.mapping(Document::getTitle, FieldType.KEYWORD, 2.0f) .mapping(Document::getContent, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD); // 設(shè)置分片及副本信息,可缺省 wrapper.settings(3, 2); // 創(chuàng)建索引 boolean isOk = documentMapper.createIndex(wrapper); }
查詢索引
@Test public void testExistsIndex() { // 測試是否存在指定名稱的索引 String indexName = Document.class.getSimpleName().toLowerCase(); boolean existsIndex = documentMapper.existsIndex(indexName); Assertions.assertTrue(existsIndex); } @Test public void testGetIndex() { GetIndexResponse indexResponse = documentMapper.getIndex(); // 這里打印下索引結(jié)構(gòu)信息 其它分片等信息皆可從indexResponse中取 indexResponse.getMappings().forEach((k, v) -> System.out.println(v.getSourceAsMap())); }
更新索引
/** * 更新索引 */ @Test public void testUpdateIndex() { // 測試更新索引 LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>(); // 指定要更新哪個(gè)索引 String indexName = Document.class.getSimpleName().toLowerCase(); wrapper.indexName(indexName); wrapper.mapping(Document::getTitle, FieldType.KEYWORD); wrapper.mapping(Document::getContent, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD); wrapper.mapping(Document::getInfo, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD); boolean isOk = documentMapper.updateIndex(wrapper); Assertions.assertTrue(isOk); }
刪除索引
@Test public void testDeleteIndex() { // 指定要?jiǎng)h除哪個(gè)索引 String indexName = Document.class.getSimpleName().toLowerCase(); boolean isOk = documentMapper.deleteIndex(indexName); Assertions.assertTrue(isOk); }
三、數(shù)據(jù)CURD
// 插入一條記錄,默認(rèn)插入至當(dāng)前mapper對應(yīng)的索引 Integer insert(T entity); // 插入一條記錄 可指定具體插入的索引,多個(gè)用逗號(hào)隔開 Integer insert(T entity, String... indexNames); // 批量插入多條記錄 Integer insertBatch(Collection<T> entityList) // 批量插入多條記錄 可指定具體插入的索引,多個(gè)用逗號(hào)隔開 Integer insertBatch(Collection<T> entityList, String... indexNames); // 根據(jù) ID 刪除 Integer deleteById(Serializable id); // 根據(jù) ID 刪除 可指定具體的索引,多個(gè)用逗號(hào)隔開 Integer deleteById(Serializable id, String... indexNames); // 根據(jù) entity 條件,刪除記錄 Integer delete(LambdaEsQueryWrapper<T> wrapper); // 刪除(根據(jù)ID 批量刪除) Integer deleteBatchIds(Collection<? extends Serializable> idList); // 刪除(根據(jù)ID 批量刪除)可指定具體的索引,多個(gè)用逗號(hào)隔開 Integer deleteBatchIds(Collection<? extends Serializable> idList, String... indexNames); //根據(jù) ID 更新 Integer updateById(T entity); //根據(jù) ID 更新 可指定具體的索引,多個(gè)用逗號(hào)隔開 Integer updateById(T entity, String... indexNames); // 根據(jù)ID 批量更新 Integer updateBatchByIds(Collection<T> entityList); //根據(jù) ID 批量更新 可指定具體的索引,多個(gè)用逗號(hào)隔開 Integer updateBatchByIds(Collection<T> entityList, String... indexNames); // 根據(jù)動(dòng)態(tài)條件 更新記錄 Integer update(T entity, LambdaEsUpdateWrapper<T> updateWrapper); // 獲取總數(shù) Long selectCount(LambdaEsQueryWrapper<T> wrapper); // 獲取總數(shù) distinct為是否去重 若為ture則必須在wrapper中指定去重字段 Long selectCount(Wrapper<T> wrapper, boolean distinct); // 根據(jù) ID 查詢 T selectById(Serializable id); // 根據(jù) ID 查詢 可指定具體的索引,多個(gè)用逗號(hào)隔開 T selectById(Serializable id, String... indexNames); // 查詢(根據(jù)ID 批量查詢) List<T> selectBatchIds(Collection<? extends Serializable> idList); // 查詢(根據(jù)ID 批量查詢)可指定具體的索引,多個(gè)用逗號(hào)隔開 List<T> selectBatchIds(Collection<? extends Serializable> idList, String... indexNames); // 根據(jù)動(dòng)態(tài)查詢條件,查詢一條記錄 若存在多條記錄 會(huì)報(bào)錯(cuò) T selectOne(LambdaEsQueryWrapper<T> wrapper); // 根據(jù)動(dòng)態(tài)查詢條件,查詢?nèi)坑涗? List<T> selectList(LambdaEsQueryWrapper<T> wrapper);
參數(shù)文檔:Easy-Es文檔
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring 數(shù)據(jù)庫連接池(JDBC)詳解
本篇文章主要介紹了基于Spring的JDBC基本框架搭建;基于Spring的JDBC增刪改查;讀取配置文件中的數(shù)據(jù)等,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-05-05Assert.assertNotNull()斷言是否是空問題
這篇文章主要介紹了Assert.assertNotNull()斷言是否是空問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Spring Boot集成Shiro實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限的完整步驟
這篇文章主要給大家介紹了關(guān)于Spring Boot集成Shiro實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Spring框架原理之實(shí)例化bean和@Autowired實(shí)現(xiàn)原理方式
這篇文章主要介紹了Spring框架原理之實(shí)例化bean和@Autowired實(shí)現(xiàn)原理方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05