Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.index.knn.engine;

/**
* Interface contains spaces supported for approximate nearest neighbor search in the k-NN plugin. Each engine's methods are
* expected to support a subset of these spaces. Validation should be done in the jni layer and an exception should be
* propagated up to the Java layer. Additionally, naming translations should be done in jni layer as well. For example,
* nmslib calls the inner_product space "negdotprod". This translation should take place in the nmslib's jni layer.
*/
public interface BaseSpaceType {
public abstract float scoreTranslation(float rawScore);

/**
* Get KNNVectorSimilarityFunction that maps to this SpaceType
*
* @return KNNVectorSimilarityFunction
*/
public BaseVectorSimilarityFunction getKnnVectorSimilarityFunction();

/**
* Validate if the given byte vector is supported by this space type
*
* @param vector the given vector
*/
public void validateVector(byte[] vector);

/**
* Validate if the given float vector is supported by this space type
*
* @param vector the given vector
*/
public void validateVector(float[] vector);

/**
* Validate if given vector data type is supported by this space type
*
* @param vectorDataType the given vector data type
*/
public void validateVectorDataType(BaseVectorDataType vectorDataType);

/**
* Get space type name in engine
*
* @return name
*/
public String getValue();

/**
* Translate a score to a distance for this space type
*
* @param score score to translate
* @return translated distance
*/
public float scoreToDistanceTranslation(float score);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.index.knn.engine;

import org.apache.lucene.document.FieldType;
import org.apache.lucene.util.BytesRef;

/**
* General interface for vector data types
*/
public interface BaseVectorDataType {
/**
* Creates a KnnVectorFieldType based on the VectorDataType using the provided dimension and
* VectorSimilarityFunction.
*
* @param dimension Dimension of the vector
* @param knnVectorSimilarityFunction KNNVectorSimilarityFunction for a given spaceType
* @return FieldType
*/
public FieldType createKnnVectorFieldType(int dimension, BaseVectorSimilarityFunction knnVectorSimilarityFunction);

/**
* Deserializes float vector from BytesRef.
*
* @param binaryValue Binary Value
* @return float vector deserialized from binary value
*/
public float[] getVectorFromBytesRef(BytesRef binaryValue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.index.knn.engine;

import org.apache.lucene.index.VectorSimilarityFunction;

/**
* Wrapper interface of VectorSimilarityFunction to support more function than what Lucene provides
*/
public interface BaseVectorSimilarityFunction {
public VectorSimilarityFunction getVectorSimilarityFunction();

public float compare(float[] var1, float[] var2);

public float compare(byte[] var1, byte[] var2);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.index.knn.engine;

import java.util.Collections;
import java.util.List;

import org.apache.lucene.util.Version;

/**
* VectorEngine is an interface that vector search engines in plugins must implement. It contains methods
* for getting the name and configurations of the engine
*/
public interface VectorEngine {
/**
* Gets the name of the KNNEngine.
*
* @return the name of the KNNEngine
*/
String getName();

/**
* Gets the version of the library that is being used. In general, this can be used for ensuring compatibility of
* serialized artifacts. For instance, this can be used to check if a given file that was created on a different
* cluster is compatible with this instance of the library.
*
* @return the string representing the library's version
*/
default String getVersion() {
return Version.LATEST.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/** Common interfaces for knn plugins to extend. */
package org.opensearch.index.knn.engine;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.plugins;

import java.util.Collections;
import java.util.Map;

import org.opensearch.index.knn.engine.VectorEngine;

/**
* An extension point for {@link Plugin} implementations to add custom knn engine implementations
*
* @opensearch.api
*/
public interface VectorSearchPlugin {
default Map<String, VectorEngine> getKnnEngines() {
return Collections.emptyMap();
}
}
Loading