Skip to content

Commit

Permalink
Add IVF binary format index loading and query support
Browse files Browse the repository at this point in the history
Signed-off-by: Junqiu Lei <[email protected]>
  • Loading branch information
junqiu-lei committed Jul 9, 2024
1 parent 596ab90 commit 70cb23f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ protected Query doToQuery(QueryShardContext context) {
knnEngine = modelMetadata.getKnnEngine();
spaceType = modelMetadata.getSpaceType();
methodComponentContext = modelMetadata.getMethodComponentContext();
vectorDataType = modelMetadata.getVectorDataType();

} else if (knnMethodContext != null) {
// If the dimension is set but the knnMethodContext is not then the field is using the legacy mapping
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/opensearch/knn/index/query/KNNWeight.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.opensearch.knn.index.query.filtered.NestedFilteredIdsKNNIterator;
import org.opensearch.knn.index.util.FieldInfoExtractor;
import org.opensearch.knn.index.util.KNNEngine;
import org.opensearch.knn.index.util.ModelInfoExtractor;
import org.opensearch.knn.indices.ModelDao;
import org.opensearch.knn.indices.ModelMetadata;
import org.opensearch.knn.indices.ModelUtil;
Expand Down Expand Up @@ -213,6 +214,7 @@ private Map<Integer, Float> doANNSearch(final LeafReaderContext context, final B

KNNEngine knnEngine;
SpaceType spaceType;
String indexDescription;

// Check if a modelId exists. If so, the space type and engine will need to be picked up from the model's
// metadata.
Expand All @@ -225,11 +227,13 @@ private Map<Integer, Float> doANNSearch(final LeafReaderContext context, final B

knnEngine = modelMetadata.getKnnEngine();
spaceType = modelMetadata.getSpaceType();
indexDescription = ModelInfoExtractor.getIndexDescription(modelMetadata);
} else {
String engineName = fieldInfo.attributes().getOrDefault(KNN_ENGINE, KNNEngine.NMSLIB.getName());
knnEngine = KNNEngine.getEngine(engineName);
String spaceTypeName = fieldInfo.attributes().getOrDefault(SPACE_TYPE, SpaceType.L2.getValue());
spaceType = SpaceType.getSpace(spaceTypeName);
indexDescription = FieldInfoExtractor.getIndexDescription(fieldInfo);
}

/*
Expand Down Expand Up @@ -261,12 +265,7 @@ private Map<Integer, Float> doANNSearch(final LeafReaderContext context, final B
new NativeMemoryEntryContext.IndexEntryContext(
indexPath.toString(),
NativeMemoryLoadStrategy.IndexLoadStrategy.getInstance(),
getParametersAtLoading(
spaceType,
knnEngine,
knnQuery.getIndexName(),
FieldInfoExtractor.getIndexDescription(fieldInfo)
),
getParametersAtLoading(spaceType, knnEngine, knnQuery.getIndexName(), indexDescription),
knnQuery.getIndexName(),
modelId
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.knn.index.util;

import org.opensearch.knn.index.MethodComponentContext;
import org.opensearch.knn.index.VectorDataType;
import org.opensearch.knn.indices.ModelMetadata;

import static org.opensearch.knn.index.util.Faiss.FAISS_BINARY_INDEX_DESCRIPTION_PREFIX;

public class ModelInfoExtractor {
public static String getIndexDescription(ModelMetadata modelMetadata) {
MethodComponentContext methodComponentContext = modelMetadata.getMethodComponentContext();
VectorDataType vectorDataType = modelMetadata.getVectorDataType();
String indexDescription = methodComponentContext.getName().toUpperCase();

if (VectorDataType.BINARY.equals(vectorDataType)) {
indexDescription = FAISS_BINARY_INDEX_DESCRIPTION_PREFIX + indexDescription;
}

return indexDescription;
}
}

0 comments on commit 70cb23f

Please sign in to comment.