Skip to content

Commit

Permalink
redisvector: fix score threshold option (#1003)
Browse files Browse the repository at this point in the history
Vector range queries expect a radius rather than a score threshold.
  • Loading branch information
acrmp authored Sep 13, 2024
1 parent 673c598 commit 47d2d99
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions vectorstores/redisvector/index_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ func (s IndexVectorSearch) AsCommand() []string {

const vectorField = "vector"
const vectorFieldAs = defaultDistanceFieldKey
const disThresholdFiled = "distance_threshold"
const disThresholdField = "distance_threshold"
const vectorKey = defaultContentVectorFieldKey
params := []string{vectorField, VectorString32(s.vector)}

if s.scoreThreshold > 0 && s.scoreThreshold < 1 {
// Range search
// "@content_vector:[VECTOR_RANGE $distance_threshold $vector]=>{$yield_distance_as: distance}"
filter := fmt.Sprintf("@%s:[VECTOR_RANGE $%s $%s]=>{$yield_distance_as: %s}", vectorKey, disThresholdFiled, vectorField, vectorFieldAs)
filter := fmt.Sprintf("@%s:[VECTOR_RANGE $%s $%s]=>{$yield_distance_as: %s}", vectorKey, disThresholdField, vectorField, vectorFieldAs)
if len(s.preFilters) > 0 {
filter = fmt.Sprintf("(%s) %s", s.preFilters, filter)
}
cmd = append(cmd, filter)
params = append(params, disThresholdFiled, strconv.FormatFloat(float64(s.scoreThreshold), 'f', -1, 32))
params = append(params, disThresholdField, strconv.FormatFloat(float64(1.0-s.scoreThreshold), 'f', -1, 32))
} else {
// KNN search
// "(*)=>[KNN n @content_vector $vector AS distance]"
Expand Down
4 changes: 2 additions & 2 deletions vectorstores/redisvector/redis_vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ func TestSimilaritySearch(t *testing.T) {
assert.Len(t, docs[0].Metadata, 3)

// search with score threshold
docs, err = store.SimilaritySearch(ctx, "Tokyo", 2,
vectorstores.WithScoreThreshold(0.5),
docs, err = store.SimilaritySearch(ctx, "Tokyo", 10,
vectorstores.WithScoreThreshold(0.8),
)
require.NoError(t, err)
assert.Len(t, docs, 2)
Expand Down

0 comments on commit 47d2d99

Please sign in to comment.