diff --git a/modules/reindex/src/main/java/org/elasticsearch/index/reindex/RestUpdateByQueryAction.java b/modules/reindex/src/main/java/org/elasticsearch/index/reindex/RestUpdateByQueryAction.java index 8b898244c0750..8f09afbb17c6c 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/index/reindex/RestUpdateByQueryAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/index/reindex/RestUpdateByQueryAction.java @@ -86,7 +86,7 @@ private static Script parseScript(Object config) { Map configMap = (Map) config; String script = null; ScriptType type = null; - String lang = DEFAULT_SCRIPT_LANG; + String lang = null; Map params = Collections.emptyMap(); for (Iterator> itr = configMap.entrySet().iterator(); itr.hasNext();) { Map.Entry entry = itr.next(); @@ -126,7 +126,15 @@ private static Script parseScript(Object config) { } assert type != null : "if script is not null, type should definitely not be null"; - return new Script(type, lang, script, params); + if (type == ScriptType.STORED) { + if (lang != null) { + throw new IllegalArgumentException("lang cannot be specified for stored scripts"); + } + + return new Script(type, null, script, null, params); + } else { + return new Script(type, lang == null ? DEFAULT_SCRIPT_LANG : lang, script, params); + } } else { throw new IllegalArgumentException("Script value should be a String or a Map"); } diff --git a/qa/smoke-test-reindex-with-all-modules/src/test/resources/rest-api-spec/test/update_by_query/10_script.yml b/qa/smoke-test-reindex-with-all-modules/src/test/resources/rest-api-spec/test/update_by_query/10_script.yml index aee2b2fb05759..dcd5977fb12bc 100644 --- a/qa/smoke-test-reindex-with-all-modules/src/test/resources/rest-api-spec/test/update_by_query/10_script.yml +++ b/qa/smoke-test-reindex-with-all-modules/src/test/resources/rest-api-spec/test/update_by_query/10_script.yml @@ -340,3 +340,84 @@ source: if (ctx._source.user == "kimchy") {ctx.op = "index"} else {ctx.op = "junk"} - match: { error.reason: 'Operation type [junk] not allowed, only [noop, index, delete] are allowed' } + +--- +"Update all docs with one deletion and one noop using a stored script": + - do: + index: + index: twitter + type: tweet + id: 1 + body: { "level": 9, "last_updated": "2016-01-01T12:10:30Z" } + - do: + index: + index: twitter + type: tweet + id: 2 + body: { "level": 10, "last_updated": "2016-01-01T12:10:30Z" } + - do: + index: + index: twitter + type: tweet + id: 3 + body: { "level": 11, "last_updated": "2016-01-01T12:10:30Z" } + - do: + index: + index: twitter + type: tweet + id: 4 + body: { "level": 12, "last_updated": "2016-01-01T12:10:30Z" } + - do: + indices.refresh: {} + - do: + put_script: + id: "my_update_script" + body: { "script": {"lang": "painless", + "source": "int choice = ctx._source.level % 3; + if (choice == 0) { + ctx._source.last_updated = '2016-01-02T00:00:00Z'; + } else if (choice == 1) { + ctx.op = 'noop'; + } else { + ctx.op = 'delete'; + }" } } + - match: { acknowledged: true } + + - do: + update_by_query: + refresh: true + index: twitter + body: + script: + id: "my_update_script" + + - match: {updated: 2} + - match: {deleted: 1} + - match: {noops: 1} + + - do: + search: + index: twitter + body: + query: + match: + last_updated: "2016-01-02T00:00:00Z" + - match: { hits.total: 2 } + + - do: + search: + index: twitter + body: + query: + match: + last_updated: "2016-01-01T12:10:30Z" + - match: { hits.total: 1 } + + - do: + search: + index: twitter + body: + query: + term: + level: 11 + - match: { hits.total: 0 }