Release Date: 2025-10-26
解决向量检索结果缺乏多样性的问题。
// 哈希去重
xb.Of(&CodeVector{}).
VectorSearch("embedding", vec, 20).
WithHashDiversity("semantic_hash").
Build()
// 最小距离
xb.Of(&CodeVector{}).
VectorSearch("embedding", vec, 20).
WithMinDistance(0.3).
Build()
// MMR 算法
xb.Of(&CodeVector{}).
VectorSearch("embedding", vec, 20).
WithMMR(0.5).
Build()
三种多样性策略:
DiversityByHash - 基于语义哈希去重DiversityByDistance - 基于向量距离去重DiversityByMMR - MMR 算法(平衡相关性和多样性)生成 Qdrant 搜索 JSON,支持完整的混合查询。
built := xb.Of(&CodeVector{}).
Eq("language", "golang").
Gt("quality_score", 0.8).
VectorSearch("embedding", vec, 20).
Build()
// 生成 Qdrant JSON
json, err := built.ToQdrantJSON()
生成的 JSON:
{
"vector": [0.1, 0.2, 0.3],
"limit": 20,
"filter": {
"must": [
{"key": "language", "match": {"value": "golang"}},
{"key": "quality_score", "range": {"gt": 0.8}}
]
},
"with_payload": true,
"params": {"hnsw_ef": 128}
}
支持的查询映射:
Eq() → match.valueIn() → match.anyGt(), Gte(), Lt(), Lte() → rangeLIKE)核心特性:一份代码,多种后端
// 同一个 Builder
builder := xb.Of(&CodeVector{}).
VectorSearch("embedding", vec, 20).
WithHashDiversity("semantic_hash") // 多样性参数
built := builder.Build()
// PostgreSQL: 自动忽略多样性 ✅
sql, args := built.SqlOfVectorSearch()
// SQL: ... LIMIT 20 (不是 100)
// Qdrant: 应用多样性 ✅
json, _ := built.ToQdrantJSON()
// limit: 100 (20 * 5 倍过度获取)
不支持的功能自动忽略,不报错!
| API | 说明 |
|---|---|
WithDiversity(strategy, params...) |
通用多样性方法 |
WithHashDiversity(hashField) |
哈希去重 |
WithMinDistance(minDistance) |
最小距离 |
WithMMR(lambda) |
MMR 算法 |
| API | 说明 |
|---|---|
ToQdrantJSON() |
生成 JSON 字符串 |
ToQdrantRequest() |
生成请求结构体 |
// 多样性策略
type DiversityStrategy string
const (
DiversityByHash DiversityStrategy = "hash"
DiversityByDistance DiversityStrategy = "distance"
DiversityByMMR DiversityStrategy = "mmr"
)
// 多样性参数
type DiversityParams struct {
Enabled bool
Strategy DiversityStrategy
HashField string
MinDistance float32
Lambda float32
OverFetchFactor int
}
// Qdrant 请求结构
type QdrantSearchRequest struct {
Vector []float32
Limit int
Filter *QdrantFilter
WithPayload interface{}
Params *QdrantSearchParams
}
新增测试文件:
qdrant_test.go - Qdrant JSON 生成测试(9 个测试,全部通过)qdrant_nil_filter_test.go - nil/0 过滤验证empty_or_and_test.go - 空 OR/AND 过滤测试all_filtering_test.go - 综合过滤机制测试所有测试通过 ✅
VECTOR_DIVERSITY_QDRANT.md - 向量多样性和 Qdrant 使用指南QDRANT_NIL_FILTER_AND_JOIN.md - nil/0 过滤和 JOIN 查询说明EMPTY_OR_AND_FILTERING.md - 空 OR/AND 过滤机制ALL_FILTERING_MECHANISMS.md - 完整的过滤机制文档(9 层过滤)WHY_QDRANT.md - 为什么选择 Qdrant详细文档:ALL_FILTERING_MECHANISMS.md
| 过滤类型 | 位置 | 被过滤的值 |
|---|---|---|
| 单个条件 | doGLE() |
nil, 0, "" |
| IN 条件 | doIn() |
nil, 0, "", 空数组 |
| LIKE 条件 | Like() |
"" |
| 空 OR/AND | orAndSub() |
空子条件 |
| OR() 连接符 | orAnd() |
空条件,连续 OR |
| Bool 条件 | Bool() |
false |
| Select 字段 | Select() |
"" |
| GroupBy | GroupBy() |
"" |
| Agg 函数 | Agg() |
"" |
用户无需手动判断,框架自动处理所有边界情况!
JSON 构建后过滤(传统方式):
构建完整对象 → 检查 → 过滤 → 重新构建
❌ 多次遍历,效率低
Builder 构建时过滤(xb 方式):
构建即过滤 → 直接转换
✅ 一次遍历,高效
✅ 代码简洁 80%
✅ AI 友好
完全向后兼容 v0.8.1
// v0.8.1 代码(不变)
xb.Of(&CodeVector{}).
VectorSearch("embedding", vec, 20).
Build()
// v0.9.0 新功能(可选)
xb.Of(&CodeVector{}).
VectorSearch("embedding", vec, 20).
WithHashDiversity("semantic_hash"). // ⭐ 新增
Build().
ToQdrantJSON() // ⭐ 新增
// 问题:返回 20 个几乎重复的登录代码
// 解决:基于语义哈希去重
built := xb.Of(&CodeVector{}).
Eq("language", "golang").
VectorSearch("embedding", vec, 20).
WithHashDiversity("semantic_hash").
Build()
// PostgreSQL: 正常查询
sql, args := built.SqlOfVectorSearch()
// Qdrant: 过度获取 100 个,应用层去重到 20 个
json, _ := built.ToQdrantJSON()
// 推荐相关但多样化的文章
xb.Of(&Article{}).
Ne("id", currentArticle.Id).
VectorSearch("embedding", currentArticle.Embedding, 10).
WithMMR(0.6). // 60% 相关性,40% 多样性
Build()
// Step 1: Qdrant 向量检索
qdrantResults := qdrantClient.Search(built.ToQdrantJSON())
// Step 2: PostgreSQL 关系查询
codeIDs := extractIDs(qdrantResults)
results := xb.Of(&CodeWithAuthor{}).
In("code.id", codeIDs...).
Build().
Query()
无需修改任何代码! 完全向后兼容。
# 更新依赖
go get github.com/fndome/xb@v0.9.0
go mod tidy
可选:使用新功能
// 添加多样性(可选)
builder.WithHashDiversity("semantic_hash")
// 生成 Qdrant JSON(可选)
json, _ := built.ToQdrantJSON()
Vector 类型在 INSERT 和 UPDATE 时被提前 JSON Marshal 的问题Vector 正确调用 driver.Valuer 接口本版本由 AI (Claude) 和人类 (sim-wangyan) 协作完成。
开发模式:
AI 参与度:
xb v0.9.0 - AI-First ORM for the Modern Era 🚀
一个 API,多种后端(PostgreSQL, Qdrant, …)
智能过滤,简洁代码,可靠运行 ✨