[WIP] Optimize vector similarity calculation in Java application layer#5
Merged
Merged
Conversation
IfeyChan702
marked this pull request as ready for review
February 10, 2026 09:54
Owner
|
5 |
Copilot stopped work on behalf of
IfeyChan702 due to an error
February 10, 2026 09:55
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Vector Calculation Optimization - Plan A Implementation
Phase 1: Code Assessment and Planning
Phase 2: Enhance Java Utilities (In Progress)
Phase 3: Modify RAG Service
Phase 4: Simplify Database Layer
Phase 5: Configuration Updates
Phase 6: Testing
Phase 7: Documentation
Phase 8: Final Verification
Original prompt
MyEnglishNote 向量计算优化方案 A:Java 应用层优化 + MySQL 保持不变
目标
优化向量相似度计算,将其从 MySQL 存储函数迁移到 Java 应用层,性能提升 5-10 倍,同时完全保留 MySQL 数据库和 RuoYi 系统。
为什么是方案 A?
实现清单
第一部分:Java 代码优化 (~2000+ 行)
1. VectorUtil.java(增强的向量工具类)
位置:
ruoyi-system/src/main/java/com/ruoyi/system/util/VectorUtil.java功能:
cosineSimilarity(List<Double> vec1, List<Double> vec2)- 余弦相似度(核心)euclideanDistance(List<Double> vec1, List<Double> vec2)- 欧氏距离(备选)manhattanDistance(List<Double> vec1, List<Double> vec2)- 曼哈顿距离(备选)normalizeVector(List<Double> vector)- 向量归一化vectorDot(List<Double> v1, List<Double> v2)- 向量点积vectorMagnitude(List<Double> vector)- 向量模长batchCosineSimilarity(List<Double> queryVector, List<List<Double>> dbVectors)- 批量并行计算特点:
2. EmbeddingUtil.java(向量序列化工具)
位置:
ruoyi-system/src/main/java/com/ruoyi/system/util/EmbeddingUtil.java功能:
embeddingToJson(List<Double> embedding)- 向量转 JSON 存储jsonToEmbedding(String json)- JSON 转向量对象jsonToEmbeddingList(String json)- JSON 转向量列表validateEmbedding(List<Double> embedding)- 验证向量有效性embeddingDimension()- 获取向量维度(1536)特点:
3. RAGServiceImpl.java(改进的 RAG 服务)
位置:
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/RAGServiceImpl.java改进点:
核心逻辑:
4. NoteMapper.xml(简化查询)
改进点:
5. 单元测试
VectorUtilTest.java:testCosineSimilarity()- 基本功能testEuclideanDistance()- 距离计算testNormalizeVector()- 向量归一化testBatchCosineSimilarity()- 批量并行计算testPerformance()- 性能基准测试(1000 个 1536 维向量)第二部分:数据库脚本简化 (~150 行)
1. sql/mysql/rag_init.sql(简化版本)
改进点:
表结构保持不变:
第三部分:配置更新
1. application-rag.yml(更新)
新增配置:
第四部分:文档
1. docs/OPTIMIZATION_GUIDE.md(优化指南)
内容:
2. docs/MIGRATION_PREPARATION.md(迁移准备指南)
内容:
文件清单
Java 源代码
ruoyi-system/src/main/java/com/ruoyi/system/util/VectorUtil.java- 向量计算工具ruoyi-system/src/main/java/com/ruoyi/system/util/EmbeddingUtil.java- 向量序列化工具ruoyi-system/src/main/java/com/ruoyi/system/service/impl/RAGServiceImpl.java- RAG 服务改进版ruoyi-system/src/main/java/com/ruoyi/system/mapper/NoteMapper.java- Mapper 优化版测试代码
ruoyi-system/src/test/java/com/ruoyi/system/util/VectorUtilTest.java- 向量工具测试ruoyi-system/src/test/java/com/ruoyi/system/service/RAGServiceTest.java- RAG 服务测试数据库脚本
sql/mysql/rag_init.sql- MySQL 初始化脚本(优化版,删除函数)配置文件
ruoyi-admin/src/main/resources/application-rag.yml- 更新配置XML 映射文件
ruoyi-system/src/main/resources/mapper/NoteMapper.xml- 简化版 SQL文档
docs/OPTIMIZATION_GUIDE.md- 优化指南docs/MIGRATION_PREPARATION.md- 迁移准备(为将来)性能对比
关键改进
实施步骤
技术细节
Java 代码特点
parallelStream()自动利用多核数据库兼容性
完成后你将拥有
✅ 性能提升 5-10 倍的应用
✅ 完全兼容现有 MySQL 和 RuoYi
✅ 清晰易维护的代码结构
✅ 完整的单元测试覆盖
✅ 详细的优化文档
✅ 为将来迁移预留的设计空间
This pull request was created from Copilot chat.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.