forked from elastic/elasticsearch
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 e…
- Loading branch information
Showing
10 changed files
with
527 additions
and
46 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
modules/lang-painless/src/test/java/org/elasticsearch/painless/MetricAggScriptsTests.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,113 @@ | ||
/* | ||
* 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.elasticsearch.painless.spi.Whitelist; | ||
import org.elasticsearch.script.MetricAggScripts; | ||
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 MetricAggScriptsTests extends ScriptTestCase { | ||
@Override | ||
protected Map<ScriptContext<?>, List<Whitelist>> scriptContexts() { | ||
Map<ScriptContext<?>, List<Whitelist>> contexts = new HashMap<>(); | ||
contexts.put(MetricAggScripts.InitScript.CONTEXT, Whitelist.BASE_WHITELISTS); | ||
contexts.put(MetricAggScripts.MapScript.CONTEXT, Whitelist.BASE_WHITELISTS); | ||
contexts.put(MetricAggScripts.CombineScript.CONTEXT, Whitelist.BASE_WHITELISTS); | ||
contexts.put(MetricAggScripts.ReduceScript.CONTEXT, Whitelist.BASE_WHITELISTS); | ||
return contexts; | ||
} | ||
|
||
public void testInitBasic() { | ||
MetricAggScripts.InitScript.Factory factory = scriptEngine.compile("test", | ||
"agg.testField = params.initialVal", MetricAggScripts.InitScript.CONTEXT, Collections.emptyMap()); | ||
|
||
Map<String, Object> params = new HashMap<>(); | ||
Map<String, Object> agg = new HashMap<>(); | ||
|
||
params.put("initialVal", 10); | ||
|
||
MetricAggScripts.InitScript script = factory.newInstance(params, agg); | ||
script.execute(); | ||
|
||
assert(agg.containsKey("testField")); | ||
assertEquals(10, agg.get("testField")); | ||
} | ||
|
||
public void testMapBasic() { | ||
MetricAggScripts.MapScript.Factory factory = scriptEngine.compile("test", | ||
"agg.testField = 2*_score", MetricAggScripts.MapScript.CONTEXT, Collections.emptyMap()); | ||
|
||
Map<String, Object> params = new HashMap<>(); | ||
Map<String, Object> agg = new HashMap<>(); | ||
double _score = 0.5; | ||
|
||
MetricAggScripts.MapScript.LeafFactory leafFactory = factory.newFactory(params, agg, null); | ||
MetricAggScripts.MapScript script = leafFactory.newInstance(null); | ||
|
||
script.execute(_score); | ||
|
||
assert(agg.containsKey("testField")); | ||
assertEquals(1.0, agg.get("testField")); | ||
} | ||
|
||
public void testCombineBasic() { | ||
MetricAggScripts.CombineScript.Factory factory = scriptEngine.compile("test", | ||
"agg.testField = params.initialVal; return agg.testField + params.inc", MetricAggScripts.CombineScript.CONTEXT, | ||
Collections.emptyMap()); | ||
|
||
Map<String, Object> params = new HashMap<>(); | ||
Map<String, Object> agg = new HashMap<>(); | ||
|
||
params.put("initialVal", 10); | ||
params.put("inc", 2); | ||
|
||
MetricAggScripts.CombineScript script = factory.newInstance(params, agg); | ||
Object res = script.execute(); | ||
|
||
assert(agg.containsKey("testField")); | ||
assertEquals(10, agg.get("testField")); | ||
assertEquals(12, res); | ||
} | ||
|
||
public void testReduceBasic() { | ||
MetricAggScripts.ReduceScript.Factory factory = scriptEngine.compile("test", | ||
"aggs[0].testField + aggs[1].testField", MetricAggScripts.ReduceScript.CONTEXT, Collections.emptyMap()); | ||
|
||
Map<String, Object> params = new HashMap<>(); | ||
List<Object> aggs = new ArrayList<>(); | ||
|
||
Map<String, Object> agg1 = new HashMap<>(), agg2 = new HashMap<>(); | ||
agg1.put("testField", 1); | ||
agg2.put("testField", 2); | ||
|
||
aggs.add(agg1); | ||
aggs.add(agg2); | ||
|
||
MetricAggScripts.ReduceScript script = factory.newInstance(params, aggs); | ||
Object res = script.execute(); | ||
assertEquals(3, res); | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
server/src/main/java/org/elasticsearch/script/MetricAggScripts.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,140 @@ | ||
/* | ||
* 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.script; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.elasticsearch.index.fielddata.ScriptDocValues; | ||
import org.elasticsearch.search.lookup.LeafSearchLookup; | ||
import org.elasticsearch.search.lookup.SearchLookup; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class MetricAggScripts { | ||
private abstract static class ParamsAndAggBase { | ||
private final Map<String, Object> params; | ||
private final Object agg; | ||
|
||
ParamsAndAggBase(Map<String, Object> params, Object agg) { | ||
this.params = params; | ||
this.agg = agg; | ||
} | ||
|
||
public Map<String, Object> getParams() { | ||
return params; | ||
} | ||
|
||
public Object getAgg() { | ||
return agg; | ||
} | ||
} | ||
|
||
public abstract static class InitScript extends ParamsAndAggBase { | ||
public InitScript(Map<String, Object> params, Object agg) { | ||
super(params, agg); | ||
} | ||
|
||
public abstract void execute(); | ||
|
||
public interface Factory { | ||
InitScript newInstance(Map<String, Object> params, Object agg); | ||
} | ||
|
||
public static String[] PARAMETERS = {}; | ||
public static ScriptContext<Factory> CONTEXT = new ScriptContext<>("aggs_init", Factory.class); | ||
} | ||
|
||
public abstract static class MapScript extends ParamsAndAggBase { | ||
private final LeafSearchLookup leafLookup; | ||
|
||
public MapScript(Map<String, Object> params, Object agg, SearchLookup lookup, LeafReaderContext leafContext) { | ||
super(params, agg); | ||
|
||
this.leafLookup = leafContext == null ? null : lookup.getLeafSearchLookup(leafContext); | ||
} | ||
|
||
// Return the doc as a map (instead of LeafDocLookup) in order to abide by type whitelisting rules for | ||
// Painless scripts. | ||
public Map<String, ScriptDocValues<?>> getDoc() { | ||
return leafLookup == null ? null : leafLookup.doc(); | ||
} | ||
|
||
public void setDocument(int docId) { | ||
if (leafLookup != null) { | ||
leafLookup.setDocument(docId); | ||
} | ||
} | ||
|
||
public abstract void execute(double _score); | ||
|
||
public interface LeafFactory { | ||
MapScript newInstance(LeafReaderContext ctx); | ||
} | ||
|
||
public interface Factory { | ||
LeafFactory newFactory(Map<String, Object> params, Object agg, SearchLookup lookup); | ||
} | ||
|
||
public static String[] PARAMETERS = new String[] {"_score"}; | ||
public static ScriptContext<Factory> CONTEXT = new ScriptContext<>("aggs_map", Factory.class); | ||
} | ||
|
||
public abstract static class CombineScript extends ParamsAndAggBase { | ||
public CombineScript(Map<String, Object> params, Object agg) { | ||
super(params, agg); | ||
} | ||
|
||
public abstract Object execute(); | ||
|
||
public interface Factory { | ||
CombineScript newInstance(Map<String, Object> params, Object agg); | ||
} | ||
|
||
public static String[] PARAMETERS = {}; | ||
public static ScriptContext<Factory> CONTEXT = new ScriptContext<>("aggs_combine", Factory.class); | ||
} | ||
|
||
public abstract static class ReduceScript { | ||
private final Map<String, Object> params; | ||
private final List<Object> aggs; | ||
|
||
public ReduceScript(Map<String, Object> params, List<Object> aggs) { | ||
this.params = params; | ||
this.aggs = aggs; | ||
} | ||
|
||
public Map<String, Object> getParams() { | ||
return params; | ||
} | ||
|
||
public List<Object> getAggs() { | ||
return aggs; | ||
} | ||
|
||
public abstract Object execute(); | ||
|
||
public interface Factory { | ||
ReduceScript newInstance(Map<String, Object> params, List<Object> aggs); | ||
} | ||
|
||
public static String[] PARAMETERS = {}; | ||
public static ScriptContext<Factory> CONTEXT = new ScriptContext<>("aggs_reduce", Factory.class); | ||
} | ||
} |
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
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.