diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/AggregatorParsers.java b/core/src/main/java/org/elasticsearch/search/aggregations/AggregatorParsers.java index 409b22f4f3c..2fabd6894b7 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/AggregatorParsers.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/AggregatorParsers.java @@ -20,7 +20,6 @@ package org.elasticsearch.search.aggregations; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.ParseFieldRegistry; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; @@ -78,15 +77,15 @@ public class AggregatorParsers { * @throws IOException When parsing fails for unknown reasons. */ public AggregatorFactories.Builder parseAggregators(QueryParseContext parseContext) throws IOException { - return parseAggregators(parseContext.parser(), parseContext, 0); + return parseAggregators(parseContext, 0); } - private AggregatorFactories.Builder parseAggregators(XContentParser parser, QueryParseContext parseContext, int level) - throws IOException { + private AggregatorFactories.Builder parseAggregators(QueryParseContext parseContext, int level) throws IOException { Matcher validAggMatcher = VALID_AGG_NAME.matcher(""); AggregatorFactories.Builder factories = new AggregatorFactories.Builder(); XContentParser.Token token = null; + XContentParser parser = parseContext.parser(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token != XContentParser.Token.FIELD_NAME) { throw new ParsingException(parser.getTokenLocation(), @@ -120,29 +119,7 @@ public class AggregatorParsers { final String fieldName = parser.currentName(); token = parser.nextToken(); - if ("aggregations_binary".equals(fieldName)) { - if (subFactories != null) { - throw new ParsingException(parser.getTokenLocation(), - "Found two sub aggregation definitions under [" + aggregationName + "]", - parser.getTokenLocation()); - } - XContentParser binaryParser = null; - if (token == XContentParser.Token.VALUE_STRING || token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) { - byte[] source = parser.binaryValue(); - binaryParser = XContentFactory.xContent(source).createParser(source); - } else { - throw new ParsingException(parser.getTokenLocation(), - "Expected [" + XContentParser.Token.VALUE_STRING + " or " + XContentParser.Token.VALUE_EMBEDDED_OBJECT - + "] for [" + fieldName + "], but got a [" + token + "] in [" + aggregationName + "]"); - } - XContentParser.Token binaryToken = binaryParser.nextToken(); - if (binaryToken != XContentParser.Token.START_OBJECT) { - throw new ParsingException(parser.getTokenLocation(), - "Expected [" + XContentParser.Token.START_OBJECT + "] as first token when parsing [" + fieldName - + "], but got a [" + binaryToken + "] in [" + aggregationName + "]"); - } - subFactories = parseAggregators(binaryParser, parseContext, level + 1); - } else if (token == XContentParser.Token.START_OBJECT) { + if (token == XContentParser.Token.START_OBJECT) { switch (fieldName) { case "meta": metaData = parser.map(); @@ -153,7 +130,7 @@ public class AggregatorParsers { throw new ParsingException(parser.getTokenLocation(), "Found two sub aggregation definitions under [" + aggregationName + "]"); } - subFactories = parseAggregators(parser, parseContext, level + 1); + subFactories = parseAggregators(parseContext, level + 1); break; default: if (aggFactory != null) { diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregatorBuilder.java index 609d1e28343..355fdd76b98 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregatorBuilder.java @@ -219,8 +219,7 @@ public class ScriptedMetricAggregatorBuilder extends AggregatorBuilder params = null; - Map reduceParams = null; - XContentParser.Token token; - String currentFieldName = null; - Set scriptParameters = new HashSet<>(); - scriptParameters.add(INIT_SCRIPT); - scriptParameters.add(MAP_SCRIPT); - scriptParameters.add(COMBINE_SCRIPT); - scriptParameters.add(REDUCE_SCRIPT); - ScriptParameterParser scriptParameterParser = new ScriptParameterParser(scriptParameters); - - XContentParser parser = context.parser(); - 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 (context.parseFieldMatcher().match(currentFieldName, INIT_SCRIPT_FIELD)) { - initScript = Script.parse(parser, context.parseFieldMatcher()); - } else if (context.parseFieldMatcher().match(currentFieldName, MAP_SCRIPT_FIELD)) { - mapScript = Script.parse(parser, context.parseFieldMatcher()); - } else if (context.parseFieldMatcher().match(currentFieldName, COMBINE_SCRIPT_FIELD)) { - combineScript = Script.parse(parser, context.parseFieldMatcher()); - } else if (context.parseFieldMatcher().match(currentFieldName, REDUCE_SCRIPT_FIELD)) { - reduceScript = Script.parse(parser, context.parseFieldMatcher()); - } else if (context.parseFieldMatcher().match(currentFieldName, PARAMS_FIELD)) { - params = parser.map(); - } else if (context.parseFieldMatcher().match(currentFieldName, REDUCE_PARAMS_FIELD)) { - reduceParams = parser.map(); - } else { - throw new ParsingException(parser.getTokenLocation(), - "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "]."); - } - } else if (token.isValue()) { - if (!scriptParameterParser.token(currentFieldName, token, parser, context.parseFieldMatcher())) { - throw new ParsingException(parser.getTokenLocation(), - "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "]."); - } - } else { - throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "]."); - } - } - - if (initScript == null) { // Didn't find anything using the new API so try using the old one instead - ScriptParameterValue scriptValue = scriptParameterParser.getScriptParameterValue(INIT_SCRIPT); - if (scriptValue != null) { - initScript = new Script(scriptValue.script(), scriptValue.scriptType(), scriptParameterParser.lang(), params); - } - } else if (initScript.getParams() != null) { - throw new ParsingException(parser.getTokenLocation(), - "init_script params are not supported. Parameters for the init_script must be specified in the params field on the scripted_metric aggregator not inside the init_script object"); - } - - if (mapScript == null) { // Didn't find anything using the new API so try using the old one instead - ScriptParameterValue scriptValue = scriptParameterParser.getScriptParameterValue(MAP_SCRIPT); - if (scriptValue != null) { - mapScript = new Script(scriptValue.script(), scriptValue.scriptType(), scriptParameterParser.lang(), params); - } - } else if (mapScript.getParams() != null) { - throw new ParsingException(parser.getTokenLocation(), - "map_script params are not supported. Parameters for the map_script must be specified in the params field on the scripted_metric aggregator not inside the map_script object"); - } - - if (combineScript == null) { // Didn't find anything using the new API so try using the old one instead - ScriptParameterValue scriptValue = scriptParameterParser.getScriptParameterValue(COMBINE_SCRIPT); - if (scriptValue != null) { - combineScript = new Script(scriptValue.script(), scriptValue.scriptType(), scriptParameterParser.lang(), params); - } - } else if (combineScript.getParams() != null) { - throw new ParsingException(parser.getTokenLocation(), - "combine_script params are not supported. Parameters for the combine_script must be specified in the params field on the scripted_metric aggregator not inside the combine_script object"); - } - - if (reduceScript == null) { // Didn't find anything using the new API so try using the old one instead - ScriptParameterValue scriptValue = scriptParameterParser.getScriptParameterValue(REDUCE_SCRIPT); - if (scriptValue != null) { - reduceScript = new Script(scriptValue.script(), scriptValue.scriptType(), scriptParameterParser.lang(), reduceParams); - } - } - - if (mapScript == null) { - throw new ParsingException(parser.getTokenLocation(), "map_script field is required in [" + aggregationName + "]."); - } - - ScriptedMetricAggregatorBuilder factory = new ScriptedMetricAggregatorBuilder(aggregationName); - if (initScript != null) { - factory.initScript(initScript); - } - if (mapScript != null) { - factory.mapScript(mapScript); - } - if (combineScript != null) { - factory.combineScript(combineScript); - } - if (reduceScript != null) { - factory.reduceScript(reduceScript); - } - if (params != null) { - factory.params(params); - } - return factory; - } - - @Override - public ScriptedMetricAggregatorBuilder getFactoryPrototypes() { - return ScriptedMetricAggregatorBuilder.PROTOTYPE; - } - -}