4cce6f2e99
为 RetrieveEngine 体系新增 Doris 后端,与现有 Qdrant/Milvus/Weaviate
等保持完整能力对齐:向量检索、关键词检索、健康检查、环境变量与多实例
DB 配置、前端类型注册、单元测试、Docker Compose 模板。
实现要点:
- 协议分工:主链路用 MySQL 协议(database/sql + go-sql-driver/mysql)
做 DDL / 查询 / 删除;批量更新走 Stream Load HTTP API,并启用
partial_update=true、merge_type=APPEND,自动按 1MiB 切分批次并处理
307 重定向。
- 表结构:UNIQUE KEY(id) + enable_unique_key_merge_on_write=true 以
支持 upsert/部分列更新;按维度分表(<base>_<dim>),每张表上建
HNSW ANN 索引(metric_type=cosine_distance)和 INVERTED 索引
(parser=chinese)。
- 分数语义:使用 cosine_distance_approximate,再以 1 - dist 转换为
"越大越相似",与现有 KVHybridRetrieveEngine 约定一致。
- 异步索引:ANN 索引为后台构建,ensureTable 通过轮询 SHOW INDEX 等
待索引就绪后再放行写入,避免首次检索召回为空。
- ARRAY<FLOAT> 序列化:go-sql-driver/mysql 不支持数组占位符,
embeddingLiteral 将 []float32 转成 SQL 字面量字符串再拼接。
新增文件:
- internal/application/repository/retriever/doris/{structs,schema,
query,repository,streamload,repository_test}.go
- scripts/e2e-doris.sh:E2E 验证清单
- docs/wiki/集成扩展/Doris改动与上游同步.md:fork-and-rebase 工作流
与改动清单
修改文件(接线 + 文档):
- internal/types/{retriever,tenant,vectorstore}.go:新增
DorisRetrieverEngineType、env 解析、表单 schema 与索引参数校验
- internal/container/{container,engine_factory}.go:环境变量驱动
与 VectorStore 配置驱动两条路径都支持 Doris
- internal/application/service/vectorstore{,_healthcheck}.go:连接
校验 + Ping/Version 健康检查
- docker-compose.yml:新增 doris-fe / doris-be 服务(profile=doris)
- .env.example:DORIS_* 环境变量与示例
- docs/{使用其他向量数据库,wiki/集成扩展/集成向量数据库}.md:
使用说明与索引/分数行为说明
依赖:go.mod/go.sum 新增 github.com/go-sql-driver/mysql(运行时)和
github.com/DATA-DOG/go-sqlmock(测试)。
测试:repository 层 SQL 形状、Stream Load HTTP 行为、whereBuilder
逻辑、embeddingLiteral 往返、健康检查错误路径均有单测覆盖。
64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package doris
|
||
|
||
import (
|
||
"database/sql"
|
||
"net/http"
|
||
"sync"
|
||
)
|
||
|
||
// dorisRepository 是 Apache Doris 4.1 的检索引擎仓储实现。
|
||
//
|
||
// 通信通道:
|
||
// - 读写主链路:MySQL 协议(database/sql + go-sql-driver/mysql),FE 默认 9030 端口。
|
||
// - Stream Load:HTTP(FE 默认 8030 端口),用于 BatchUpdate* 的 partial update。
|
||
//
|
||
// 表结构按维度分表:<tableBaseName>_<dim>,UNIQUE KEY(id),
|
||
// 同时建立倒排索引(filter / 全文)+ ANN(HNSW) 索引。
|
||
//
|
||
// 与 Qdrant/Milvus/Weaviate 一样,initializedTables 缓存"已确保存在"的维度,
|
||
// 避免每次写入都打 SHOW TABLES。
|
||
type dorisRepository struct {
|
||
db *sql.DB
|
||
|
||
httpClient *http.Client
|
||
// fe HTTP base,例如 "http://doris-fe:8030"。Stream Load 路径
|
||
// 由 streamLoadURL(table) 拼接:<feHTTPBase>/api/<database>/<table>/_stream_load。
|
||
feHTTPBase string
|
||
|
||
username string
|
||
password string
|
||
database string
|
||
|
||
tableBaseName string
|
||
bucketsNum int // 0 -> default 10
|
||
replicationNum int // 0 -> default 1
|
||
|
||
// 已经确保过 ensureTable 的维度集合:dim -> true。
|
||
initializedTables sync.Map
|
||
}
|
||
|
||
// DorisVectorEmbedding 是落到 Doris 表里的一行的领域模型。
|
||
//
|
||
// 字段顺序与 schema.go 中的 INSERT 列序保持一致,
|
||
// 调整时需要同时更新 createInsert 与 columns。
|
||
type DorisVectorEmbedding struct {
|
||
ID string
|
||
Content string
|
||
SourceID string
|
||
SourceType int
|
||
ChunkID string
|
||
KnowledgeID string
|
||
KnowledgeBaseID string
|
||
TagID string
|
||
IsEnabled bool
|
||
Embedding []float32
|
||
}
|
||
|
||
// DorisVectorEmbeddingWithScore 是检索结果的领域模型,
|
||
// Score 在向量检索时存储 (1 - cosine_distance_approximate),
|
||
// 在关键词检索时统一赋 1.0(与 Qdrant 行为一致)。
|
||
type DorisVectorEmbeddingWithScore struct {
|
||
DorisVectorEmbedding
|
||
Score float64
|
||
}
|