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 3ddd3618ab812..a3f7111053bb5 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 @@ -67,7 +67,7 @@ protected UpdateByQueryRequest buildRequest(RestRequest request) throws IOExcept Map> consumers = new HashMap<>(); consumers.put("conflicts", o -> internal.setConflicts((String) o)); - consumers.put("script", o -> internal.setScript(parseScript((Map)o))); + consumers.put("script", o -> internal.setScript(parseScript(o))); parseInternalRequest(internal, request, consumers); @@ -76,49 +76,58 @@ protected UpdateByQueryRequest buildRequest(RestRequest request) throws IOExcept } @SuppressWarnings("unchecked") - private static Script parseScript(Map config) { - String script = null; - ScriptType type = null; - String lang = DEFAULT_SCRIPT_LANG; - Map params = Collections.emptyMap(); - for (Iterator> itr = config.entrySet().iterator(); itr.hasNext();) { - Map.Entry entry = itr.next(); - String parameterName = entry.getKey(); - Object parameterValue = entry.getValue(); - if (Script.LANG_PARSE_FIELD.match(parameterName)) { - if (parameterValue instanceof String || parameterValue == null) { - lang = (String) parameterValue; - } else { - throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]"); - } - } else if (Script.PARAMS_PARSE_FIELD.match(parameterName)) { - if (parameterValue instanceof Map || parameterValue == null) { - params = (Map) parameterValue; - } else { - throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]"); - } - } else if (ScriptType.INLINE.getParseField().match(parameterName)) { - if (parameterValue instanceof String || parameterValue == null) { - script = (String) parameterValue; - type = ScriptType.INLINE; - } else { - throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]"); - } - } else if (ScriptType.STORED.getParseField().match(parameterName)) { - if (parameterValue instanceof String || parameterValue == null) { - script = (String) parameterValue; - type = ScriptType.STORED; - } else { - throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]"); + private static Script parseScript(Object config) { + assert config != null : "Script should not be null"; + + if (config instanceof String) { + return new Script((String) config); + } else if (config instanceof Map) { + Map configMap = (Map) config; + String script = null; + ScriptType type = null; + String lang = DEFAULT_SCRIPT_LANG; + Map params = Collections.emptyMap(); + for (Iterator> itr = configMap.entrySet().iterator(); itr.hasNext();) { + Map.Entry entry = itr.next(); + String parameterName = entry.getKey(); + Object parameterValue = entry.getValue(); + if (Script.LANG_PARSE_FIELD.match(parameterName)) { + if (parameterValue instanceof String || parameterValue == null) { + lang = (String) parameterValue; + } else { + throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]"); + } + } else if (Script.PARAMS_PARSE_FIELD.match(parameterName)) { + if (parameterValue instanceof Map || parameterValue == null) { + params = (Map) parameterValue; + } else { + throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]"); + } + } else if (ScriptType.INLINE.getParseField().match(parameterName)) { + if (parameterValue instanceof String || parameterValue == null) { + script = (String) parameterValue; + type = ScriptType.INLINE; + } else { + throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]"); + } + } else if (ScriptType.STORED.getParseField().match(parameterName)) { + if (parameterValue instanceof String || parameterValue == null) { + script = (String) parameterValue; + type = ScriptType.STORED; + } else { + throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]"); + } } } - } - if (script == null) { - throw new ElasticsearchParseException("expected one of [{}] or [{}] fields, but found none", + if (script == null) { + throw new ElasticsearchParseException("expected one of [{}] or [{}] fields, but found none", ScriptType.INLINE.getParseField().getPreferredName(), ScriptType.STORED.getParseField().getPreferredName()); - } - assert type != null : "if script is not null, type should definitely not be null"; + } + assert type != null : "if script is not null, type should definitely not be null"; - return new Script(type, lang, script, params); + return new Script(type, 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 b43af1fc07e90..ea9fa33e6a9cf 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 @@ -29,6 +29,34 @@ user: notkimchy - match: { hits.total: 1 } +--- +"Update document using short `script` form": + - do: + index: + index: twitter + type: tweet + id: 1 + body: { "user": "kimchy" } + - do: + indices.refresh: {} + + - do: + update_by_query: + index: twitter + refresh: true + body: { "script": "ctx._source.user = \"not\" + ctx._source.user" } + - match: {updated: 1} + - match: {noops: 0} + + - do: + search: + index: twitter + body: + query: + match: + user: notkimchy + - match: { hits.total: 1 } + --- "Noop one doc": - do: