Skip to content

Commit 74b1e7d

Browse files
reeselevinecolings86
authored andcommitted
scripted_metric _agg parameter disappears if params are provided (#27159)
* Fixes #19768: scripted_metric _agg parameter disappears if params are provided * Test case for #19768 * Compare boolean to false instead of negating it * Added mocked script in ScriptedMetricIT * Fix test in ScriptedMetricIT for implicit _agg map
1 parent df5c8bb commit 74b1e7d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregatorFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public Aggregator createInternal(Aggregator parent, boolean collectsFromSingleBu
7070
params = deepCopyParams(params, context);
7171
} else {
7272
params = new HashMap<>();
73+
}
74+
if (params.containsKey("_agg") == false) {
7375
params.put("_agg", new HashMap<String, Object>());
7476
}
7577

core/src/test/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricIT.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
9494
scripts.put("_agg.add(1)", vars ->
9595
aggScript(vars, agg -> ((List) agg).add(1)));
9696

97+
scripts.put("_agg[param1] = param2", vars ->
98+
aggScript(vars, agg -> ((Map) agg).put(XContentMapValues.extractValue("params.param1", vars),
99+
XContentMapValues.extractValue("params.param2", vars))));
100+
97101
scripts.put("vars.multiplier = 3", vars ->
98102
((Map<String, Object>) vars.get("vars")).put("multiplier", 3));
99103

@@ -356,6 +360,52 @@ public void testMapWithParams() {
356360
assertThat(totalCount, equalTo(numDocs));
357361
}
358362

363+
public void testMapWithParamsAndImplicitAggMap() {
364+
Map<String, Object> params = new HashMap<>();
365+
// don't put any _agg map in params
366+
params.put("param1", "12");
367+
params.put("param2", 1);
368+
369+
// The _agg hashmap will be available even if not declared in the params map
370+
Script mapScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_agg[param1] = param2", params);
371+
372+
SearchResponse response = client().prepareSearch("idx")
373+
.setQuery(matchAllQuery())
374+
.addAggregation(scriptedMetric("scripted").params(params).mapScript(mapScript))
375+
.get();
376+
assertSearchResponse(response);
377+
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));
378+
379+
Aggregation aggregation = response.getAggregations().get("scripted");
380+
assertThat(aggregation, notNullValue());
381+
assertThat(aggregation, instanceOf(ScriptedMetric.class));
382+
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
383+
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
384+
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
385+
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
386+
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
387+
assertThat(aggregationList.size(), equalTo(getNumShards("idx").numPrimaries));
388+
int numShardsRun = 0;
389+
for (Object object : aggregationList) {
390+
assertThat(object, notNullValue());
391+
assertThat(object, instanceOf(Map.class));
392+
Map<?,?> map = (Map<?,?>) object;
393+
for (Map.Entry<?,?> entry : map.entrySet()) {
394+
assertThat(entry, notNullValue());
395+
assertThat(entry.getKey(), notNullValue());
396+
assertThat(entry.getKey(), instanceOf(String.class));
397+
assertThat(entry.getValue(), notNullValue());
398+
assertThat(entry.getValue(), instanceOf(Number.class));
399+
String stringValue = (String) entry.getKey();
400+
assertThat(stringValue, equalTo("12"));
401+
Number numberValue = (Number) entry.getValue();
402+
assertThat(numberValue, equalTo((Number) 1));
403+
numShardsRun++;
404+
}
405+
}
406+
assertThat(numShardsRun, greaterThan(0));
407+
}
408+
359409
public void testInitMapWithParams() {
360410
Map<String, Object> varsMap = new HashMap<>();
361411
varsMap.put("multiplier", 1);

0 commit comments

Comments
 (0)