in src/main/java/org/opensearch/knn/index/KNNMethodContext.java [147:223]
public static KNNMethodContext parse(Object in) {
if (!(in instanceof Map<?, ?>)) {
throw new MapperParsingException("Unable to parse mapping into KNNMethodContext. Object not of type \"Map\"");
}
@SuppressWarnings("unchecked")
Map<String, Object> methodMap = (Map<String, Object>) in;
KNNEngine engine = KNNEngine.DEFAULT; // Get or default
SpaceType spaceType = SpaceType.DEFAULT; // Get or default
String name = "";
Map<String, Object> parameters = new HashMap<>();
String key;
Object value;
for (Map.Entry<String, Object> methodEntry : methodMap.entrySet()) {
key = methodEntry.getKey();
value = methodEntry.getValue();
if (KNN_ENGINE.equals(key)) {
if (value != null && !(value instanceof String)) {
throw new MapperParsingException("\"" + KNN_ENGINE + "\" must be a string");
}
if (value != null) {
try {
engine = KNNEngine.getEngine((String) value);
} catch (IllegalArgumentException iae) {
throw new MapperParsingException("Invalid " + KNN_ENGINE + ": " + value);
}
}
} else if (METHOD_PARAMETER_SPACE_TYPE.equals(key)) {
if (value != null && !(value instanceof String)) {
throw new MapperParsingException("\"" + METHOD_PARAMETER_SPACE_TYPE + "\" must be a string");
}
try {
spaceType = SpaceType.getSpace((String) value);
} catch (IllegalArgumentException iae) {
throw new MapperParsingException("Invalid " + METHOD_PARAMETER_SPACE_TYPE + ": " + value);
}
} else if (NAME.equals(key)) {
if (!(value instanceof String)) {
throw new MapperParsingException(NAME + "has to be a string");
}
name = (String) value;
} else if (PARAMETERS.equals(key)) {
if (!(value instanceof Map)) {
throw new MapperParsingException("Unable to parse parameters for main method component");
}
// Interpret all map parameters as sub-MethodComponentContexts
@SuppressWarnings("unchecked")
Map<String, Object> parameters1 = ((Map<String, Object>) value).entrySet().stream().collect(Collectors.toMap(
Map.Entry::getKey, e -> {
Object v = e.getValue();
if (v instanceof Map) {
return MethodComponentContext.parse(v);
}
return v;
}
));
parameters = parameters1;
} else {
throw new MapperParsingException("Invalid parameter: " + key);
}
}
if (name.isEmpty()) {
throw new MapperParsingException(NAME + " needs to be set");
}
MethodComponentContext method = new MethodComponentContext(name, parameters);
return new KNNMethodContext(engine, spaceType, method);
}