|
@@ -19,6 +19,11 @@
|
|
|
|
|
|
package org.elasticsearch.action.update;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.function.LongSupplier;
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
import org.elasticsearch.ElasticsearchException;
|
|
|
import org.elasticsearch.action.DocWriteResponse;
|
|
@@ -42,21 +47,22 @@ import org.elasticsearch.index.get.GetResult;
|
|
|
import org.elasticsearch.index.mapper.RoutingFieldMapper;
|
|
|
import org.elasticsearch.index.shard.IndexShard;
|
|
|
import org.elasticsearch.index.shard.ShardId;
|
|
|
-import org.elasticsearch.script.ExecutableScript;
|
|
|
import org.elasticsearch.script.Script;
|
|
|
import org.elasticsearch.script.ScriptService;
|
|
|
+import org.elasticsearch.script.UpdateScript;
|
|
|
import org.elasticsearch.search.lookup.SourceLookup;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.function.LongSupplier;
|
|
|
+import static org.elasticsearch.common.Booleans.parseBoolean;
|
|
|
|
|
|
/**
|
|
|
* Helper for translating an update request to an index, delete request or update response.
|
|
|
*/
|
|
|
public class UpdateHelper extends AbstractComponent {
|
|
|
+
|
|
|
+ /** Whether scripts should add the ctx variable to the params map. */
|
|
|
+ private static final boolean CTX_IN_PARAMS =
|
|
|
+ parseBoolean(System.getProperty("es.scripting.update.ctx_in_params"), true);
|
|
|
+
|
|
|
private final ScriptService scriptService;
|
|
|
|
|
|
public UpdateHelper(Settings settings, ScriptService scriptService) {
|
|
@@ -279,10 +285,18 @@ public class UpdateHelper extends AbstractComponent {
|
|
|
private Map<String, Object> executeScript(Script script, Map<String, Object> ctx) {
|
|
|
try {
|
|
|
if (scriptService != null) {
|
|
|
- ExecutableScript.Factory factory = scriptService.compile(script, ExecutableScript.UPDATE_CONTEXT);
|
|
|
- ExecutableScript executableScript = factory.newInstance(script.getParams());
|
|
|
- executableScript.setNextVar(ContextFields.CTX, ctx);
|
|
|
- executableScript.run();
|
|
|
+ UpdateScript.Factory factory = scriptService.compile(script, UpdateScript.CONTEXT);
|
|
|
+ final Map<String, Object> params;
|
|
|
+ if (CTX_IN_PARAMS) {
|
|
|
+ params = new HashMap<>(script.getParams());
|
|
|
+ params.put(ContextFields.CTX, ctx);
|
|
|
+ deprecationLogger.deprecated("Using `ctx` via `params.ctx` is deprecated. " +
|
|
|
+ "Use -Des.scripting.update.ctx_in_params=false to enforce non-deprecated usage.");
|
|
|
+ } else {
|
|
|
+ params = script.getParams();
|
|
|
+ }
|
|
|
+ UpdateScript executableScript = factory.newInstance(params);
|
|
|
+ executableScript.execute(ctx);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
throw new IllegalArgumentException("failed to execute script", e);
|