Skip to content

Commit 42fe661

Browse files
authored
Fix Parsing Bug with Update By Query for Stored Scripts (#29039)
This changes the parsing logic for stored scripts in update by query to match the parsing logic for scripts in general Elasticsearch. Closes #28002
1 parent 9046912 commit 42fe661

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private static Script parseScript(Object config) {
8686
Map<String,Object> configMap = (Map<String, Object>) config;
8787
String script = null;
8888
ScriptType type = null;
89-
String lang = DEFAULT_SCRIPT_LANG;
89+
String lang = null;
9090
Map<String, Object> params = Collections.emptyMap();
9191
for (Iterator<Map.Entry<String, Object>> itr = configMap.entrySet().iterator(); itr.hasNext();) {
9292
Map.Entry<String, Object> entry = itr.next();
@@ -126,7 +126,15 @@ private static Script parseScript(Object config) {
126126
}
127127
assert type != null : "if script is not null, type should definitely not be null";
128128

129-
return new Script(type, lang, script, params);
129+
if (type == ScriptType.STORED) {
130+
if (lang != null) {
131+
throw new IllegalArgumentException("lang cannot be specified for stored scripts");
132+
}
133+
134+
return new Script(type, null, script, null, params);
135+
} else {
136+
return new Script(type, lang == null ? DEFAULT_SCRIPT_LANG : lang, script, params);
137+
}
130138
} else {
131139
throw new IllegalArgumentException("Script value should be a String or a Map");
132140
}

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,84 @@
340340
source: if (ctx._source.user == "kimchy") {ctx.op = "index"} else {ctx.op = "junk"}
341341

342342
- match: { error.reason: 'Operation type [junk] not allowed, only [noop, index, delete] are allowed' }
343+
344+
---
345+
"Update all docs with one deletion and one noop using a stored script":
346+
- do:
347+
index:
348+
index: twitter
349+
type: tweet
350+
id: 1
351+
body: { "level": 9, "last_updated": "2016-01-01T12:10:30Z" }
352+
- do:
353+
index:
354+
index: twitter
355+
type: tweet
356+
id: 2
357+
body: { "level": 10, "last_updated": "2016-01-01T12:10:30Z" }
358+
- do:
359+
index:
360+
index: twitter
361+
type: tweet
362+
id: 3
363+
body: { "level": 11, "last_updated": "2016-01-01T12:10:30Z" }
364+
- do:
365+
index:
366+
index: twitter
367+
type: tweet
368+
id: 4
369+
body: { "level": 12, "last_updated": "2016-01-01T12:10:30Z" }
370+
- do:
371+
indices.refresh: {}
372+
- do:
373+
put_script:
374+
id: "my_update_script"
375+
body: { "script": {"lang": "painless",
376+
"source": "int choice = ctx._source.level % 3;
377+
if (choice == 0) {
378+
ctx._source.last_updated = '2016-01-02T00:00:00Z';
379+
} else if (choice == 1) {
380+
ctx.op = 'noop';
381+
} else {
382+
ctx.op = 'delete';
383+
}" } }
384+
- match: { acknowledged: true }
385+
386+
- do:
387+
update_by_query:
388+
refresh: true
389+
index: twitter
390+
body:
391+
script:
392+
id: "my_update_script"
393+
394+
- match: {updated: 2}
395+
- match: {deleted: 1}
396+
- match: {noops: 1}
397+
398+
- do:
399+
search:
400+
index: twitter
401+
body:
402+
query:
403+
match:
404+
last_updated: "2016-01-02T00:00:00Z"
405+
- match: { hits.total: 2 }
406+
407+
- do:
408+
search:
409+
index: twitter
410+
body:
411+
query:
412+
match:
413+
last_updated: "2016-01-01T12:10:30Z"
414+
- match: { hits.total: 1 }
415+
416+
- do:
417+
search:
418+
index: twitter
419+
body:
420+
query:
421+
term:
422+
level: 11
423+
- match: { hits.total: 0 }

0 commit comments

Comments
 (0)