SpringBoot集成Easy-Es全過程
SpringBoot集成Easy-Es
Easy-Es(簡(jiǎn)稱EE)是一款基于ElasticSearch(簡(jiǎn)稱Es)官方提供的RestHighLevelClient打造的ORM開發(fā)框架,在 RestHighLevelClient 的基礎(chǔ)上,只做增強(qiáng)不做改變,為簡(jiǎn)化開發(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、測(cè)試
@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ù)場(chǎng)景推薦使用
documentMapper.createIndex();
}
@Test
void createIndex02(){
// 適用于定時(shí)任務(wù)按日期創(chuàng)建索引場(chǎng)景
String indexName = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
documentMapper.createIndex(indexName);
}
@Test
void createIndex03() {
// 復(fù)雜場(chǎng)景使用
LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>();
// 此處簡(jiǎn)單起見 索引名稱須保持和實(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() {
// 測(cè)試是否存在指定名稱的索引
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() {
// 測(cè)試更新索引
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對(duì)應(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常用注解 使用注解來構(gòu)造IoC容器的方法
下面小編就為大家分享一篇Spring常用注解 使用注解來構(gòu)造IoC容器的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Win10系統(tǒng)下配置java環(huán)境變量的全過程
這篇文章主要給大家介紹了關(guān)于Win10系統(tǒng)下配置java環(huán)境變量的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
解決maven父子工程install的時(shí)候排除某些子模塊,讓子模塊不install問題
在Maven父子工程中,如果希望某個(gè)子模塊不被安裝到本地倉(cāng)庫(kù),可以在該子模塊的`pom.xml`文件中添加以下配置: ```xml ... org.apache.maven.plugins maven-install-plugin 2.5.2 true2024-12-12
SpringBoot打包發(fā)布到linux上(centos 7)的步驟
這篇文章主要介紹了SpringBoot打包發(fā)布到linux上(centos 7)的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-12-12
Java多線程工具CompletableFuture的使用教程
CompletableFuture實(shí)現(xiàn)了CompletionStage接口和Future接口,前者是對(duì)后者的一個(gè)擴(kuò)展,增加了異步回調(diào)、流式處理、多個(gè)Future組合處理的能力。本文就來詳細(xì)講講CompletableFuture的使用方式,需要的可以參考一下2022-08-08
在Spring Boot2中使用CompletableFuture的方法教程
這篇文章主要給大家介紹了關(guān)于在Spring Boot2中使用CompletableFuture的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2019-01-01

