Set<String> scriptParameters = new HashSet<>();
scriptParameters.add(INIT_SCRIPT);
scriptParameters.add(MAP_SCRIPT);
scriptParameters.add(COMBINE_SCRIPT);
scriptParameters.add(REDUCE_SCRIPT);
ScriptParameterParser scriptParameterParser = new ScriptParameterParser(scriptParameters);
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if (PARAMS_FIELD.match(currentFieldName)) {
params = parser.map();
} else if (REDUCE_PARAMS_FIELD.match(currentFieldName)) {
reduceParams = parser.map();
} else {
throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else if (token.isValue()) {
if (!scriptParameterParser.token(currentFieldName, token, parser)) {
throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else {
throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].");
}
}
ScriptParameterValue initScriptValue = scriptParameterParser.getScriptParameterValue(INIT_SCRIPT);
String initScript = null;
ScriptType initScriptType = null;
if (initScriptValue != null) {
initScript = initScriptValue.script();
initScriptType = initScriptValue.scriptType();
}
ScriptParameterValue mapScriptValue = scriptParameterParser.getScriptParameterValue(MAP_SCRIPT);
String mapScript = null;
ScriptType mapScriptType = null;
if (mapScriptValue != null) {
mapScript = mapScriptValue.script();
mapScriptType = mapScriptValue.scriptType();
}
ScriptParameterValue combineScriptValue = scriptParameterParser.getScriptParameterValue(COMBINE_SCRIPT);
String combineScript = null;
ScriptType combineScriptType = null;
if (combineScriptValue != null) {
combineScript = combineScriptValue.script();
combineScriptType = combineScriptValue.scriptType();
}
ScriptParameterValue reduceScriptValue = scriptParameterParser.getScriptParameterValue(REDUCE_SCRIPT);
String reduceScript = null;
ScriptType reduceScriptType = null;
if (reduceScriptValue != null) {
reduceScript = reduceScriptValue.script();
reduceScriptType = reduceScriptValue.scriptType();
}
scriptLang = scriptParameterParser.lang();
if (mapScript == null) {
throw new SearchParseException(context, "map_script field is required in [" + aggregationName + "].");
}
return new ScriptedMetricAggregator.Factory(aggregationName, scriptLang, initScriptType, initScript, mapScriptType, mapScript,