From 5916190773d3e21c20b909a5714ef149f0539c5a Mon Sep 17 00:00:00 2001 From: Junqiu Lei Date: Wed, 17 Apr 2024 10:45:22 -0700 Subject: [PATCH 1/4] Use 0.95 as default ratio for lucene radial search traversal similarity Signed-off-by: Junqiu Lei --- .../org/opensearch/knn/common/KNNConstants.java | 2 ++ .../knn/index/query/RNNQueryFactory.java | 17 +++++++++++++++-- .../knn/index/query/KNNQueryBuilderTests.java | 6 +++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/opensearch/knn/common/KNNConstants.java b/src/main/java/org/opensearch/knn/common/KNNConstants.java index 69a2bc806..ef5b5abc2 100644 --- a/src/main/java/org/opensearch/knn/common/KNNConstants.java +++ b/src/main/java/org/opensearch/knn/common/KNNConstants.java @@ -133,4 +133,6 @@ public class KNNConstants { // Please refer this github issue for more details for choosing this value: // https://github.com/opensearch-project/k-NN/issues/1049#issuecomment-1694741092 public static int MAX_DISTANCE_COMPUTATIONS = 2048000; + + public static float LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO = 0.95f; } diff --git a/src/main/java/org/opensearch/knn/index/query/RNNQueryFactory.java b/src/main/java/org/opensearch/knn/index/query/RNNQueryFactory.java index cd32ac4f3..eca370b52 100644 --- a/src/main/java/org/opensearch/knn/index/query/RNNQueryFactory.java +++ b/src/main/java/org/opensearch/knn/index/query/RNNQueryFactory.java @@ -5,6 +5,7 @@ package org.opensearch.knn.index.query; +import static org.opensearch.knn.common.KNNConstants.LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO; import static org.opensearch.knn.common.KNNConstants.VECTOR_DATA_TYPE_FIELD; import static org.opensearch.knn.index.VectorDataType.SUPPORTED_VECTOR_DATA_TYPES; @@ -118,7 +119,13 @@ private static Query getFloatVectorSimilarityQuery( final float resultSimilarity, final Query filterQuery ) { - return new FloatVectorSimilarityQuery(fieldName, floatVector, resultSimilarity, filterQuery); + return new FloatVectorSimilarityQuery( + fieldName, + floatVector, + LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity, + resultSimilarity, + filterQuery + ); } /** @@ -131,6 +138,12 @@ private static Query getByteVectorSimilarityQuery( final float resultSimilarity, final Query filterQuery ) { - return new ByteVectorSimilarityQuery(fieldName, byteVector, resultSimilarity, filterQuery); + return new ByteVectorSimilarityQuery( + fieldName, + byteVector, + LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity, + resultSimilarity, + filterQuery + ); } } diff --git a/src/test/java/org/opensearch/knn/index/query/KNNQueryBuilderTests.java b/src/test/java/org/opensearch/knn/index/query/KNNQueryBuilderTests.java index 1922e5a08..76aa9158a 100644 --- a/src/test/java/org/opensearch/knn/index/query/KNNQueryBuilderTests.java +++ b/src/test/java/org/opensearch/knn/index/query/KNNQueryBuilderTests.java @@ -52,6 +52,7 @@ import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.opensearch.knn.common.KNNConstants.LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO; import static org.opensearch.knn.common.KNNConstants.METHOD_HNSW; import static org.opensearch.knn.index.KNNClusterTestUtils.mockClusterService; import static org.opensearch.knn.index.util.KNNEngine.ENGINES_SUPPORTING_RADIAL_SEARCH; @@ -394,7 +395,10 @@ public void testDoToQuery_whenNormal_whenDoRadiusSearch_whenDistanceThreshold_th KNNMethodContext knnMethodContext = new KNNMethodContext(KNNEngine.LUCENE, SpaceType.L2, methodComponentContext); when(mockKNNVectorField.getKnnMethodContext()).thenReturn(knnMethodContext); FloatVectorSimilarityQuery query = (FloatVectorSimilarityQuery) knnQueryBuilder.doToQuery(mockQueryShardContext); - assertTrue(query.toString().contains("resultSimilarity=" + KNNEngine.LUCENE.distanceToRadialThreshold(MAX_DISTANCE, SpaceType.L2))); + float resultSimilarity = KNNEngine.LUCENE.distanceToRadialThreshold(MAX_DISTANCE, SpaceType.L2); + + assertTrue(query.toString().contains("resultSimilarity=" + resultSimilarity)); + assertTrue(query.toString().contains("traversalSimilarity=" + LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity)); } public void testDoToQuery_whenNormal_whenDoRadiusSearch_whenScoreThreshold_thenSucceed() { From 8926872132f8e664d3a886e19814e8cc014f8250 Mon Sep 17 00:00:00 2001 From: Junqiu Lei Date: Wed, 17 Apr 2024 13:25:42 -0700 Subject: [PATCH 2/4] Update name with default Signed-off-by: Junqiu Lei --- .../opensearch/knn/common/KNNConstants.java | 2 +- .../knn/index/query/RNNQueryFactory.java | 22 +++++++++---------- .../knn/index/query/KNNQueryBuilderTests.java | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/opensearch/knn/common/KNNConstants.java b/src/main/java/org/opensearch/knn/common/KNNConstants.java index ef5b5abc2..594f7d63a 100644 --- a/src/main/java/org/opensearch/knn/common/KNNConstants.java +++ b/src/main/java/org/opensearch/knn/common/KNNConstants.java @@ -134,5 +134,5 @@ public class KNNConstants { // https://github.com/opensearch-project/k-NN/issues/1049#issuecomment-1694741092 public static int MAX_DISTANCE_COMPUTATIONS = 2048000; - public static float LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO = 0.95f; + public static float DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO = 0.95f; } diff --git a/src/main/java/org/opensearch/knn/index/query/RNNQueryFactory.java b/src/main/java/org/opensearch/knn/index/query/RNNQueryFactory.java index eca370b52..2818f5d6a 100644 --- a/src/main/java/org/opensearch/knn/index/query/RNNQueryFactory.java +++ b/src/main/java/org/opensearch/knn/index/query/RNNQueryFactory.java @@ -5,7 +5,7 @@ package org.opensearch.knn.index.query; -import static org.opensearch.knn.common.KNNConstants.LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO; +import static org.opensearch.knn.common.KNNConstants.DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO; import static org.opensearch.knn.common.KNNConstants.VECTOR_DATA_TYPE_FIELD; import static org.opensearch.knn.index.VectorDataType.SUPPORTED_VECTOR_DATA_TYPES; @@ -120,11 +120,11 @@ private static Query getFloatVectorSimilarityQuery( final Query filterQuery ) { return new FloatVectorSimilarityQuery( - fieldName, - floatVector, - LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity, - resultSimilarity, - filterQuery + fieldName, + floatVector, + DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity, + resultSimilarity, + filterQuery ); } @@ -139,11 +139,11 @@ private static Query getByteVectorSimilarityQuery( final Query filterQuery ) { return new ByteVectorSimilarityQuery( - fieldName, - byteVector, - LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity, - resultSimilarity, - filterQuery + fieldName, + byteVector, + DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity, + resultSimilarity, + filterQuery ); } } diff --git a/src/test/java/org/opensearch/knn/index/query/KNNQueryBuilderTests.java b/src/test/java/org/opensearch/knn/index/query/KNNQueryBuilderTests.java index 76aa9158a..a0784d73a 100644 --- a/src/test/java/org/opensearch/knn/index/query/KNNQueryBuilderTests.java +++ b/src/test/java/org/opensearch/knn/index/query/KNNQueryBuilderTests.java @@ -52,7 +52,7 @@ import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.opensearch.knn.common.KNNConstants.LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO; +import static org.opensearch.knn.common.KNNConstants.DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO; import static org.opensearch.knn.common.KNNConstants.METHOD_HNSW; import static org.opensearch.knn.index.KNNClusterTestUtils.mockClusterService; import static org.opensearch.knn.index.util.KNNEngine.ENGINES_SUPPORTING_RADIAL_SEARCH; @@ -398,7 +398,7 @@ public void testDoToQuery_whenNormal_whenDoRadiusSearch_whenDistanceThreshold_th float resultSimilarity = KNNEngine.LUCENE.distanceToRadialThreshold(MAX_DISTANCE, SpaceType.L2); assertTrue(query.toString().contains("resultSimilarity=" + resultSimilarity)); - assertTrue(query.toString().contains("traversalSimilarity=" + LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity)); + assertTrue(query.toString().contains("traversalSimilarity=" + DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity)); } public void testDoToQuery_whenNormal_whenDoRadiusSearch_whenScoreThreshold_thenSucceed() { From fa8407252aaefba87025ff3b58e808cb88150134 Mon Sep 17 00:00:00 2001 From: Junqiu Lei Date: Wed, 17 Apr 2024 15:09:57 -0700 Subject: [PATCH 3/4] Fix spotless Signed-off-by: Junqiu Lei --- .../knn/index/query/RNNQueryFactory.java | 20 +++++++++---------- .../knn/index/query/KNNQueryBuilderTests.java | 4 +++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/opensearch/knn/index/query/RNNQueryFactory.java b/src/main/java/org/opensearch/knn/index/query/RNNQueryFactory.java index 2818f5d6a..db8084864 100644 --- a/src/main/java/org/opensearch/knn/index/query/RNNQueryFactory.java +++ b/src/main/java/org/opensearch/knn/index/query/RNNQueryFactory.java @@ -120,11 +120,11 @@ private static Query getFloatVectorSimilarityQuery( final Query filterQuery ) { return new FloatVectorSimilarityQuery( - fieldName, - floatVector, - DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity, - resultSimilarity, - filterQuery + fieldName, + floatVector, + DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity, + resultSimilarity, + filterQuery ); } @@ -139,11 +139,11 @@ private static Query getByteVectorSimilarityQuery( final Query filterQuery ) { return new ByteVectorSimilarityQuery( - fieldName, - byteVector, - DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity, - resultSimilarity, - filterQuery + fieldName, + byteVector, + DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity, + resultSimilarity, + filterQuery ); } } diff --git a/src/test/java/org/opensearch/knn/index/query/KNNQueryBuilderTests.java b/src/test/java/org/opensearch/knn/index/query/KNNQueryBuilderTests.java index a0784d73a..134a5ab76 100644 --- a/src/test/java/org/opensearch/knn/index/query/KNNQueryBuilderTests.java +++ b/src/test/java/org/opensearch/knn/index/query/KNNQueryBuilderTests.java @@ -398,7 +398,9 @@ public void testDoToQuery_whenNormal_whenDoRadiusSearch_whenDistanceThreshold_th float resultSimilarity = KNNEngine.LUCENE.distanceToRadialThreshold(MAX_DISTANCE, SpaceType.L2); assertTrue(query.toString().contains("resultSimilarity=" + resultSimilarity)); - assertTrue(query.toString().contains("traversalSimilarity=" + DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity)); + assertTrue( + query.toString().contains("traversalSimilarity=" + DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO * resultSimilarity) + ); } public void testDoToQuery_whenNormal_whenDoRadiusSearch_whenScoreThreshold_thenSucceed() { From 2b83dc43963d7babe1adad9010c92a3fe2ff9f40 Mon Sep 17 00:00:00 2001 From: Junqiu Lei Date: Wed, 17 Apr 2024 15:37:29 -0700 Subject: [PATCH 4/4] Resolve feedback Signed-off-by: Junqiu Lei --- src/main/java/org/opensearch/knn/common/KNNConstants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/opensearch/knn/common/KNNConstants.java b/src/main/java/org/opensearch/knn/common/KNNConstants.java index 594f7d63a..7d872d60f 100644 --- a/src/main/java/org/opensearch/knn/common/KNNConstants.java +++ b/src/main/java/org/opensearch/knn/common/KNNConstants.java @@ -134,5 +134,5 @@ public class KNNConstants { // https://github.com/opensearch-project/k-NN/issues/1049#issuecomment-1694741092 public static int MAX_DISTANCE_COMPUTATIONS = 2048000; - public static float DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO = 0.95f; + public static final Float DEFAULT_LUCENE_RADIAL_SEARCH_TRAVERSAL_SIMILARITY_RATIO = 0.95f; }