Skip to content

Commit 17e47c6

Browse files
albendzcolings86
authored andcommitted
Port aggregation parameter requirement changes from master branch (#34254)
1 parent e6c107b commit 17e47c6

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregationBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919

2020
package org.elasticsearch.search.aggregations.metrics.scripted;
2121

22+
import org.apache.logging.log4j.Logger;
2223
import org.elasticsearch.common.ParseField;
2324
import org.elasticsearch.common.ParsingException;
2425
import org.elasticsearch.common.io.stream.StreamInput;
2526
import org.elasticsearch.common.io.stream.StreamOutput;
27+
import org.elasticsearch.common.logging.Loggers;
2628
import org.elasticsearch.common.xcontent.XContentBuilder;
2729
import org.elasticsearch.common.xcontent.XContentParser;
2830
import org.elasticsearch.index.query.QueryShardContext;
@@ -33,6 +35,7 @@
3335
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
3436
import org.elasticsearch.search.aggregations.AggregatorFactory;
3537
import org.elasticsearch.search.internal.SearchContext;
38+
import org.elasticsearch.common.logging.DeprecationLogger;
3639

3740
import java.io.IOException;
3841
import java.util.Collections;
@@ -41,6 +44,8 @@
4144

4245
public class ScriptedMetricAggregationBuilder extends AbstractAggregationBuilder<ScriptedMetricAggregationBuilder> {
4346
public static final String NAME = "scripted_metric";
47+
private static final Logger logger = Loggers.getLogger(ScriptedMetricAggregationBuilder.class);
48+
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger);
4449

4550
private static final ParseField INIT_SCRIPT_FIELD = new ParseField("init_script");
4651
private static final ParseField MAP_SCRIPT_FIELD = new ParseField("map_script");
@@ -196,6 +201,13 @@ public Map<String, Object> params() {
196201
protected ScriptedMetricAggregatorFactory doBuild(SearchContext context, AggregatorFactory<?> parent,
197202
Builder subfactoriesBuilder) throws IOException {
198203

204+
if (combineScript == null) {
205+
deprecationLogger.deprecated("[combineScript] must be provided for metric aggregations.");
206+
}
207+
if(reduceScript == null) {
208+
deprecationLogger.deprecated("[reduceScript] must be provided for metric aggregations.");
209+
}
210+
199211
QueryShardContext queryShardContext = context.getQueryShardContext();
200212

201213
// Extract params from scripts and pass them along to ScriptedMetricAggregatorFactory, since it won't have

server/src/test/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregatorAggStateV6CompatTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ public void testWithImplicitAggParam() throws IOException {
132132
}
133133
}
134134

135-
assertWarnings(ScriptedMetricAggContexts.AGG_PARAM_DEPRECATION_WARNING);
135+
assertWarnings(ScriptedMetricAggContexts.AGG_PARAM_DEPRECATION_WARNING,
136+
"[reduceScript] must be provided for metric aggregations.");
136137
}
137138

138139
/**
@@ -161,7 +162,8 @@ public void testWithExplicitAggParam() throws IOException {
161162
}
162163
}
163164

164-
assertWarnings(ScriptedMetricAggContexts.AGG_PARAM_DEPRECATION_WARNING);
165+
assertWarnings(ScriptedMetricAggContexts.AGG_PARAM_DEPRECATION_WARNING,
166+
"[reduceScript] must be provided for metric aggregations.");
165167
}
166168

167169
/**

server/src/test/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregatorTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ public void testNoDocs() throws IOException {
139139
assertEquals(AGG_NAME, scriptedMetric.getName());
140140
assertNotNull(scriptedMetric.aggregation());
141141
assertEquals(0, ((HashMap<Object, String>) scriptedMetric.aggregation()).size());
142+
} finally {
143+
assertWarnings("[combineScript] must be provided for metric aggregations.",
144+
"[reduceScript] must be provided for metric aggregations.");
142145
}
143146
}
144147
}
@@ -163,6 +166,9 @@ public void testScriptedMetricWithoutCombine() throws IOException {
163166
assertNotNull(scriptedMetric.aggregation());
164167
Map<String, Object> agg = (Map<String, Object>) scriptedMetric.aggregation();
165168
assertEquals(numDocs, ((List<Integer>) agg.get("collector")).size());
169+
} finally {
170+
assertWarnings("[combineScript] must be provided for metric aggregations.",
171+
"[reduceScript] must be provided for metric aggregations.");
166172
}
167173
}
168174
}
@@ -185,6 +191,8 @@ public void testScriptedMetricWithCombine() throws IOException {
185191
assertEquals(AGG_NAME, scriptedMetric.getName());
186192
assertNotNull(scriptedMetric.aggregation());
187193
assertEquals(numDocs, scriptedMetric.aggregation());
194+
} finally {
195+
assertWarnings("[reduceScript] must be provided for metric aggregations.");
188196
}
189197
}
190198
}
@@ -208,6 +216,8 @@ public void testScriptedMetricWithCombineAccessesScores() throws IOException {
208216
assertNotNull(scriptedMetric.aggregation());
209217
// all documents have score of 1.0
210218
assertEquals((double) numDocs, scriptedMetric.aggregation());
219+
} finally {
220+
assertWarnings("[reduceScript] must be provided for metric aggregations.");
211221
}
212222
}
213223
}
@@ -227,6 +237,8 @@ public void testScriptParamsPassedThrough() throws IOException {
227237

228238
// The result value depends on the script params.
229239
assertEquals(306, scriptedMetric.aggregation());
240+
} finally {
241+
assertWarnings("[reduceScript] must be provided for metric aggregations.");
230242
}
231243
}
232244
}
@@ -250,6 +262,8 @@ public void testConflictingAggAndScriptParams() throws IOException {
250262
);
251263
assertEquals("Parameter name \"" + CONFLICTING_PARAM_NAME + "\" used in both aggregation and script parameters",
252264
ex.getMessage());
265+
} finally {
266+
assertWarnings("[reduceScript] must be provided for metric aggregations.");
253267
}
254268
}
255269
}

0 commit comments

Comments
 (0)