-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Pass through script params in scripted metric agg #29154
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,28 +35,35 @@ | |
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
public class ScriptedMetricAggregatorFactory extends AggregatorFactory<ScriptedMetricAggregatorFactory> { | ||
|
||
private final SearchScript.Factory mapScript; | ||
private final Map<String, Object> mapScriptParams; | ||
private final ExecutableScript.Factory combineScript; | ||
private final Map<String, Object> combineScriptParams; | ||
private final Script reduceScript; | ||
private final Map<String, Object> params; | ||
private final Map<String, Object> aggParams; | ||
private final SearchLookup lookup; | ||
private final ExecutableScript.Factory initScript; | ||
private final Map<String, Object> initScriptParams; | ||
|
||
public ScriptedMetricAggregatorFactory(String name, SearchScript.Factory mapScript, ExecutableScript.Factory initScript, | ||
ExecutableScript.Factory combineScript, Script reduceScript, Map<String, Object> params, | ||
public ScriptedMetricAggregatorFactory(String name, SearchScript.Factory mapScript, Map<String, Object> mapScriptParams, | ||
ExecutableScript.Factory initScript, Map<String, Object> initScriptParams, | ||
ExecutableScript.Factory combineScript, Map<String, Object> combineScriptParams, | ||
Script reduceScript, Map<String, Object> aggParams, | ||
SearchLookup lookup, SearchContext context, AggregatorFactory<?> parent, | ||
AggregatorFactories.Builder subFactories, Map<String, Object> metaData) throws IOException { | ||
super(name, context, parent, subFactories, metaData); | ||
this.mapScript = mapScript; | ||
this.mapScriptParams = mapScriptParams; | ||
this.initScript = initScript; | ||
this.initScriptParams = initScriptParams; | ||
this.combineScript = combineScript; | ||
this.combineScriptParams = combineScriptParams; | ||
this.reduceScript = reduceScript; | ||
this.lookup = lookup; | ||
this.params = params; | ||
this.aggParams = aggParams; | ||
} | ||
|
||
@Override | ||
|
@@ -65,26 +72,26 @@ public Aggregator createInternal(Aggregator parent, boolean collectsFromSingleBu | |
if (collectsFromSingleBucket == false) { | ||
return asMultiBucketAggregator(this, context, parent); | ||
} | ||
Map<String, Object> params = this.params; | ||
if (params != null) { | ||
params = deepCopyParams(params, context); | ||
Map<String, Object> aggParams = this.aggParams; | ||
if (aggParams != null) { | ||
aggParams = deepCopyParams(aggParams, context); | ||
} else { | ||
params = new HashMap<>(); | ||
aggParams = new HashMap<>(); | ||
} | ||
if (params.containsKey("_agg") == false) { | ||
params.put("_agg", new HashMap<String, Object>()); | ||
if (aggParams.containsKey("_agg") == false) { | ||
aggParams.put("_agg", new HashMap<String, Object>()); | ||
} | ||
|
||
final ExecutableScript initScript = this.initScript.newInstance(params); | ||
final SearchScript.LeafFactory mapScript = this.mapScript.newFactory(params, lookup); | ||
final ExecutableScript combineScript = this.combineScript.newInstance(params); | ||
final ExecutableScript initScript = this.initScript.newInstance(mergeParams(aggParams, initScriptParams)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I considered merging the parameter lists at construction time (which would also move conflict detection to construction time) but hit some test failures in that case. I did not rigorously track down the problem because I wasn't sure that it was a good idea to evaluate the params list before the last minute anyway, in case they could be modified in any way between AggregatorFactory construction and calling createInternal(). This is a smaller behavior change. |
||
final SearchScript.LeafFactory mapScript = this.mapScript.newFactory(mergeParams(aggParams, mapScriptParams), lookup); | ||
final ExecutableScript combineScript = this.combineScript.newInstance(mergeParams(aggParams, combineScriptParams)); | ||
|
||
final Script reduceScript = deepCopyScript(this.reduceScript, context); | ||
if (initScript != null) { | ||
initScript.run(); | ||
} | ||
return new ScriptedMetricAggregator(name, mapScript, | ||
combineScript, reduceScript, params, context, parent, | ||
combineScript, reduceScript, aggParams, context, parent, | ||
pipelineAggregators, metaData); | ||
} | ||
|
||
|
@@ -128,5 +135,18 @@ private static <T> T deepCopyParams(T original, SearchContext context) { | |
return clone; | ||
} | ||
|
||
private static Map<String, Object> mergeParams(Map<String, Object> agg, Map<String, Object> script) { | ||
// Start with script params | ||
Map<String, Object> combined = new HashMap<>(script); | ||
|
||
// Add in agg params, throwing an exception if any conflicts are detected | ||
for (Map.Entry<String, Object> aggEntry : agg.entrySet()) { | ||
if (combined.putIfAbsent(aggEntry.getKey(), aggEntry.getValue()) != null) { | ||
throw new IllegalArgumentException("Parameter name \"" + aggEntry.getKey() + | ||
"\" used in both aggregation and script parameters"); | ||
} | ||
} | ||
|
||
return combined; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
package org.elasticsearch.search.aggregations.metrics; | ||
|
||
import org.elasticsearch.action.index.IndexRequestBuilder; | ||
import org.elasticsearch.action.search.SearchPhaseExecutionException; | ||
import org.elasticsearch.action.search.SearchRequestBuilder; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.common.bytes.BytesArray; | ||
import org.elasticsearch.common.settings.Settings; | ||
|
@@ -62,6 +64,7 @@ | |
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; | ||
import static org.hamcrest.Matchers.allOf; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.greaterThan; | ||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
|
@@ -322,11 +325,11 @@ public void testMap() { | |
assertThat(numShardsRun, greaterThan(0)); | ||
} | ||
|
||
public void testMapWithParams() { | ||
public void testExplicitAggParam() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. testMapWithParams was previously actually working because the params were on the aggregation, not because the params were on the map script. This test may be extraneous now but I renamed it instead of deleting it since it does provide specific coverage for an explicit _agg param, even if that may be kind of a weird case. I wouldn't presume to delete it without an opinion from a dev more familiar with this code.. |
||
Map<String, Object> params = new HashMap<>(); | ||
params.put("_agg", new ArrayList<>()); | ||
|
||
Script mapScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_agg.add(1)", params); | ||
Script mapScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_agg.add(1)", Collections.emptyMap()); | ||
|
||
SearchResponse response = client().prepareSearch("idx") | ||
.setQuery(matchAllQuery()) | ||
|
@@ -361,17 +364,17 @@ public void testMapWithParams() { | |
} | ||
|
||
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); | ||
// Split the params up between the script and the aggregation. | ||
// Don't put any _agg map in params. | ||
Map<String, Object> scriptParams = Collections.singletonMap("param1", "12"); | ||
Map<String, Object> aggregationParams = Collections.singletonMap("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); | ||
Script mapScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_agg[param1] = param2", scriptParams); | ||
|
||
SearchResponse response = client().prepareSearch("idx") | ||
.setQuery(matchAllQuery()) | ||
.addAggregation(scriptedMetric("scripted").params(params).mapScript(mapScript)) | ||
.addAggregation(scriptedMetric("scripted").params(aggregationParams).mapScript(mapScript)) | ||
.get(); | ||
assertSearchResponse(response); | ||
assertThat(response.getHits().getTotalHits(), equalTo(numDocs)); | ||
|
@@ -1001,4 +1004,16 @@ public void testDontCacheScripts() throws Exception { | |
assertThat(client().admin().indices().prepareStats("cache_test_idx").setRequestCache(true).get().getTotal().getRequestCache() | ||
.getMissCount(), equalTo(0L)); | ||
} | ||
|
||
public void testConflictingAggAndScriptParams() { | ||
Map<String, Object> params = Collections.singletonMap("param1", "12"); | ||
Script mapScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_agg.add(1)", params); | ||
|
||
SearchRequestBuilder builder = client().prepareSearch("idx") | ||
.setQuery(matchAllQuery()) | ||
.addAggregation(scriptedMetric("scripted").params(params).mapScript(mapScript)); | ||
|
||
SearchPhaseExecutionException ex = expectThrows(SearchPhaseExecutionException.class, builder::get); | ||
assertThat(ex.getCause().getMessage(), containsString("Parameter name \"param1\" used in both aggregation and script parameters")); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to combine together the compiled script factories and the params into some kind of a tuple object rather than just adding more constructor parameters? I opted not to because they're different types and AFAIK passing them around paired with parameters isn't a common case.