Skip to content

Commit

Permalink
Fix ExactKnnQueryBuilderTests testToQuery (elastic#110357) (elastic#1…
Browse files Browse the repository at this point in the history
…10589)

closes elastic#110357

With the loosening of what is considered a unit vector, we need to
ensure we only normalize for equality checking if the query vector is
indeed not a unit vector.
  • Loading branch information
benwtrent authored Jul 8, 2024
1 parent e8556c1 commit fd790ff
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
3 changes: 0 additions & 3 deletions muted-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ tests:
- class: org.elasticsearch.backwards.SearchWithMinCompatibleSearchNodeIT
method: testMinVersionAsOldVersion
issue: https://github.com/elastic/elasticsearch/issues/109454
- class: org.elasticsearch.search.vectors.ExactKnnQueryBuilderTests
method: testToQuery
issue: https://github.com/elastic/elasticsearch/issues/110357
- class: org.elasticsearch.search.aggregations.bucket.terms.RareTermsIT
method: testSingleValuedString
issue: https://github.com/elastic/elasticsearch/issues/110388
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public class DenseVectorFieldMapper extends FieldMapper {
public static final String COSINE_MAGNITUDE_FIELD_SUFFIX = "._magnitude";
private static final float EPS = 1e-3f;

static boolean isNotUnitVector(float magnitude) {
public static boolean isNotUnitVector(float magnitude) {
return Math.abs(magnitude - 1.0f) > EPS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.index.IndexVersions;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.AbstractQueryTestCase;
Expand Down Expand Up @@ -87,7 +88,9 @@ protected void doAssertLuceneQuery(ExactKnnQueryBuilder queryBuilder, Query quer
DenseVectorQuery.Floats denseVectorQuery = (DenseVectorQuery.Floats) query;
assertEquals(VECTOR_FIELD, denseVectorQuery.field);
float[] expected = Arrays.copyOf(queryBuilder.getQuery().asFloatVector(), queryBuilder.getQuery().asFloatVector().length);
if (context.getIndexSettings().getIndexVersionCreated().onOrAfter(IndexVersions.NORMALIZED_VECTOR_COSINE)) {
float magnitude = VectorUtil.dotProduct(expected, expected);
if (context.getIndexSettings().getIndexVersionCreated().onOrAfter(IndexVersions.NORMALIZED_VECTOR_COSINE)
&& DenseVectorFieldMapper.isNotUnitVector(magnitude)) {
VectorUtil.l2normalize(expected);
assertArrayEquals(expected, denseVectorQuery.getQuery(), 0.0f);
} else {
Expand Down

0 comments on commit fd790ff

Please sign in to comment.