Skip to content

Commit

Permalink
Refactor API name: modifyRequestSize -> adjustRequestSizeForKNN, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavdangeti committed Nov 20, 2023
1 parent 54685ca commit 6b903a4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion index_alias_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func MultiSearch(ctx context.Context, req *SearchRequest, indexes ...Index) (*Se
}
originalSize := req.Size
if len(indexes) > 1 {
modifyRequestSize(req, len(indexes))
req.Size = adjustRequestSizeForKNN(req, len(indexes))
}

// run search on each index in separate go routine
Expand Down
24 changes: 13 additions & 11 deletions search/scorer/scorer_knn.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (

var reflectStaticSizeKNNQueryScorer int

const MaxAllowedScore = 10000.0

func init() {
var sqs KNNQueryScorer
reflectStaticSizeKNNQueryScorer = int(reflect.TypeOf(sqs).Size())
Expand Down Expand Up @@ -62,22 +60,26 @@ func NewKNNQueryScorer(queryVector []float32, queryField string, queryBoost floa
}
}

// TODO: Better value needed here?
const maxEuclideanDistance = 100000.0

func (sqs *KNNQueryScorer) Score(ctx *search.SearchContext,
knnMatch *index.VectorDoc) *search.DocumentMatch {
rv := ctx.DocumentMatchPool.Get()

if sqs.includeScore || sqs.options.Explain {
var scoreExplanation *search.Explanation
score := knnMatch.Score
if score == 0.0 {
// the vector is found to be exactly same as the query vector
// this is a perfect match, so return the max score
score = MaxAllowedScore
} else if sqs.similarityMetric == index.EuclideanDistance {
// safeguard from divide-by-zero
// euclidean distances need to be inverted to work
// tf-idf scoring
score = 1.0 / 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
}
}

// if the query weight isn't 1, multiply
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion search/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ const (
const SearchIncrementalCostKey = "_search_incremental_cost_key"
const QueryTypeKey = "_query_type_key"
const FuzzyMatchPhraseKey = "_fuzzy_match_phrase_key"
const AliasPartitionedModeKey = "_alias_partitioned_mode_key"

func RecordSearchCost(ctx context.Context,
msg SearchIncrementalCostCallbackMsg, bytes uint64) {
Expand Down
23 changes: 14 additions & 9 deletions search_knn.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,22 @@ func mergeKNNResults(req *SearchRequest, sr *SearchResult) {
}
}

func modifyRequestSize(req *SearchRequest, numIndexes int) {
if len(req.KNN) > 0 {
var minSizeReq int64
for _, knn := range req.KNN {
minSizeReq += knn.K
}
minSizeReq *= int64(numIndexes)
if int64(req.Size) < minSizeReq {
req.Size = int(minSizeReq)
func adjustRequestSizeForKNN(req *SearchRequest, numIndexPartitions int) int {
var adjustedSize int
if req != nil {
adjustedSize = req.Size
if len(req.KNN) > 0 {
var minSizeReq int64
for _, knn := range req.KNN {
minSizeReq += knn.K
}
minSizeReq *= int64(numIndexPartitions)
if int64(adjustedSize) < minSizeReq {
adjustedSize = int(minSizeReq)
}
}
}
return adjustedSize
}

// heap impl
Expand Down
7 changes: 6 additions & 1 deletion search_no_knn.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ func validateKNN(req *SearchRequest) error {
}

func mergeKNNResults(req *SearchRequest, sr *SearchResult) {
// no-op
}

func modifyRequestSize(req *SearchRequest, numPartitions int) {
func adjustRequestSizeForKNN(req *SearchRequest, numIndexPartitions int) int {
if req != nil {
return req.Size
}
return 0
}

0 comments on commit 6b903a4

Please sign in to comment.