Skip to content

Commit b0136a1

Browse files
pozhidaevaknik9000
authored andcommitted
Update by Query is modified to accept short script parameter. (#26841)
Update by Query is modified to accept short `script` parameter. Closes issue #24898
1 parent a86ff67 commit b0136a1

File tree

2 files changed

+78
-41
lines changed

2 files changed

+78
-41
lines changed

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

+50-41
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,58 @@ 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+
} else {
130+
throw new IllegalArgumentException("Script value should be a String or a Map");
131+
}
123132
}
124133
}

qa/smoke-test-reindex-with-all-modules/src/test/resources/rest-api-spec/test/update_by_query/10_script.yml

+28
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,34 @@
2929
user: notkimchy
3030
- match: { hits.total: 1 }
3131

32+
---
33+
"Update document using short `script` form":
34+
- do:
35+
index:
36+
index: twitter
37+
type: tweet
38+
id: 1
39+
body: { "user": "kimchy" }
40+
- do:
41+
indices.refresh: {}
42+
43+
- do:
44+
update_by_query:
45+
index: twitter
46+
refresh: true
47+
body: { "script": "ctx._source.user = \"not\" + ctx._source.user" }
48+
- match: {updated: 1}
49+
- match: {noops: 0}
50+
51+
- do:
52+
search:
53+
index: twitter
54+
body:
55+
query:
56+
match:
57+
user: notkimchy
58+
- match: { hits.total: 1 }
59+
3260
---
3361
"Noop one doc":
3462
- do:

0 commit comments

Comments
 (0)