Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scripted_metric _agg parameter disappears if params are provided #27159

Merged
merged 9 commits into from
Nov 8, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public Aggregator createInternal(Aggregator parent, boolean collectsFromSingleBu
params = deepCopyParams(params, context);
} else {
params = new HashMap<>();
}
if (params.containsKey("_agg") == false) {
params.put("_agg", new HashMap<String, Object>());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
scripts.put("_agg.add(1)", vars ->
aggScript(vars, agg -> ((List) agg).add(1)));

scripts.put("_agg[param1] = param2", vars ->
aggScript(vars, agg -> ((Map) agg).put(XContentMapValues.extractValue("params.param1", vars),
XContentMapValues.extractValue("params.param2", vars))));

scripts.put("vars.multiplier = 3", vars ->
((Map<String, Object>) vars.get("vars")).put("multiplier", 3));

Expand Down Expand Up @@ -356,6 +360,52 @@ public void testMapWithParams() {
assertThat(totalCount, equalTo(numDocs));
}

public void testMapWithParamsAndImplicitAggMap() {
Map<String, Object> params = new HashMap<>();
// don't put any _agg map in params
params.put("param1", "12");
params.put("param2", 1);

// The _agg hashmap will be available even if not declared in the params map
Script mapScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_agg[param1] = param2", params);

SearchResponse response = client().prepareSearch("idx")
.setQuery(matchAllQuery())
.addAggregation(scriptedMetric("scripted").params(params).mapScript(mapScript))
.get();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(numDocs));

Aggregation aggregation = response.getAggregations().get("scripted");
assertThat(aggregation, notNullValue());
assertThat(aggregation, instanceOf(ScriptedMetric.class));
ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
assertThat(aggregationList.size(), equalTo(getNumShards("idx").numPrimaries));
int numShardsRun = 0;
for (Object object : aggregationList) {
assertThat(object, notNullValue());
assertThat(object, instanceOf(Map.class));
Map<?,?> map = (Map<?,?>) object;
for (Map.Entry<?,?> entry : map.entrySet()) {
assertThat(entry, notNullValue());
assertThat(entry.getKey(), notNullValue());
assertThat(entry.getKey(), instanceOf(String.class));
assertThat(entry.getValue(), notNullValue());
assertThat(entry.getValue(), instanceOf(Number.class));
String stringValue = (String) entry.getKey();
assertThat(stringValue, equalTo("12"));
Number numberValue = (Number) entry.getValue();
assertThat(numberValue, equalTo((Number) 1));
numShardsRun++;
}
}
assertThat(numShardsRun, greaterThan(0));
}

public void testInitMapWithParams() {
Map<String, Object> varsMap = new HashMap<>();
varsMap.put("multiplier", 1);
Expand Down