Skip to content

Commit

Permalink
Simplify parseSourceValue for valueFetcher
Browse files Browse the repository at this point in the history
Signed-off-by: Ethan Emoto <[email protected]>
  • Loading branch information
e-emoto committed Jan 13, 2025
1 parent c4094b0 commit 7773a78
Showing 1 changed file with 6 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package org.opensearch.knn.index.mapper;

import lombok.Getter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
Expand All @@ -23,18 +25,19 @@
import org.opensearch.search.lookup.SearchLookup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.function.Supplier;

import static org.opensearch.knn.common.KNNConstants.VECTOR_DATA_TYPE_FIELD;
import static org.opensearch.knn.index.mapper.KNNVectorFieldMapperUtil.deserializeStoredVector;

/**
* A KNNVector field type to represent the vector field in Opensearch
*/
@Getter
public class KNNVectorFieldType extends MappedFieldType {
private static final Logger logger = LogManager.getLogger(KNNVectorFieldType.class);
KNNMappingConfig knnMappingConfig;
VectorDataType vectorDataType;

Expand All @@ -58,24 +61,10 @@ public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchL
@Override
protected Object parseSourceValue(Object value) {
if (value instanceof ArrayList) {
for (Object item : (ArrayList) value) {
if (VectorDataType.BINARY == vectorDataType || VectorDataType.BYTE == vectorDataType) {
assert item instanceof Integer;
} else if (VectorDataType.FLOAT == vectorDataType) {
assert item instanceof Double;
} else {
throw new IllegalArgumentException(
String.format(
Locale.ROOT,
"Cannot parse source value for unsupported values provided for field [%s]",
VECTOR_DATA_TYPE_FIELD
)
);
}
}
return value;
} else {
return deserializeStoredVector((BytesRef) value, vectorDataType);
logger.warn("Expected type ArrayList for value, but got {} ", value.getClass());
return Collections.emptyList();
}
}
};
Expand Down

0 comments on commit 7773a78

Please sign in to comment.