From 1c5449fe1ae5de519bbf5b7e315b7709b595e0f1 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 13 Apr 2016 14:03:49 -0400 Subject: [PATCH] Remove PROTOTYPE from scripted_metric aggregation and cut it to registerAggregation. Relates to #17085 --- .../resources/checkstyle_suppressions.xml | 1 - .../elasticsearch/search/SearchModule.java | 5 +- .../aggregations/AggregatorBuilder.java | 4 +- .../ScriptedMetricAggregatorBuilder.java | 189 +++++++++++++++--- .../scripted/ScriptedMetricParser.java | 167 ---------------- 5 files changed, 169 insertions(+), 197 deletions(-) delete mode 100644 core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricParser.java diff --git a/buildSrc/src/main/resources/checkstyle_suppressions.xml b/buildSrc/src/main/resources/checkstyle_suppressions.xml index 557ffbcb298..5d52dbce3d3 100644 --- a/buildSrc/src/main/resources/checkstyle_suppressions.xml +++ b/buildSrc/src/main/resources/checkstyle_suppressions.xml @@ -729,7 +729,6 @@ - diff --git a/core/src/main/java/org/elasticsearch/search/SearchModule.java b/core/src/main/java/org/elasticsearch/search/SearchModule.java index f2cfd02532e..f625f9f7766 100644 --- a/core/src/main/java/org/elasticsearch/search/SearchModule.java +++ b/core/src/main/java/org/elasticsearch/search/SearchModule.java @@ -162,7 +162,7 @@ import org.elasticsearch.search.aggregations.metrics.percentiles.hdr.InternalHDR import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.InternalTDigestPercentileRanks; import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.InternalTDigestPercentiles; import org.elasticsearch.search.aggregations.metrics.scripted.InternalScriptedMetric; -import org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetricParser; +import org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetricAggregatorBuilder; import org.elasticsearch.search.aggregations.metrics.stats.InternalStats; import org.elasticsearch.search.aggregations.metrics.stats.StatsParser; import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStatsParser; @@ -467,7 +467,8 @@ public class SearchModule extends AbstractModule { registerAggregatorParser(new TopHitsParser()); registerAggregatorParser(new GeoBoundsParser()); registerAggregatorParser(new GeoCentroidParser()); - registerAggregatorParser(new ScriptedMetricParser()); + registerAggregation(ScriptedMetricAggregatorBuilder::new, ScriptedMetricAggregatorBuilder::parse, + ScriptedMetricAggregatorBuilder.AGGREGATION_NAME_FIELD); registerAggregation(ChildrenAggregatorBuilder::new, ChildrenAggregatorBuilder::parse, ChildrenAggregatorBuilder.AGGREGATION_NAME_FIELD); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/AggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/AggregatorBuilder.java index f5f8a9833db..bd05577801a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/AggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/AggregatorBuilder.java @@ -100,7 +100,9 @@ public abstract class AggregatorBuilder> extend return factory; } - protected abstract AB doReadFrom(String name, StreamInput in) throws IOException; + protected AB doReadFrom(String name, StreamInput in) throws IOException { + throw new UnsupportedOperationException(); // NORELEASE remove before 5.0.0GA + } /** * Add a sub aggregation to this aggregation. 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 219c199bfda..609d1e28343 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 @@ -19,22 +19,38 @@ package org.elasticsearch.search.aggregations.metrics.scripted; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptParameterParser; +import org.elasticsearch.script.ScriptParameterParser.ScriptParameterValue; import org.elasticsearch.search.aggregations.AggregatorBuilder; -import org.elasticsearch.search.aggregations.AggregatorFactory; import org.elasticsearch.search.aggregations.AggregatorFactories.Builder; +import org.elasticsearch.search.aggregations.AggregatorFactory; import org.elasticsearch.search.aggregations.support.AggregationContext; import java.io.IOException; +import java.util.HashSet; import java.util.Map; import java.util.Objects; +import java.util.Set; public class ScriptedMetricAggregatorBuilder extends AggregatorBuilder { - static final ScriptedMetricAggregatorBuilder PROTOTYPE = new ScriptedMetricAggregatorBuilder(""); + public static final String NAME = InternalScriptedMetric.TYPE.name(); + public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME); + + private static final ParseField INIT_SCRIPT_FIELD = new ParseField("init_script"); + private static final ParseField MAP_SCRIPT_FIELD = new ParseField("map_script"); + private static final ParseField COMBINE_SCRIPT_FIELD = new ParseField("combine_script"); + private static final ParseField REDUCE_SCRIPT_FIELD = new ParseField("reduce_script"); + private static final ParseField PARAMS_FIELD = new ParseField("params"); + private static final ParseField REDUCE_PARAMS_FIELD = new ParseField("reduce_params"); private Script initScript; private Script mapScript; @@ -46,6 +62,38 @@ public class ScriptedMetricAggregatorBuilder extends AggregatorBuilderinit script. */ @@ -149,52 +197,141 @@ public class ScriptedMetricAggregatorBuilder extends AggregatorBuilder params = null; + Map reduceParams = null; + XContentParser.Token token; + String currentFieldName = null; + Set scriptParameters = new HashSet<>(); + scriptParameters.add(INIT_SCRIPT_FIELD.getPreferredName()); + scriptParameters.add(MAP_SCRIPT_FIELD.getPreferredName()); + scriptParameters.add(COMBINE_SCRIPT_FIELD.getPreferredName()); + scriptParameters.add(REDUCE_SCRIPT_FIELD.getPreferredName()); + 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 (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_FIELD.getPreferredName()); + 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_FIELD.getPreferredName()); + 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_FIELD.getPreferredName()); + 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_FIELD.getPreferredName()); + 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 - protected void doWriteTo(StreamOutput out) throws IOException { - out.writeOptionalStreamable(initScript); - out.writeOptionalStreamable(mapScript); - out.writeOptionalStreamable(combineScript); - out.writeOptionalStreamable(reduceScript); - boolean hasParams = params != null; - out.writeBoolean(hasParams); - if (hasParams) { - out.writeMap(params); - } + public String getWriteableName() { + return NAME; } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricParser.java deleted file mode 100644 index 65035692e15..00000000000 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricParser.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.search.aggregations.metrics.scripted; - -import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.ParsingException; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.query.QueryParseContext; -import org.elasticsearch.script.Script; -import org.elasticsearch.script.ScriptParameterParser; -import org.elasticsearch.script.ScriptParameterParser.ScriptParameterValue; -import org.elasticsearch.search.aggregations.Aggregator; -import java.io.IOException; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -public class ScriptedMetricParser implements Aggregator.Parser { - - public static final String INIT_SCRIPT = "init_script"; - public static final String MAP_SCRIPT = "map_script"; - public static final String COMBINE_SCRIPT = "combine_script"; - public static final String REDUCE_SCRIPT = "reduce_script"; - public static final ParseField INIT_SCRIPT_FIELD = new ParseField("init_script"); - public static final ParseField MAP_SCRIPT_FIELD = new ParseField("map_script"); - public static final ParseField COMBINE_SCRIPT_FIELD = new ParseField("combine_script"); - public static final ParseField REDUCE_SCRIPT_FIELD = new ParseField("reduce_script"); - public static final ParseField PARAMS_FIELD = new ParseField("params"); - public static final ParseField REDUCE_PARAMS_FIELD = new ParseField("reduce_params"); - public static final ParseField LANG_FIELD = new ParseField("lang"); - - @Override - public String type() { - return InternalScriptedMetric.TYPE.name(); - } - - @Override - public ScriptedMetricAggregatorBuilder parse(String aggregationName, XContentParser parser, - QueryParseContext context) throws IOException { - Script initScript = null; - Script mapScript = null; - Script combineScript = null; - Script reduceScript = null; - Map 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); - - 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; - } - -}