fix(sqlite): treat zero vector threshold as no filter

Match Milvus/Tencent VectorDB and CLI semantics where threshold 0
means no score floor, and add a regression test for anti-correlated
vectors that would otherwise be dropped.
main
wizardchen 2026-07-31 10:56:29 +08:00 committed by lyingbug
parent ca19596874
commit 3afdc796b0
2 changed files with 22 additions and 1 deletions

View File

@ -434,7 +434,7 @@ func (r *sqliteRepository) vectorRetrieve(ctx context.Context, params types.Retr
for i, v := range rows {
// cosine distance = 1 - cosine_similarity
score := 1 - v.Distance
if score < params.Threshold {
if params.Threshold > 0 && score < params.Threshold {
continue
}

View File

@ -115,6 +115,27 @@ func TestVectorRetrieveFiltersBeforeTopK(t *testing.T) {
}
}
func TestVectorRetrieveZeroThresholdDoesNotFilter(t *testing.T) {
repository := newSQLiteRetrieverTestRepository(t)
saveSQLiteTestVector(t, repository,
sqliteTestIndex("anti-correlated", "kb-target", "knowledge-target", "tag-target", true),
[]float32{-1, 0},
)
results, err := repository.vectorRetrieve(context.Background(), types.RetrieveParams{
Embedding: []float32{1, 0},
KnowledgeBaseIDs: []string{"kb-target"},
TopK: 1,
Threshold: 0,
RetrieverType: types.VectorRetrieverType,
})
require.NoError(t, err)
require.Len(t, results, 1)
require.Len(t, results[0].Results, 1)
assert.Equal(t, "anti-correlated", results[0].Results[0].ChunkID)
assert.Less(t, results[0].Results[0].Score, 0.0)
}
func TestVectorRetrieveAppliesSimilarityThreshold(t *testing.T) {
repository := newSQLiteRetrieverTestRepository(t)
saveSQLiteTestVector(t, repository,