Skip to content

Commit 2862450

Browse files
authored
3.0 dev convert l2sq score to l2 score for hnsw and ivfflat (#22543)
convert l2sq score to l2 score for hnsw and ivfflat Approved by: @XuPeng-SH, @fengttt
1 parent 65261d0 commit 2862450

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

pkg/vectorindex/hnsw/search.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"errors"
1919
"fmt"
2020
"io"
21+
"math"
2122
"os"
2223
"sync"
2324
"sync/atomic"
@@ -265,6 +266,10 @@ func (s *HnswSearch) Search(proc *process.Process, anyquery any, rt vectorindex.
265266
return nil, nil, moerr.NewInternalError(proc.Ctx, "heap return key is not int64")
266267
}
267268
reskeys = append(reskeys, sr.Id)
269+
if s.Idxcfg.Usearch.Metric == usearch.L2sq {
270+
// distance functino of L2Distance is l2sq so sqrt at the end
271+
sr.Distance = math.Sqrt(sr.Distance)
272+
}
268273
resdistances = append(resdistances, sr.Distance)
269274
}
270275

pkg/vectorindex/ivfflat/search.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package ivfflat
1717
import (
1818
"context"
1919
"fmt"
20+
"math"
2021
"strconv"
2122
"sync"
2223

@@ -284,6 +285,10 @@ func (idx *IvfflatSearchIndex[T]) Search(proc *process.Process, idxcfg vectorind
284285
return nil, nil, moerr.NewInternalError(proc.Ctx, "ivf search: heap return key is not any")
285286
}
286287
resid = append(resid, sr.Id)
288+
if metric.MetricType(idxcfg.Ivfflat.Metric) == metric.Metric_L2Distance {
289+
// distance functino of L2Distance is l2sq so sqrt at the end
290+
sr.Distance = math.Sqrt(sr.Distance)
291+
}
287292
resdist = append(resdist, sr.Distance)
288293
}
289294

0 commit comments

Comments
 (0)