diff --git a/modules/reindex/src/main/java/org/elasticsearch/index/reindex/AbstractAsyncBulkIndexByScrollAction.java b/modules/reindex/src/main/java/org/elasticsearch/index/reindex/AbstractAsyncBulkIndexByScrollAction.java index 09b7fd272c49a..c20b05da80cb6 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/index/reindex/AbstractAsyncBulkIndexByScrollAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/index/reindex/AbstractAsyncBulkIndexByScrollAction.java @@ -179,23 +179,23 @@ protected boolean applyScript(IndexRequest index, SearchHit doc, ExecutableScrip */ index.source((Map) resultCtx.remove(SourceFieldMapper.NAME)); - Object newValue = ctx.remove(IndexFieldMapper.NAME); + Object newValue = resultCtx.remove(IndexFieldMapper.NAME); if (false == doc.index().equals(newValue)) { scriptChangedIndex(index, newValue); } - newValue = ctx.remove(TypeFieldMapper.NAME); + newValue = resultCtx.remove(TypeFieldMapper.NAME); if (false == doc.type().equals(newValue)) { scriptChangedType(index, newValue); } - newValue = ctx.remove(IdFieldMapper.NAME); + newValue = resultCtx.remove(IdFieldMapper.NAME); if (false == doc.id().equals(newValue)) { scriptChangedId(index, newValue); } - newValue = ctx.remove(VersionFieldMapper.NAME); + newValue = resultCtx.remove(VersionFieldMapper.NAME); if (false == Objects.equals(oldVersion, newValue)) { scriptChangedVersion(index, newValue); } - newValue = ctx.remove(ParentFieldMapper.NAME); + newValue = resultCtx.remove(ParentFieldMapper.NAME); if (false == Objects.equals(oldParent, newValue)) { scriptChangedParent(index, newValue); } @@ -203,22 +203,22 @@ protected boolean applyScript(IndexRequest index, SearchHit doc, ExecutableScrip * Its important that routing comes after parent in case you want to * change them both. */ - newValue = ctx.remove(RoutingFieldMapper.NAME); + newValue = resultCtx.remove(RoutingFieldMapper.NAME); if (false == Objects.equals(oldRouting, newValue)) { scriptChangedRouting(index, newValue); } - newValue = ctx.remove(TimestampFieldMapper.NAME); + newValue = resultCtx.remove(TimestampFieldMapper.NAME); if (false == Objects.equals(oldTimestamp, newValue)) { scriptChangedTimestamp(index, newValue); } - newValue = ctx.remove(TTLFieldMapper.NAME); + newValue = resultCtx.remove(TTLFieldMapper.NAME); if (false == Objects.equals(oldTTL, newValue)) { scriptChangedTTL(index, newValue); } - if (false == ctx.isEmpty()) { + if (false == resultCtx.isEmpty()) { StringBuilder msg = new StringBuilder("Invalid fields added to ctx ["); boolean first = true; - for (String key : ctx.keySet()) { + for (String key : resultCtx.keySet()) { if (first) { first = false; } else { diff --git a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/SimpleExecutableScript.java b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/SimpleExecutableScript.java index da344e7bf3072..c1c4d59192910 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/SimpleExecutableScript.java +++ b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/SimpleExecutableScript.java @@ -22,8 +22,11 @@ import org.elasticsearch.common.util.Consumer; import org.elasticsearch.script.ExecutableScript; +import java.util.HashMap; import java.util.Map; +import static org.elasticsearch.test.ESTestCase.randomBoolean; + public class SimpleExecutableScript implements ExecutableScript { private final Consumer> script; private Map ctx; @@ -50,6 +53,13 @@ public void setNextVar(String name, Object value) { @Override public Object unwrap(Object value) { + // Some script engines (javascript) copy any maps they unwrap + if (randomBoolean()) { + if (value instanceof Map) { + return new HashMap<>((Map) value); + } + } + // Others just return the objects plain (groovy, painless) return value; } } \ No newline at end of file diff --git a/plugins/lang-javascript/src/main/java/org/elasticsearch/script/javascript/JavaScriptScriptEngineService.java b/plugins/lang-javascript/src/main/java/org/elasticsearch/script/javascript/JavaScriptScriptEngineService.java index ce5475ee450de..a316eb4e9fc33 100644 --- a/plugins/lang-javascript/src/main/java/org/elasticsearch/script/javascript/JavaScriptScriptEngineService.java +++ b/plugins/lang-javascript/src/main/java/org/elasticsearch/script/javascript/JavaScriptScriptEngineService.java @@ -175,14 +175,16 @@ public Object compile(String script, Map params) { } @Override - public ExecutableScript executable(CompiledScript compiledScript, Map vars) { + public ExecutableScript executable(CompiledScript compiledScript, @Nullable Map vars) { Context ctx = Context.enter(); try { Scriptable scope = ctx.newObject(globalScope); scope.setPrototype(globalScope); scope.setParentScope(null); - for (Map.Entry entry : vars.entrySet()) { - ScriptableObject.putProperty(scope, entry.getKey(), entry.getValue()); + if (vars != null) { + for (Map.Entry entry : vars.entrySet()) { + ScriptableObject.putProperty(scope, entry.getKey(), entry.getValue()); + } } return new JavaScriptExecutableScript((Script) compiledScript.compiled(), scope); diff --git a/plugins/lang-javascript/src/test/java/org/elasticsearch/script/javascript/JavaScriptScriptEngineTests.java b/plugins/lang-javascript/src/test/java/org/elasticsearch/script/javascript/JavaScriptScriptEngineTests.java index 2fe73d6eb44eb..02efef8bd2904 100644 --- a/plugins/lang-javascript/src/test/java/org/elasticsearch/script/javascript/JavaScriptScriptEngineTests.java +++ b/plugins/lang-javascript/src/test/java/org/elasticsearch/script/javascript/JavaScriptScriptEngineTests.java @@ -62,7 +62,13 @@ public void testSimpleEquation() { assertThat(((Number) o).intValue(), equalTo(3)); } - @Test + public void testNullVars() { + CompiledScript script = new CompiledScript(ScriptService.ScriptType.INLINE, "testSimpleEquation", "js", + se.compile("1 + 2", Collections.emptyMap())); + Object o = se.executable(script, null).run(); + assertThat(((Number) o).intValue(), equalTo(3)); + } + public void testMapAccess() { Map vars = new HashMap();