SpringBoot集成Easy-Es的實(shí)戰(zhàn)操作指南
一、Easy ES 簡介
Easy ES(簡稱EE)是一款基于 Elasticsearch 官方 RestHighLevelClient 封裝的 ORM 框架,提供類似 MyBatis-Plus 的 API 設(shè)計,可以幫助開發(fā)者更簡單地集成和使用 Elasticsearch,讓操作 Elasticsearch 變得更加方便和高效,大大降低了 Elasticsearch 操作復(fù)雜度。
二、環(huán)境準(zhǔn)備
1. 依賴引入
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.17.19</version>
</dependency>
<dependency>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
2. 增加配置
# application.yml
easy-es:
enable: true
address: 127.0.0.1:9200
username: admin
password: 123456
# 可選配置
global-config:
print-dsl: true # 打印DSL語句
async-process-index-blocking: true # 自動托管索引
三、核心功能集成
1. 新增 Mapper
自定義一個 Mapper,并繼承 BaseEsMapper
public interface MyEsCollectionMapper extends BaseEsMapper<MyCollection> {
}
2. 指定索引
在 Collection 中,需要指定和 ES 交互的索引 key
@Data
@IndexName(value = "my_collection")
public class MyCollection extends BaseEntity {
/**
* 姓名
*/
private String name;
// 其他字段
}
3. 在啟動類上添加 ES 的 Mapper 掃描配置
@EsMapperScan("cn.feizhu.jgs.*.infrastructure.es.mapper")
4. 使用 Easy ES 進(jìn)行查詢
@Component
public class MyEsCollectionMapperTest {
@Resource
private MyEsCollectionMapper myEsCollectionMapper;
@Test
public void test(){
LambdaEsQueryWrapper<MyCollection> queryWrapper = new LambdaEsQueryWrapper<>();
queryWrapper.match(MyCollection::getName, "會飛的我")
.and(wrapper -> wrapper
.match(MyCollection::getIsDeleted, true));
EsPageInfo<MyCollection> results = myEsCollectionMapper.pageQuery(queryWrapper, 1, 10);
}
}
四、Easy ES 的用法
1.基礎(chǔ) CRUD 示例
@Service
public class ArticleService {
@Resource
private ArticleMapper articleMapper;
// 新增文檔
public Boolean addArticle(Article article) {
return articleMapper.insert(article) > 0;
}
// 條件查詢
public List<Article> searchByKeyword(String keyword) {
LambdaEsQueryWrapper<Article> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(Article::getContent, keyword);
return articleMapper.selectList(wrapper);
}
// 更新文檔
public Boolean updateAuthor(String id, String newAuthor) {
Article article = new Article();
article.setId(id);
article.setAuthor(newAuthor);
return articleMapper.updateById(article) > 0;
}
// 刪除文檔
public Boolean deleteArticle(String id) {
return articleMapper.deleteById(id) > 0;
}
}
2.分頁查詢
public PageInfo<Article> searchPage(String keyword, int pageNum, int pageSize) {
LambdaEsQueryWrapper<Article> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(Article::getTitle, keyword)
.orderByDesc(Article::getCreateTime);
return articleMapper.pageQuery(wrapper, pageNum, pageSize);
}
3. 復(fù)雜布爾查詢
public List<Article> complexQuery(String author, Date startDate) {
LambdaEsQueryWrapper<Article> wrapper = new LambdaEsQueryWrapper<>();
wrapper.eq(Article::getAuthor, author)
.ge(Article::getCreateTime, startDate)
.or()
.match(Article::getContent, "技術(shù)");
return articleMapper.selectList(wrapper);
}
4. 高亮顯示
public List<Article> searchWithHighlight(String keyword) {
LambdaEsQueryWrapper<Article> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(Article::getContent, keyword)
.highLight(Article::getContent,
"<em>", "</em>", 100);
return articleMapper.selectList(wrapper);
}
5. 開啟自動創(chuàng)建索引的兩種方式
1.通過配置文件開啟(推薦)
# application.yml
easy-es:
global-config:
async-process-index-blocking: true # 自動托管索引(包含自動創(chuàng)建)
2.通過代碼配置(動態(tài)啟用)
@Configuration
public class EsConfig {
@Bean
public GlobalConfig globalConfig() {
GlobalConfig config = new GlobalConfig();
config.setAsyncProcessIndexBlocking(true); // 開啟索引自動托管
return config;
}
}
五、注意事項
索引管理:開啟auto-create-index后,首次插入數(shù)據(jù)時會自動創(chuàng)建索引
字段映射:ES 字段類型需與 Java 類型匹配,避免類型轉(zhuǎn)換異常
分詞器配置:中文搜索建議使用 ik 分詞器,需提前安裝插件
版本兼容:確保 ES 服務(wù)版本與 Easy ES 兼容(推薦ES 7.x+)
六、總結(jié)
通過 Easy ES 框架,我們可以:
- 減少約 80% 的 ES 操作代碼量
- 使用熟悉的 MyBatis-Plus 風(fēng)格 API
- 支持自動索引托管等高級特性
- 保留原生 API 擴(kuò)展能力
到此這篇關(guān)于SpringBoot集成Easy-Es的實(shí)戰(zhàn)操作指南的文章就介紹到這了,更多相關(guān)SpringBoot集成Easy-Es內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java異步判斷線程池所有任務(wù)是否執(zhí)行完成的操作方法
這篇文章主要介紹了Java異步判斷線程池所有任務(wù)是否執(zhí)行完成的方法,在這個示例中,我使用了傳統(tǒng)的匿名內(nèi)部類來創(chuàng)建Callable任務(wù)(同時也提供了Lambda表達(dá)式的注釋),以便與各種Java版本兼容,需要的朋友可以參考下2024-07-07
SpringSecurity+jwt+redis基于數(shù)據(jù)庫登錄認(rèn)證的實(shí)現(xiàn)
本文主要介紹了SpringSecurity+jwt+redis基于數(shù)據(jù)庫登錄認(rèn)證的實(shí)現(xiàn),其中也涉及到自定義的過濾器和處理器,具有一定的參考價值,感興趣的可以了解一下2023-09-09
SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫遷移的實(shí)現(xiàn)示例
Flyway是一個數(shù)據(jù)庫遷移工具,它提供遷移歷史和回滾的功能,本文主要介紹了如何使用Flyway來管理Spring Boot應(yīng)用程序中的SQL數(shù)據(jù)庫架構(gòu),感興趣的可以了解一下2023-08-08
Mybatis-plus如何在xml中傳入自定義的SQL語句
這篇文章主要介紹了Mybatis-plus如何在xml中傳入自定義的SQL語句問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Java?InputStream實(shí)戰(zhàn)之輕松讀取操作文件流
在Java中,輸入輸出是非常重要的基礎(chǔ)功能,其中,InputStream是Java中的一個重要輸入流類,用于從輸入源讀取數(shù)據(jù),下面我們就來學(xué)習(xí)一下InputStream類的相關(guān)知識吧2023-10-10
使用springcloud+oauth2攜帶token去請求其他服務(wù)
這篇文章主要介紹了使用springcloud+oauth2攜帶token去請求其他服務(wù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

