-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactors FieldMapper logic. It removes the LegacyFieldMapper and replaces it with a FlatFieldMapper. The FlatFieldMapper's role is to create fields that do not build ANN indices. Additionally, it puts dimension, model_id, and knn_method_context in a new ANNConfig class and adds some safety checks around accessing them. This should make calling logic easier to handle. Lastly, it cleans up the parsing so that there isnt encoder parsing directly in the KNNVectorFieldMapper. Signed-off-by: John Mazanec <[email protected]>
- Loading branch information
1 parent
56698f7
commit 9de1292
Showing
33 changed files
with
1,454 additions
and
918 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/main/java/org/opensearch/knn/index/mapper/FlatVectorFieldMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.mapper; | ||
|
||
import org.apache.lucene.document.FieldType; | ||
import org.opensearch.Version; | ||
import org.opensearch.common.Explicit; | ||
import org.opensearch.knn.index.VectorDataType; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Mapper used when you dont want to build an underlying KNN struct - you just want to | ||
* store vectors as doc values | ||
*/ | ||
public class FlatVectorFieldMapper extends KNNVectorFieldMapper { | ||
|
||
private final PerDimensionValidator perDimensionValidator; | ||
|
||
public static FlatVectorFieldMapper createFieldMapper( | ||
String fullname, | ||
String simpleName, | ||
Map<String, String> metaValue, | ||
VectorDataType vectorDataType, | ||
Integer dimension, | ||
MultiFields multiFields, | ||
CopyTo copyTo, | ||
Explicit<Boolean> ignoreMalformed, | ||
boolean stored, | ||
boolean hasDocValues, | ||
Version indexCreatedVersion | ||
) { | ||
final KNNVectorFieldType mappedFieldType = new KNNVectorFieldType(fullname, metaValue, vectorDataType, new KNNMappingConfig() { | ||
@Override | ||
public Optional<Integer> getDimension() { | ||
return Optional.of(dimension); | ||
} | ||
}); | ||
return new FlatVectorFieldMapper( | ||
simpleName, | ||
mappedFieldType, | ||
multiFields, | ||
copyTo, | ||
ignoreMalformed, | ||
stored, | ||
hasDocValues, | ||
indexCreatedVersion | ||
); | ||
} | ||
|
||
private FlatVectorFieldMapper( | ||
String simpleName, | ||
KNNVectorFieldType mappedFieldType, | ||
MultiFields multiFields, | ||
CopyTo copyTo, | ||
Explicit<Boolean> ignoreMalformed, | ||
boolean stored, | ||
boolean hasDocValues, | ||
Version indexCreatedVersion | ||
) { | ||
super(simpleName, mappedFieldType, multiFields, copyTo, ignoreMalformed, stored, hasDocValues, indexCreatedVersion, null); | ||
this.perDimensionValidator = selectPerDimensionValidator(vectorDataType); | ||
this.fieldType = new FieldType(KNNVectorFieldMapper.Defaults.FIELD_TYPE); | ||
this.fieldType.freeze(); | ||
} | ||
|
||
private PerDimensionValidator selectPerDimensionValidator(VectorDataType vectorDataType) { | ||
if (VectorDataType.BINARY == vectorDataType) { | ||
return PerDimensionValidator.DEFAULT_BIT_VALIDATOR; | ||
} | ||
|
||
if (VectorDataType.BYTE == vectorDataType) { | ||
return PerDimensionValidator.DEFAULT_BYTE_VALIDATOR; | ||
} | ||
|
||
return PerDimensionValidator.DEFAULT_FLOAT_VALIDATOR; | ||
} | ||
|
||
@Override | ||
protected VectorValidator getVectorValidator() { | ||
return VectorValidator.NOOP_VECTOR_VALIDATOR; | ||
} | ||
|
||
@Override | ||
protected PerDimensionValidator getPerDimensionValidator() { | ||
return perDimensionValidator; | ||
} | ||
|
||
@Override | ||
protected PerDimensionProcessor getPerDimensionProcessor() { | ||
return PerDimensionProcessor.NOOP_PROCESSOR; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/org/opensearch/knn/index/mapper/KNNMappingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.mapper; | ||
|
||
import org.opensearch.knn.index.engine.KNNMethodContext; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Class holds information about how the ANN indices are created. The design of this class ensures that we do not | ||
* accidentally configure an index that has multiple ways it can be created. This class is immutable. | ||
*/ | ||
public interface KNNMappingConfig { | ||
/** | ||
* | ||
* @return Optional containing the modelId if created from model, otherwise empty | ||
*/ | ||
default Optional<String> getModelId() { | ||
return Optional.empty(); | ||
} | ||
|
||
/** | ||
* | ||
* @return Optional containing the KNNMethodContext if created from method, otherwise empty | ||
*/ | ||
default Optional<KNNMethodContext> getKnnMethodContext() { | ||
return Optional.empty(); | ||
} | ||
|
||
/** | ||
* | ||
* @return the dimension of the index; for model based indices, it will be null | ||
*/ | ||
default Optional<Integer> getDimension() { | ||
return Optional.empty(); | ||
} | ||
} |
Oops, something went wrong.