Skip to content

Commit 3b07cfb

Browse files
author
Anton Pozhidaev
committed
Update by Query is modified to accept short script parameter. Closes issue elastic#24898
1 parent 37453ee commit 3b07cfb

File tree

1 file changed

+51
-41
lines changed

1 file changed

+51
-41
lines changed

modules/reindex/src/main/java/org/elasticsearch/index/reindex/RestUpdateByQueryAction.java

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected UpdateByQueryRequest buildRequest(RestRequest request) throws IOExcept
6767

6868
Map<String, Consumer<Object>> consumers = new HashMap<>();
6969
consumers.put("conflicts", o -> internal.setConflicts((String) o));
70-
consumers.put("script", o -> internal.setScript(parseScript((Map<String, Object>)o)));
70+
consumers.put("script", o -> internal.setScript(parseScript(o)));
7171

7272
parseInternalRequest(internal, request, consumers);
7373

@@ -76,49 +76,59 @@ protected UpdateByQueryRequest buildRequest(RestRequest request) throws IOExcept
7676
}
7777

7878
@SuppressWarnings("unchecked")
79-
private static Script parseScript(Map<String, Object> config) {
80-
String script = null;
81-
ScriptType type = null;
82-
String lang = DEFAULT_SCRIPT_LANG;
83-
Map<String, Object> params = Collections.emptyMap();
84-
for (Iterator<Map.Entry<String, Object>> itr = config.entrySet().iterator(); itr.hasNext();) {
85-
Map.Entry<String, Object> entry = itr.next();
86-
String parameterName = entry.getKey();
87-
Object parameterValue = entry.getValue();
88-
if (Script.LANG_PARSE_FIELD.match(parameterName)) {
89-
if (parameterValue instanceof String || parameterValue == null) {
90-
lang = (String) parameterValue;
91-
} else {
92-
throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]");
93-
}
94-
} else if (Script.PARAMS_PARSE_FIELD.match(parameterName)) {
95-
if (parameterValue instanceof Map || parameterValue == null) {
96-
params = (Map<String, Object>) parameterValue;
97-
} else {
98-
throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]");
99-
}
100-
} else if (ScriptType.INLINE.getParseField().match(parameterName)) {
101-
if (parameterValue instanceof String || parameterValue == null) {
102-
script = (String) parameterValue;
103-
type = ScriptType.INLINE;
104-
} else {
105-
throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]");
106-
}
107-
} else if (ScriptType.STORED.getParseField().match(parameterName)) {
108-
if (parameterValue instanceof String || parameterValue == null) {
109-
script = (String) parameterValue;
110-
type = ScriptType.STORED;
111-
} else {
112-
throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]");
79+
private static Script parseScript(Object config) {
80+
assert config != null : "Script should not be null";
81+
82+
if (config instanceof String) {
83+
return new Script((String) config);
84+
} else if (config instanceof Map) {
85+
Map<String,Object> configMap = (Map<String, Object>) config;
86+
String script = null;
87+
ScriptType type = null;
88+
String lang = DEFAULT_SCRIPT_LANG;
89+
Map<String, Object> params = Collections.emptyMap();
90+
for (Iterator<Map.Entry<String, Object>> itr = configMap.entrySet().iterator(); itr.hasNext();) {
91+
Map.Entry<String, Object> entry = itr.next();
92+
String parameterName = entry.getKey();
93+
Object parameterValue = entry.getValue();
94+
if (Script.LANG_PARSE_FIELD.match(parameterName)) {
95+
if (parameterValue instanceof String || parameterValue == null) {
96+
lang = (String) parameterValue;
97+
} else {
98+
throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]");
99+
}
100+
} else if (Script.PARAMS_PARSE_FIELD.match(parameterName)) {
101+
if (parameterValue instanceof Map || parameterValue == null) {
102+
params = (Map<String, Object>) parameterValue;
103+
} else {
104+
throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]");
105+
}
106+
} else if (ScriptType.INLINE.getParseField().match(parameterName)) {
107+
if (parameterValue instanceof String || parameterValue == null) {
108+
script = (String) parameterValue;
109+
type = ScriptType.INLINE;
110+
} else {
111+
throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]");
112+
}
113+
} else if (ScriptType.STORED.getParseField().match(parameterName)) {
114+
if (parameterValue instanceof String || parameterValue == null) {
115+
script = (String) parameterValue;
116+
type = ScriptType.STORED;
117+
} else {
118+
throw new ElasticsearchParseException("Value must be of type String: [" + parameterName + "]");
119+
}
113120
}
114121
}
115-
}
116-
if (script == null) {
117-
throw new ElasticsearchParseException("expected one of [{}] or [{}] fields, but found none",
122+
if (script == null) {
123+
throw new ElasticsearchParseException("expected one of [{}] or [{}] fields, but found none",
118124
ScriptType.INLINE.getParseField().getPreferredName(), ScriptType.STORED.getParseField().getPreferredName());
119-
}
120-
assert type != null : "if script is not null, type should definitely not be null";
125+
}
126+
assert type != null : "if script is not null, type should definitely not be null";
121127

122-
return new Script(type, lang, script, params);
128+
return new Script(type, lang, script, params);
129+
}
130+
else {
131+
throw new IllegalArgumentException("Script value should be a String or a Map");
132+
}
123133
}
124134
}

0 commit comments

Comments
 (0)