Skip to content

Commit

Permalink
use maxKNNScore
Browse files Browse the repository at this point in the history
  • Loading branch information
CascadingRadium committed Nov 21, 2023
1 parent 0ee7015 commit 671df18
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
9 changes: 5 additions & 4 deletions knn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package bleve
import (
"archive/zip"
"encoding/json"
"math"
"math/rand"
"testing"

Expand Down Expand Up @@ -355,8 +356,8 @@ func runKNNTest(t *testing.T, randomizeDocuments bool) {
numIndexPartitions: 1,
expectedResults: map[string]testResult{
"doc7": {
score: 2357.022603955158,
scoreBreakdown: []float64{0, 0, 7071.067811865475},
score: math.MaxFloat64,
scoreBreakdown: []float64{0, 0, math.MaxFloat64 / 3.0},
},
"doc29": {
score: 0.6774608026082964,
Expand Down Expand Up @@ -402,8 +403,8 @@ func runKNNTest(t *testing.T, randomizeDocuments bool) {
numIndexPartitions: 4,
expectedResults: map[string]testResult{
"doc7": {
score: 2357.022603955158,
scoreBreakdown: []float64{0, 0, 7071.067811865475},
score: math.MaxFloat64,
scoreBreakdown: []float64{0, 0, math.MaxFloat64 / 3.0},
},
"doc29": {
score: 0.567426591648309,
Expand Down
26 changes: 13 additions & 13 deletions search/scorer/scorer_knn.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package scorer

import (
"math"
"reflect"

"github.com/blevesearch/bleve/v2/search"
Expand Down Expand Up @@ -60,8 +61,9 @@ func NewKNNQueryScorer(queryVector []float32, queryField string, queryBoost floa
}
}

// TODO: Better value needed here?
const maxEuclideanDistance = 10000.0
// Score used when the knnMatch.Score = 0 ->
// the query and indexed vector are exactly the same.
const maxKNNScore = math.MaxFloat64

func (sqs *KNNQueryScorer) Score(ctx *search.SearchContext,
knnMatch *index.VectorDoc) *search.DocumentMatch {
Expand All @@ -70,20 +72,18 @@ func (sqs *KNNQueryScorer) Score(ctx *search.SearchContext,
if sqs.includeScore || sqs.options.Explain {
var scoreExplanation *search.Explanation
score := knnMatch.Score
if sqs.similarityMetric == index.EuclideanDistance {
// in case of euclidean distance being the distance metric,
// an exact vector (perfect match), would return distance = 0
if score == 0 {
score = maxEuclideanDistance
} else {
// euclidean distances need to be inverted to work with
// tf-idf scoring
score = 1.0 / score
}
// in case of euclidean distance being the distance metric,
// an exact vector (perfect match), would return distance = 0
if score == 0 {
score = maxKNNScore
} else {
// euclidean distances need to be inverted to work with
// tf-idf scoring
score = 1.0 / score
}

// if the query weight isn't 1, multiply
if sqs.queryWeight != 1.0 {
if sqs.queryWeight != 1.0 && score != maxKNNScore {
score = score * sqs.queryWeight
}

Expand Down

0 comments on commit 671df18

Please sign in to comment.