尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

从0到1搭建全文检索系统:Easy-Es+Spring Boot实战案例

从0到1搭建全文检索系统:Easy-Es+Spring Boot实战案例 从0到1搭建全文检索系统Easy-EsSpring Boot实战案例在当今信息爆炸的时代快速高效地检索数据成为许多应用的核心需求。全文检索系统能够帮助用户从海量数据中迅速找到所需信息而Easy-Es作为一款基于ElasticSearch的低代码开发框架结合Spring Boot可以快速构建强大的全文检索功能。本文将带您一步步完成从环境搭建到实现基本CRUD操作的全过程让您轻松掌握全文检索系统的搭建技巧。一、什么是Easy-EsEasy-Es简称EE是Mybatis-Plus在Elastic Search的平替版基于ElasticSearch官方提供的RestHighLevelClient打造致力于简化开发、提高效率。如果您用过Mybatis-Plus那么基本可以零学习成本上手Easy-Es它不仅融合了Mybatis-Plus的易用性还融入了更多ElasticSearch独有的功能。二、准备工作在开始之前请确保您已满足以下环境要求拥有Java开发环境及相应IDE熟悉Spring Boot推荐版本2.5.x熟悉Maven已安装ElasticSearch推荐7.x版本本案例使用7.10.0版本了解ElasticSearch基本概念建议阅读避坑指南三、快速开始3.1 创建Spring Boot工程首先创建一个空的Spring Boot工程可以使用Spring Initializer快速初始化。3.2 添加依赖在pom.xml中添加Easy-Es的starter依赖dependency groupIdcn.easy-es/groupId artifactIdeasy-es-boot-starter/artifactId versionLatest Version/version /dependency提示Latest Version可通过官方渠道获取最新版本号3.3 配置Easy-Es在application.yml配置文件中添加Easy-Es相关配置easy-es: enable: true # 默认为true若为false则不启用本框架 address: 127.0.0.1:9200 # es的连接地址集群可用逗号隔开 username: elastic # 若无则可省略 password: WG7WVmuNMtM4GwNYkyWH # 若无则可省略在Spring Boot启动类中添加EsMapperScan注解扫描Mapper文件夹SpringBootApplication EsMapperScan(com.xpc.easyes.sample.mapper) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }四、编码实现4.1 编写实体类创建Document实体类使用Lombok简化代码Data public class Document { /** * es中的唯一id */ private String id; /** * 文档标题 */ private String title; /** * 文档内容 */ private String content; }提示String类型默认会被EE创建为keyword类型如需分词查询可通过注解配置字段类型为text并指定分词器。4.2 编写Mapper接口创建DocumentMapper接口继承BaseEsMapperpublic interface DocumentMapper extends BaseEsMapperDocument { }4.3 索引自动创建启动项目后Easy-Es会自动帮您创建索引相当于MySQL中的表索引托管成功后控制台会显示 Congratulations auto process index by Easy-Es is done !提示后续索引的更新、重建、数据迁移等工作默认都由EE自动完成您也可以通过配置关闭自动托管手动维护索引。五、CRUD操作实战5.1 新增数据Test public void testInsert() { Document document new Document(); document.setTitle(老汉); document.setContent(推*技术过硬); String id documentMapper.insert(document); System.out.println(id); }5.2 查询数据Test public void testSelect() { String title 老汉; LambdaEsQueryWrapperDocument wrapper new LambdaEsQueryWrapper(); wrapper.eq(Document::getTitle, title); Document document documentMapper.selectOne(wrapper); System.out.println(document); }5.3 更新数据Test public void testUpdate() { // case1: 已知id更新 String id krkvN30BUP1SGucenZQ9; Document document1 new Document(); document1.setId(id); document1.setTitle(隔壁老王); documentMapper.updateById(document1); // case2: 根据条件更新 LambdaEsUpdateWrapperDocument wrapper new LambdaEsUpdateWrapper(); wrapper.eq(Document::getTitle, 隔壁老王); Document document2 new Document(); document2.setTitle(隔壁老李); document2.setContent(推*技术过软); documentMapper.update(document2, wrapper); }5.4 删除数据Test public void testDelete() { LambdaEsQueryWrapperDocument wrapper new LambdaEsQueryWrapper(); wrapper.eq(Document::getTitle, 隔壁老李); int successCount documentMapper.delete(wrapper); System.out.println(successCount); }六、总结通过以上几个简单的步骤我们就实现了Document的索引创建及CRUD功能。集成Easy-Es非常简单只需引入starter工程并配置mapper扫描路径即可。Easy-Es的强大远不止这些功能它的API基本上覆盖了Elastic Search 90%以上的功能99%以上的常用功能您可以基于EE快速构建出各种复杂的检索系统。完整的代码示例可参考项目中的easy-es-sample目录更多高级功能请查阅官方文档。希望本文能帮助您快速上手Easy-Es搭建属于自己的全文检索系统创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表