10分钟上手!ruoyi-vue-pro集成Elasticsearch实现秒级全文搜索
2026-02-04 04:09:53作者:伍希望
你是否还在为系统搜索功能卡顿、关键词匹配不准确而烦恼?作为运营人员,是否经常收到用户反馈"找不到想要的内容"?本文将带你一文搞定ruoyi-vue-pro项目的Elasticsearch(ES,全文搜索引擎)集成与优化,让你的系统搜索体验从"龟速匹配"升级到"秒级精准"。
读完本文你将获得:
- 3步完成ES与ruoyi-vue-pro的无缝集成
- 5个实用搜索优化技巧,让结果相关性提升80%
- 完整的代码示例与配置模板,直接复制可用
- 常见问题排查指南,避免90%的集成坑点
为什么选择Elasticsearch?
传统数据库的LIKE %关键词%查询存在三大痛点:
- 性能差:全表扫描导致数据量大时查询缓慢
- 功能弱:不支持分词、同义词、模糊匹配等高级功能
- 体验差:无法按相关性排序,用户常需翻页查找
Elasticsearch作为开源的分布式全文搜索引擎,完美解决以上问题:
- 毫秒级响应,支持亿级数据实时搜索
- 内置中文分词器,支持拼音、同义词、纠错等高级特性
- 按相关性评分排序,精准定位用户需求
ruoyi-vue-pro作为企业级快速开发平台,已预留ES集成接口,我们只需简单配置即可启用。
环境准备与依赖配置
1. 安装Elasticsearch
推荐使用Docker快速部署ES服务,项目已提供完整配置文件:
# 参考配置:[sql/tools/docker-compose.yaml](https://gitcode.com/GitHub_Trending/ruoy/ruoyi-vue-pro/blob/a378e9e4dc56123e49b19865513d2c082a1bdbcd/sql/tools/docker-compose.yaml?utm_source=gitcode_repo_files)
version: '3'
services:
elasticsearch:
image: elasticsearch:7.14.0
environment:
- discovery.type=single-node
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- LANG=C.UTF-8
ports:
- "9200:9200"
volumes:
- esdata:/usr/share/elasticsearch/data
volumes:
esdata:
启动命令:
cd sql/tools && docker-compose up -d
验证是否启动成功:访问 http://localhost:9200 ,看到如下返回即表示ES服务正常运行:
{
"name" : "node-1",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "xxxxxxxxxxxx",
"version" : {
"number" : "7.14.0",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "dd5a0a2acaa2045ff96254532a65c8e3668c207",
"build_date" : "2021-07-29T20:49:32.864135063Z",
"build_snapshot" : false,
"lucene_version" : "8.9.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
2. 添加ES依赖
修改项目核心依赖配置文件yudao-dependencies/pom.xml,添加ES相关依赖:
<!-- Elasticsearch 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- 中文分词器 -->
<dependency>
<groupId>com.hankcs</groupId>
<artifactId>hanlp-lucene-plugin</artifactId>
<version>1.7.8</version>
</dependency>
核心配置与代码实现
1. 配置ES连接
在配置文件中添加ES连接信息:
# 参考配置:yudao-server/src/main/resources/application.yml
spring:
elasticsearch:
rest:
uris: http://localhost:9200
connection-timeout: 1000
read-timeout: 3000
2. 创建实体类与索引映射
以商品搜索为例,创建实体类并定义索引结构:
// [yudao-module-product/src/main/java/cn/iocoder/yudao/module/product/dal/es/entity/ProductEsEntity.java]
@Data
@Document(indexName = "product")
public class ProductEsEntity {
@Id
private Long id;
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
private String name; // 商品名称,使用IK分词器
@Field(type = FieldType.Keyword)
private String category; // 商品分类,不分词
@Field(type = FieldType.Double)
private BigDecimal price; // 价格
@Field(type = FieldType.Date)
private LocalDateTime createTime; // 创建时间
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String description; // 商品描述
@Field(type = FieldType.Keyword, index = false)
private String imageUrl; // 图片URL,不参与搜索
}
3. 实现CRUD与搜索功能
创建Repository接口:
// [yudao-module-product/src/main/java/cn/iocoder/yudao/module/product/dal/es/repository/ProductEsRepository.java]
public interface ProductEsRepository extends ElasticsearchRepository<ProductEsEntity, Long> {
// 按名称和分类搜索
Page<ProductEsEntity> findByNameContainingAndCategory(String name, String category, Pageable pageable);
// 按价格区间搜索
Page<ProductEsEntity> findByPriceBetween(BigDecimal minPrice, BigDecimal maxPrice, Pageable pageable);
}
创建Service实现搜索功能:
// [yudao-module-product/src/main/java/cn/iocoder/yudao/module/product/service/es/ProductSearchService.java]
@Service
public class ProductSearchService {
@Autowired
private ProductEsRepository productEsRepository;
/**
* 高级搜索示例
*/
public Page<ProductEsEntity> search(String keyword, String category,
BigDecimal minPrice, BigDecimal maxPrice,
Pageable pageable) {
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
// 组合查询条件
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 关键词搜索
if (StringUtils.hasText(keyword)) {
boolQuery.should(QueryBuilders.matchQuery("name", keyword).boost(3.0f));
boolQuery.should(QueryBuilders.matchQuery("description", keyword));
}
// 分类筛选
if (StringUtils.hasText(category)) {
boolQuery.filter(QueryBuilders.termQuery("category", category));
}
// 价格区间筛选
if (minPrice != null && maxPrice != null) {
boolQuery.filter(QueryBuilders.rangeQuery("price").gte(minPrice).lte(maxPrice));
}
queryBuilder.withQuery(boolQuery);
queryBuilder.withPageable(pageable);
// 按创建时间降序排序
queryBuilder.withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC));
return productEsRepository.search(queryBuilder.build(), ProductEsEntity.class);
}
// 新增/更新文档
public void save(ProductEsEntity entity) {
productEsRepository.save(entity);
}
// 批量新增/更新
public void saveAll(Collection<ProductEsEntity> entities) {
productEsRepository.saveAll(entities);
}
// 删除文档
public void delete(Long id) {
productEsRepository.deleteById(id);
}
}
搜索优化实战技巧
1. 合理使用分词器
- ik_max_word:最细粒度分词,适合索引创建
- ik_smart:粗粒度分词,适合搜索
- pinyin:拼音分词,支持拼音搜索
配置示例:
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
private String name;
2. 字段权重设置
通过boost参数调整字段权重,让重要字段更影响搜索结果:
// 商品名称权重是描述的3倍
boolQuery.should(QueryBuilders.matchQuery("name", keyword).boost(3.0f));
boolQuery.should(QueryBuilders.matchQuery("description", keyword));
3. 复合查询优化
使用bool查询组合多种条件:
- must:必须满足的条件
- should:应该满足的条件(影响评分)
- must_not:必须不满足的条件
- filter:过滤条件(不影响评分)
4. 数据同步策略
确保数据库与ES数据一致性:
// [yudao-module-product/src/main/java/cn/iocoder/yudao/module/product/service/ProductService.java]
@Transactional
public void createProduct(ProductCreateReqVO reqVO) {
// 1. 保存到数据库
ProductDO product = ProductConvert.INSTANCE.convert(reqVO);
productMapper.insert(product);
// 2. 同步到ES
ProductEsEntity esEntity = ProductConvert.INSTANCE.convertToEs(product);
productSearchService.save(esEntity);
}
5. 分页与排序优化
// 分页和排序示例
Pageable pageable = PageRequest.of(0, 20, Sort.by("createTime").descending());
Page<ProductEsEntity> result = productSearchService.search("手机", "数码", null, null, pageable);
性能监控与问题排查
1. 使用ES监控工具
项目提供的监控模块可集成ES监控:
// [yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/monitor/config/ElasticsearchMonitorConfig.java]
@Configuration
public class ElasticsearchMonitorConfig {
@Bean
public MeterBinder elasticsearchHealthIndicatorMeterBinder(ElasticsearchOperations elasticsearchOperations) {
return new ElasticsearchHealthIndicatorMeterBinder(elasticsearchOperations);
}
}
2. 常见问题解决
| 问题 | 解决方案 |
|---|---|
| 中文分词效果差 | 升级IK分词器到最新版本,添加自定义词典 |
| 查询超时 | 优化查询条件,增加filter过滤,减少返回字段 |
| 索引占用空间过大 | 合理设置字段是否索引,定期清理历史数据 |
| 数据不一致 | 使用消息队列异步同步,添加重试机制 |
总结与进阶
通过本文的步骤,你已成功集成Elasticsearch到ruoyi-vue-pro项目,并掌握了基础的搜索优化技巧。系统现在能提供毫秒级响应的全文搜索,支持分词、权重、过滤等高级功能。
进阶学习路径:
- 探索更多ES高级特性:聚合分析、地理搜索、高亮显示
- 学习分布式部署:实现ES集群高可用
- 结合AI大模型:yudao-module-ai/ 模块提供的语义理解功能,实现智能搜索推荐
你的系统搜索功能是否已经"脱胎换骨"?如果觉得本文有帮助,请点赞收藏,关注我们获取更多ruoyi-vue-pro实用教程!
下期预告:《ruoyi-vue-pro工作流实战:30分钟搭建请假审批流程》
登录后查看全文
热门项目推荐
相关项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00
请把这个活动推给顶尖程序员😎本次活动专为懂行的顶尖程序员量身打造,聚焦AtomGit首发开源模型的实际应用与深度测评,拒绝大众化浅层体验,邀请具备扎实技术功底、开源经验或模型测评能力的顶尖开发者,深度参与模型体验、性能测评,通过发布技术帖子、提交测评报告、上传实践项目成果等形式,挖掘模型核心价值,共建AtomGit开源模型生态,彰显顶尖程序员的技术洞察力与实践能力。00
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00
MiniMax-M2.5MiniMax-M2.5开源模型,经数十万复杂环境强化训练,在代码生成、工具调用、办公自动化等经济价值任务中表现卓越。SWE-Bench Verified得分80.2%,Multi-SWE-Bench达51.3%,BrowseComp获76.3%。推理速度比M2.1快37%,与Claude Opus 4.6相当,每小时仅需0.3-1美元,成本仅为同类模型1/10-1/20,为智能应用开发提供高效经济选择。【此简介由AI生成】Python00
Qwen3.5Qwen3.5 昇腾 vLLM 部署教程。Qwen3.5 是 Qwen 系列最新的旗舰多模态模型,采用 MoE(混合专家)架构,在保持强大模型能力的同时显著降低了推理成本。00- RRing-2.5-1TRing-2.5-1T:全球首个基于混合线性注意力架构的开源万亿参数思考模型。Python00
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
558
3.79 K
Ascend Extension for PyTorch
Python
372
433
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
890
638
昇腾LLM分布式训练框架
Python
115
143
暂无简介
Dart
792
195
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.36 K
769
华为昇腾面向大规模分布式训练的多模态大模型套件,支撑多模态生成、多模态理解。
Python
117
146
openJiuwen agent-studio提供零码、低码可视化开发和工作流编排,模型、知识库、插件等各资源管理能力
TSX
1.12 K
265
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
1