Skip to content

Commit

Permalink
Update query_rewriter.go
Browse files Browse the repository at this point in the history
corrected sqrt in euclidean and cosine
  • Loading branch information
rderbier committed May 8, 2024
1 parent f1ece2b commit 6544286
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,12 +659,12 @@ func rewriteAsSimilarByIdQuery(
topK := query.ArgValue(schema.SimilarTopKArgName)
similarByField := typ.Field(similarBy)
metric := similarByField.EmbeddingSearchMetric()
distanceFormula := "math((v2 - v1) dot (v2 - v1))" // default - euclidian
distanceFormula := "math(sqrt((v2 - v1) dot (v2 - v1)))" // default - euclidian

if metric == schema.SimilarSearchMetricDotProduct {
distanceFormula = "math((1.0 - (v1 dot v2)) /2.0)"
} else if metric == schema.SimilarSearchMetricCosine {
distanceFormula = "math((1.0 - ((v1 dot v2) / ((v1 dot v1) * (v2 dot v2)))) / 2.0)"
distanceFormula = "math((1.0 - ((v1 dot v2) / sqrt( (v1 dot v1) * (v2 dot v2) ) )) / 2.0)"
}

// First generate the query to fetch the uid
Expand Down Expand Up @@ -819,12 +819,12 @@ func rewriteAsSimilarByEmbeddingQuery(

similarByField := typ.Field(similarBy)
metric := similarByField.EmbeddingSearchMetric()
distanceFormula := "math((v2 - $search_vector) dot (v2 - $search_vector))" // default = euclidian
distanceFormula := "math(sqrt((v2 - $search_vector) dot (v2 - $search_vector)))" // default = euclidian

if metric == schema.SimilarSearchMetricDotProduct {
distanceFormula = "math(( 1.0 - (($search_vector) dot v2)) /2.0)"
} else if metric == schema.SimilarSearchMetricCosine {
distanceFormula = "math(( ($search_vector) dot v2) / (( ($search_vector) dot ($search_vector)) * (v2 dot v2)))"
distanceFormula = "math((1.0 - ( (($search_vector) dot v2) / sqrt( (($search_vector) dot ($search_vector)) * (v2 dot v2) ) )) / 2.0)"

Check failure on line 827 in graphql/resolve/query_rewriter.go

View workflow job for this annotation

GitHub Actions / lint

line is 135 characters (lll)
}

// Save vectorString as a query variable, $search_vector
Expand Down

0 comments on commit 6544286

Please sign in to comment.