【SpringBoot实战指南】集成Easy ES

发布于:2025-05-14 ⋅ 阅读:(15) ⋅ 点赞:(0)

一、Easy ES 简介

Easy ES(简称EE)是一款基于 Elasticsearch 官方 RestHighLevelClient 封装的 ORM 框架,提供类似 MyBatis-Plus 的 API 设计,可以帮助开发者更简单地集成和使用 Elasticsearch,让操作 Elasticsearch 变得更加方便和高效,大大降低了 Elasticsearch 操作复杂度。

二、环境准备

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 进行查询

@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.基础 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. 复杂布尔查询

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, "技术");
    
    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. 开启自动创建索引的两种方式

1.通过配置文件开启(推荐)

# application.yml
easy-es:
  global-config:
    async-process-index-blocking: true  # 自动托管索引(包含自动创建)

2.通过代码配置(动态启用)

@Configuration
public class EsConfig {
    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig config = new GlobalConfig();
        config.setAsyncProcessIndexBlocking(true); // 开启索引自动托管
        return config;
    }
}

五、注意事项

  1. 索引管理:开启auto-create-index后,首次插入数据时会自动创建索引
  2. 字段映射:ES 字段类型需与 Java 类型匹配,避免类型转换异常
  3. 分词器配置:中文搜索建议使用 ik 分词器,需提前安装插件
  4. 版本兼容:确保 ES 服务版本与 Easy ES 兼容(推荐ES 7.x+)

六、总结

通过 Easy ES 框架,我们可以:
● 减少约 80% 的 ES 操作代码量
● 使用熟悉的 MyBatis-Plus 风格 API
● 支持自动索引托管等高级特性
● 保留原生 API 扩展能力


网站公告

今日签到

点亮在社区的每一天
去签到