Skip to content

Commit 83506e8

Browse files
committed
Merge branch 'feat/add-search-radius' into 'main' (merge request !70)
feat: add search radius
2 parents 92d7770 + fd3df61 commit 83506e8

File tree

6 files changed

+32
-10
lines changed

6 files changed

+32
-10
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## v1.5.4
4+
* feat: support search radius for searching in collection
5+
36
## v1.5.3
47
* fix: optimize warning message for modifing vector indexes interface
58

tcvectordb/api/document/api.go

+1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ type SearchCond struct {
178178
Vectors [][]float32 `json:"vectors,omitempty"`
179179
Filter string `json:"filter,omitempty"`
180180
EmbeddingItems []string `json:"embeddingItems,omitempty"`
181+
Radius *float32 `json:"radius,omitempty"` // 距离阈值,范围搜索时有效
181182
}
182183

183184
type SearchParams struct {

tcvectordb/base_document.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,17 @@ func (i *implementerDocument) Query(ctx context.Context, documentIds []string, p
181181
// If RetrieveVector is true, the vector values will be returned.
182182
// - OutputFields: (Optional) Return columns specified by the list of column names.
183183
// - Limit: (Required) Limit the number of documents returned (default to 1).
184+
// - Radius: (Optional) Specifies the radius range for similarity retrieval.
185+
// IP: return when score >= radius, value range (-∞, +∞).
186+
// COSINE: return when score >= radius, value range [-1, 1].
187+
// L2: return when score <= radius, value range [0, +∞).
184188
type SearchDocumentParams struct {
185189
Filter *Filter
186190
Params *SearchDocParams
187191
RetrieveVector bool
188192
OutputFields []string
189193
Limit int64
194+
Radius *float32
190195
}
191196

192197
// [SearchDocParams] holds the parameters for searching documents to a collection.
@@ -196,10 +201,10 @@ type SearchDocumentParams struct {
196201
// of vectors to be accessed. Valid range is [1, nlist], and nlist is defined by creating collection.
197202
// - Ef: (Optional) HNSW type index requires configuration parameter ef to specify the number
198203
// of vectors to be accessed (default to 10). Valid range is [1, 32768]
199-
// - Radius: (Optional) Specifies the radius range for similarity retrieval.
200204
type SearchDocParams struct {
201-
Nprobe uint32 `json:"nprobe,omitempty"` // 搜索时查找的聚类数量,使用索引默认值即可
202-
Ef uint32 `json:"ef,omitempty"` // HNSW
205+
Nprobe uint32 `json:"nprobe,omitempty"` // 搜索时查找的聚类数量,使用索引默认值即可
206+
Ef uint32 `json:"ef,omitempty"` // HNSW
207+
// Deprecated: Radius is deprecated and should not be used, which is supported in [SearchDocumentParams].
203208
Radius float32 `json:"radius,omitempty"` // 距离阈值,范围搜索时有效
204209
}
205210

@@ -694,11 +699,13 @@ func (i *implementerFlatDocument) search(ctx context.Context, databaseName, coll
694699
req.Search.OutputFields = param.OutputFields
695700
req.Search.Limit = param.Limit
696701

702+
req.Search.Params = new(document.SearchParams)
697703
if param.Params != nil {
698-
req.Search.Params = new(document.SearchParams)
699704
req.Search.Params.Nprobe = param.Params.Nprobe
700705
req.Search.Params.Ef = param.Params.Ef
701-
req.Search.Params.Radius = param.Params.Radius
706+
}
707+
if param.Radius != nil {
708+
req.Search.Radius = param.Radius
702709
}
703710
}
704711

tcvectordb/rpc_base_document.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,15 @@ func (r *rpcImplementerFlatDocument) search(ctx context.Context, databaseName, c
689689
req.Search.Params = &olama.SearchParams{
690690
Nprobe: param.Params.Nprobe,
691691
Ef: param.Params.Ef,
692-
Radius: param.Params.Radius,
693692
}
693+
694+
}
695+
if param.Radius != nil {
696+
if req.Search.Params == nil {
697+
req.Search.Params = new(olama.SearchParams)
698+
}
699+
req.Search.Range = true
700+
req.Search.Params.Radius = *param.Radius
694701
}
695702
}
696703
res, err := r.rpcClient.Search(ctx, req)

tcvectordb/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818

1919
package tcvectordb
2020

21-
const SDKVersion = "v1.5.3"
21+
const SDKVersion = "v1.5.4"

test/normal_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,17 @@ func TestQuery(t *testing.T) {
298298
func TestSearch(t *testing.T) {
299299
col := cli.Database(database).Collection(collectionName)
300300

301+
radius := float32(0.76)
301302
searchRes, err := col.Search(ctx, [][]float32{
302-
{0.3123, 0.43, 0.213},
303+
//{0.3123, 0.43, 0.213},
303304
{0.233, 0.12, 0.97},
304305
}, &tcvectordb.SearchDocumentParams{
305-
Params: &tcvectordb.SearchDocParams{Ef: 100},
306+
Params: &tcvectordb.SearchDocParams{
307+
Ef: 100,
308+
},
309+
Radius: &radius,
306310
RetrieveVector: false,
307-
Limit: 10,
311+
Limit: 2,
308312
})
309313
printErr(err)
310314
log.Printf("search by vector-----------------")

0 commit comments

Comments
 (0)