-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate scripted metric aggregation scripts to ScriptContext design (#…
…30111) * Migrate scripted metric aggregation scripts to ScriptContext design #29328 * Rename new script context container class and add clarifying comments to remaining references to params._agg(s) * Misc cleanup: make mock metric agg script inner classes static * Move _score to an accessor rather than an arg for scripted metric agg scripts This causes the score to be evaluated only when it's used. * Documentation changes for params._agg -> agg * Migration doc addition for scripted metric aggs _agg object change * Rename "agg" Scripted Metric Aggregation script context variable to "state" * Rename a private base class from ...Agg to ...State that I missed in my last commit * Clean up imports after merge
- Loading branch information
Showing
12 changed files
with
616 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...ang-painless/src/test/java/org/elasticsearch/painless/ScriptedMetricAggContextsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* 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.painless; | ||
|
||
import org.apache.lucene.search.DocIdSetIterator; | ||
import org.apache.lucene.search.Scorer; | ||
import org.elasticsearch.painless.spi.Whitelist; | ||
import org.elasticsearch.script.ScriptedMetricAggContexts; | ||
import org.elasticsearch.script.ScriptContext; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ScriptedMetricAggContextsTests extends ScriptTestCase { | ||
@Override | ||
protected Map<ScriptContext<?>, List<Whitelist>> scriptContexts() { | ||
Map<ScriptContext<?>, List<Whitelist>> contexts = new HashMap<>(); | ||
contexts.put(ScriptedMetricAggContexts.InitScript.CONTEXT, Whitelist.BASE_WHITELISTS); | ||
contexts.put(ScriptedMetricAggContexts.MapScript.CONTEXT, Whitelist.BASE_WHITELISTS); | ||
contexts.put(ScriptedMetricAggContexts.CombineScript.CONTEXT, Whitelist.BASE_WHITELISTS); | ||
contexts.put(ScriptedMetricAggContexts.ReduceScript.CONTEXT, Whitelist.BASE_WHITELISTS); | ||
return contexts; | ||
} | ||
|
||
public void testInitBasic() { | ||
ScriptedMetricAggContexts.InitScript.Factory factory = scriptEngine.compile("test", | ||
"state.testField = params.initialVal", ScriptedMetricAggContexts.InitScript.CONTEXT, Collections.emptyMap()); | ||
|
||
Map<String, Object> params = new HashMap<>(); | ||
Map<String, Object> state = new HashMap<>(); | ||
|
||
params.put("initialVal", 10); | ||
|
||
ScriptedMetricAggContexts.InitScript script = factory.newInstance(params, state); | ||
script.execute(); | ||
|
||
assert(state.containsKey("testField")); | ||
assertEquals(10, state.get("testField")); | ||
} | ||
|
||
public void testMapBasic() { | ||
ScriptedMetricAggContexts.MapScript.Factory factory = scriptEngine.compile("test", | ||
"state.testField = 2*_score", ScriptedMetricAggContexts.MapScript.CONTEXT, Collections.emptyMap()); | ||
|
||
Map<String, Object> params = new HashMap<>(); | ||
Map<String, Object> state = new HashMap<>(); | ||
|
||
Scorer scorer = new Scorer(null) { | ||
@Override | ||
public int docID() { return 0; } | ||
|
||
@Override | ||
public float score() { return 0.5f; } | ||
|
||
@Override | ||
public DocIdSetIterator iterator() { return null; } | ||
}; | ||
|
||
ScriptedMetricAggContexts.MapScript.LeafFactory leafFactory = factory.newFactory(params, state, null); | ||
ScriptedMetricAggContexts.MapScript script = leafFactory.newInstance(null); | ||
|
||
script.setScorer(scorer); | ||
script.execute(); | ||
|
||
assert(state.containsKey("testField")); | ||
assertEquals(1.0, state.get("testField")); | ||
} | ||
|
||
public void testCombineBasic() { | ||
ScriptedMetricAggContexts.CombineScript.Factory factory = scriptEngine.compile("test", | ||
"state.testField = params.initialVal; return state.testField + params.inc", ScriptedMetricAggContexts.CombineScript.CONTEXT, | ||
Collections.emptyMap()); | ||
|
||
Map<String, Object> params = new HashMap<>(); | ||
Map<String, Object> state = new HashMap<>(); | ||
|
||
params.put("initialVal", 10); | ||
params.put("inc", 2); | ||
|
||
ScriptedMetricAggContexts.CombineScript script = factory.newInstance(params, state); | ||
Object res = script.execute(); | ||
|
||
assert(state.containsKey("testField")); | ||
assertEquals(10, state.get("testField")); | ||
assertEquals(12, res); | ||
} | ||
|
||
public void testReduceBasic() { | ||
ScriptedMetricAggContexts.ReduceScript.Factory factory = scriptEngine.compile("test", | ||
"states[0].testField + states[1].testField", ScriptedMetricAggContexts.ReduceScript.CONTEXT, Collections.emptyMap()); | ||
|
||
Map<String, Object> params = new HashMap<>(); | ||
List<Object> states = new ArrayList<>(); | ||
|
||
Map<String, Object> state1 = new HashMap<>(), state2 = new HashMap<>(); | ||
state1.put("testField", 1); | ||
state2.put("testField", 2); | ||
|
||
states.add(state1); | ||
states.add(state2); | ||
|
||
ScriptedMetricAggContexts.ReduceScript script = factory.newInstance(params, states); | ||
Object res = script.execute(); | ||
assertEquals(3, res); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.