diff --git a/src/main/java/org/opensearch/knn/index/mapper/FlatVectorFieldMapper.java b/src/main/java/org/opensearch/knn/index/mapper/FlatVectorFieldMapper.java index f671b07f8..9f1ebcf01 100644 --- a/src/main/java/org/opensearch/knn/index/mapper/FlatVectorFieldMapper.java +++ b/src/main/java/org/opensearch/knn/index/mapper/FlatVectorFieldMapper.java @@ -110,8 +110,4 @@ protected PerDimensionProcessor getPerDimensionProcessor() { return PerDimensionProcessor.NOOP_PROCESSOR; } - @Override - protected VectorTransformer getVectorTransformer() { - return VectorTransformer.NOOP_VECTOR_TRANSFORMER; - } } diff --git a/src/main/java/org/opensearch/knn/index/mapper/KNNVectorFieldMapper.java b/src/main/java/org/opensearch/knn/index/mapper/KNNVectorFieldMapper.java index 8604d5506..4e552a9e0 100644 --- a/src/main/java/org/opensearch/knn/index/mapper/KNNVectorFieldMapper.java +++ b/src/main/java/org/opensearch/knn/index/mapper/KNNVectorFieldMapper.java @@ -689,11 +689,21 @@ protected void validatePreparse() { protected abstract PerDimensionProcessor getPerDimensionProcessor(); /** - * Getter for vector transformer after vector parsing and validation + * Retrieves the vector transformer for the KNN vector field. + * This method provides access to the vector transformer instance that will be used + * for processing vectors in the KNN field. The transformer is responsible for any + * necessary vector transformations before indexing or searching. + * This implementation delegates to the VectorTransformerFactory to obtain + * the appropriate transformer instance. The returned transformer is typically + * stateless and thread-safe. + * + * @return VectorTransformer An instance of VectorTransformer that will be used + * for vector transformations in this field * - * @return VectorTransformer */ - protected abstract VectorTransformer getVectorTransformer(); + protected VectorTransformer getVectorTransformer() { + return VectorTransformerFactory.getVectorTransformer(); + } protected void parseCreateField(ParseContext context, int dimension, VectorDataType vectorDataType) throws IOException { validatePreparse(); diff --git a/src/main/java/org/opensearch/knn/index/mapper/VectorTransformer.java b/src/main/java/org/opensearch/knn/index/mapper/VectorTransformer.java index ac6a9b1ac..eb8c9dca0 100644 --- a/src/main/java/org/opensearch/knn/index/mapper/VectorTransformer.java +++ b/src/main/java/org/opensearch/knn/index/mapper/VectorTransformer.java @@ -34,10 +34,4 @@ default void transform(final byte[] vector) { throw new IllegalArgumentException("Input vector cannot be null"); } } - - /** - * A no-operation transformer that returns vector values unchanged. - */ - VectorTransformer NOOP_VECTOR_TRANSFORMER = new VectorTransformer() { - }; } diff --git a/src/main/java/org/opensearch/knn/index/mapper/VectorTransformerFactory.java b/src/main/java/org/opensearch/knn/index/mapper/VectorTransformerFactory.java index 94463533c..4df5daa32 100644 --- a/src/main/java/org/opensearch/knn/index/mapper/VectorTransformerFactory.java +++ b/src/main/java/org/opensearch/knn/index/mapper/VectorTransformerFactory.java @@ -19,6 +19,12 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class VectorTransformerFactory { + /** + * A no-operation transformer that returns vector values unchanged. + */ + private final static VectorTransformer NOOP_VECTOR_TRANSFORMER = new VectorTransformer() { + }; + /** * Returns a vector transformer based on the provided KNN method context. * For FAISS engine with cosine similarity space type, returns a NormalizeVectorTransformer @@ -36,6 +42,20 @@ public static VectorTransformer getVectorTransformer(final KNNMethodContext cont return getVectorTransformer(context.getKnnEngine(), context.getSpaceType()); } + /** + * Returns a vector transformer instance for vector transformations. + * This method provides access to the default no-operation vector transformer + * that performs identity transformation on vectors. The transformer does not + * modify the input vectors and returns them as-is.This implementation returns a stateless, thread-safe transformer + * instance that can be safely shared across multiple calls + * + * @return VectorTransformer A singleton instance of the no-operation vector + * transformer (NOOP_VECTOR_TRANSFORMER) + */ + public static VectorTransformer getVectorTransformer() { + return NOOP_VECTOR_TRANSFORMER; + } + /** * Creates a VectorTransformer based on the provided model metadata. * @@ -69,7 +89,7 @@ public static VectorTransformer getVectorTransformer(final ModelMetadata metadat * @return VectorTransformer An appropriate vector transformer instance */ private static VectorTransformer getVectorTransformer(final KNNEngine knnEngine, final SpaceType spaceType) { - return shouldNormalizeVector(knnEngine, spaceType) ? new NormalizeVectorTransformer() : VectorTransformer.NOOP_VECTOR_TRANSFORMER; + return shouldNormalizeVector(knnEngine, spaceType) ? new NormalizeVectorTransformer() : getVectorTransformer(); } private static boolean shouldNormalizeVector(final KNNEngine knnEngine, final SpaceType spaceType) { diff --git a/src/test/java/org/opensearch/knn/index/mapper/VectorTransformerFactoryTests.java b/src/test/java/org/opensearch/knn/index/mapper/VectorTransformerFactoryTests.java index 3e50dd546..ef666134c 100644 --- a/src/test/java/org/opensearch/knn/index/mapper/VectorTransformerFactoryTests.java +++ b/src/test/java/org/opensearch/knn/index/mapper/VectorTransformerFactoryTests.java @@ -76,7 +76,7 @@ private static void validateTransformer(SpaceType spaceType, KNNEngine engine, V } else { assertSame( "Should return NOOP transformer for " + engine + " with COSINESIMIL", - VectorTransformer.NOOP_VECTOR_TRANSFORMER, + VectorTransformerFactory.getVectorTransformer(), transformer ); }