|
@@ -33,6 +33,7 @@ import org.elasticsearch.search.lookup.SearchLookup;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Function;
|
|
|
|
|
@@ -115,6 +116,18 @@ public class MockScriptEngine implements ScriptEngine {
|
|
|
} else if (context.instanceClazz.equals(ScoreScript.class)) {
|
|
|
ScoreScript.Factory factory = new MockScoreScript(script);
|
|
|
return context.factoryClazz.cast(factory);
|
|
|
+ } else if (context.instanceClazz.equals(ScriptedMetricAggContexts.InitScript.class)) {
|
|
|
+ ScriptedMetricAggContexts.InitScript.Factory factory = mockCompiled::createMetricAggInitScript;
|
|
|
+ return context.factoryClazz.cast(factory);
|
|
|
+ } else if (context.instanceClazz.equals(ScriptedMetricAggContexts.MapScript.class)) {
|
|
|
+ ScriptedMetricAggContexts.MapScript.Factory factory = mockCompiled::createMetricAggMapScript;
|
|
|
+ return context.factoryClazz.cast(factory);
|
|
|
+ } else if (context.instanceClazz.equals(ScriptedMetricAggContexts.CombineScript.class)) {
|
|
|
+ ScriptedMetricAggContexts.CombineScript.Factory factory = mockCompiled::createMetricAggCombineScript;
|
|
|
+ return context.factoryClazz.cast(factory);
|
|
|
+ } else if (context.instanceClazz.equals(ScriptedMetricAggContexts.ReduceScript.class)) {
|
|
|
+ ScriptedMetricAggContexts.ReduceScript.Factory factory = mockCompiled::createMetricAggReduceScript;
|
|
|
+ return context.factoryClazz.cast(factory);
|
|
|
}
|
|
|
throw new IllegalArgumentException("mock script engine does not know how to handle context [" + context.name + "]");
|
|
|
}
|
|
@@ -179,6 +192,23 @@ public class MockScriptEngine implements ScriptEngine {
|
|
|
public MovingFunctionScript createMovingFunctionScript() {
|
|
|
return new MockMovingFunctionScript();
|
|
|
}
|
|
|
+
|
|
|
+ public ScriptedMetricAggContexts.InitScript createMetricAggInitScript(Map<String, Object> params, Object state) {
|
|
|
+ return new MockMetricAggInitScript(params, state, script != null ? script : ctx -> 42d);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ScriptedMetricAggContexts.MapScript.LeafFactory createMetricAggMapScript(Map<String, Object> params, Object state,
|
|
|
+ SearchLookup lookup) {
|
|
|
+ return new MockMetricAggMapScript(params, state, lookup, script != null ? script : ctx -> 42d);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ScriptedMetricAggContexts.CombineScript createMetricAggCombineScript(Map<String, Object> params, Object state) {
|
|
|
+ return new MockMetricAggCombineScript(params, state, script != null ? script : ctx -> 42d);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ScriptedMetricAggContexts.ReduceScript createMetricAggReduceScript(Map<String, Object> params, List<Object> states) {
|
|
|
+ return new MockMetricAggReduceScript(params, states, script != null ? script : ctx -> 42d);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public class MockExecutableScript implements ExecutableScript {
|
|
@@ -333,6 +363,108 @@ public class MockScriptEngine implements ScriptEngine {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static class MockMetricAggInitScript extends ScriptedMetricAggContexts.InitScript {
|
|
|
+ private final Function<Map<String, Object>, Object> script;
|
|
|
+
|
|
|
+ MockMetricAggInitScript(Map<String, Object> params, Object state,
|
|
|
+ Function<Map<String, Object>, Object> script) {
|
|
|
+ super(params, state);
|
|
|
+ this.script = script;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void execute() {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+
|
|
|
+ if (getParams() != null) {
|
|
|
+ map.putAll(getParams()); // TODO: remove this once scripts know to look for params under params key
|
|
|
+ map.put("params", getParams());
|
|
|
+ }
|
|
|
+
|
|
|
+ map.put("state", getState());
|
|
|
+ script.apply(map);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class MockMetricAggMapScript implements ScriptedMetricAggContexts.MapScript.LeafFactory {
|
|
|
+ private final Map<String, Object> params;
|
|
|
+ private final Object state;
|
|
|
+ private final SearchLookup lookup;
|
|
|
+ private final Function<Map<String, Object>, Object> script;
|
|
|
+
|
|
|
+ MockMetricAggMapScript(Map<String, Object> params, Object state, SearchLookup lookup,
|
|
|
+ Function<Map<String, Object>, Object> script) {
|
|
|
+ this.params = params;
|
|
|
+ this.state = state;
|
|
|
+ this.lookup = lookup;
|
|
|
+ this.script = script;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ScriptedMetricAggContexts.MapScript newInstance(LeafReaderContext context) {
|
|
|
+ return new ScriptedMetricAggContexts.MapScript(params, state, lookup, context) {
|
|
|
+ @Override
|
|
|
+ public void execute() {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+
|
|
|
+ if (getParams() != null) {
|
|
|
+ map.putAll(getParams()); // TODO: remove this once scripts know to look for params under params key
|
|
|
+ map.put("params", getParams());
|
|
|
+ }
|
|
|
+
|
|
|
+ map.put("state", getState());
|
|
|
+ map.put("doc", getDoc());
|
|
|
+ map.put("_score", get_score());
|
|
|
+
|
|
|
+ script.apply(map);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class MockMetricAggCombineScript extends ScriptedMetricAggContexts.CombineScript {
|
|
|
+ private final Function<Map<String, Object>, Object> script;
|
|
|
+
|
|
|
+ MockMetricAggCombineScript(Map<String, Object> params, Object state,
|
|
|
+ Function<Map<String, Object>, Object> script) {
|
|
|
+ super(params, state);
|
|
|
+ this.script = script;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object execute() {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+
|
|
|
+ if (getParams() != null) {
|
|
|
+ map.putAll(getParams()); // TODO: remove this once scripts know to look for params under params key
|
|
|
+ map.put("params", getParams());
|
|
|
+ }
|
|
|
+
|
|
|
+ map.put("state", getState());
|
|
|
+ return script.apply(map);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class MockMetricAggReduceScript extends ScriptedMetricAggContexts.ReduceScript {
|
|
|
+ private final Function<Map<String, Object>, Object> script;
|
|
|
+
|
|
|
+ MockMetricAggReduceScript(Map<String, Object> params, List<Object> states,
|
|
|
+ Function<Map<String, Object>, Object> script) {
|
|
|
+ super(params, states);
|
|
|
+ this.script = script;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object execute() {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+
|
|
|
+ if (getParams() != null) {
|
|
|
+ map.putAll(getParams()); // TODO: remove this once scripts know to look for params under params key
|
|
|
+ map.put("params", getParams());
|
|
|
+ }
|
|
|
+
|
|
|
+ map.put("states", getStates());
|
|
|
+ return script.apply(map);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static Script mockInlineScript(final String script) {
|
|
|
return new Script(ScriptType.INLINE, "mock", script, emptyMap());
|
|
|
}
|
|
@@ -343,15 +475,15 @@ public class MockScriptEngine implements ScriptEngine {
|
|
|
return MovingFunctions.unweightedAvg(values);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public class MockScoreScript implements ScoreScript.Factory {
|
|
|
-
|
|
|
+
|
|
|
private final Function<Map<String, Object>, Object> scripts;
|
|
|
-
|
|
|
+
|
|
|
MockScoreScript(Function<Map<String, Object>, Object> scripts) {
|
|
|
this.scripts = scripts;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public ScoreScript.LeafFactory newFactory(Map<String, Object> params, SearchLookup lookup) {
|
|
|
return new ScoreScript.LeafFactory() {
|
|
@@ -359,7 +491,7 @@ public class MockScriptEngine implements ScriptEngine {
|
|
|
public boolean needs_score() {
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public ScoreScript newInstance(LeafReaderContext ctx) throws IOException {
|
|
|
Scorer[] scorerHolder = new Scorer[1];
|
|
@@ -373,7 +505,7 @@ public class MockScriptEngine implements ScriptEngine {
|
|
|
}
|
|
|
return ((Number) scripts.apply(vars)).doubleValue();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public void setScorer(Scorer scorer) {
|
|
|
scorerHolder[0] = scorer;
|