}
@Override
protected Context prepare(@Nullable Map<String, Object> params) {
if (params == null) {
throw new ScriptException("Parameter required");
}
String operatorName = XContentMapValues.nodeStringValue(params.get("op"), null);
if (operatorName == null) {
throw new ScriptException("Missing the op parameter");
}
// extract operator arguments
if (!params.containsKey("args") || !XContentMapValues.isArray(params.get("args"))) {
throw new ScriptException(String.format(Locale.ENGLISH, "No parameters given for operator %s", operatorName));
}
List args = (List)params.get("args");
List<WrappedArgument> operatorArgs = new ArrayList<>(args.size());
List<DataType> operatorArgTypes = new ArrayList<>(args.size());
for (Object arg : args) {
assert arg instanceof Map;
WrappedArgument argument = getArgument((Map<String, Object>)arg);
operatorArgs.add(argument);
// TODO: use real types from argument here
operatorArgTypes.add(DataTypes.DOUBLE);
}
// first argument should be the scalar
if (!(operatorArgs.get(0) instanceof ScalarArgument)) {
throw new ScriptException("first argument in search script no scalar!");
}
ScalarArgument function = ((ScalarArgument) operatorArgs.get(0));
// resolve operator
FunctionIdent operatorIdent = new FunctionIdent(operatorName, operatorArgTypes);
Operator operator = (Operator)functions.get(operatorIdent);
if (operator == null) {
throw new ScriptException(String.format("Cannot resolve operator with ident %s", operatorIdent));
}
return new SearchContext(function, operator, operatorArgs);
}