Update by Query is modified to accept short `script` parameter. (#26841)

Update by Query is modified to accept short `script` parameter.

Closes issue #24898
This commit is contained in:
Anton Pozhidaev 2017-10-12 00:57:46 +03:00 committed by Nik Everett
parent f3942799bb
commit cee9640c20
2 changed files with 78 additions and 41 deletions

View File

@ -67,7 +67,7 @@ public class RestUpdateByQueryAction extends AbstractBulkByQueryRestHandler<Upda
Map<String, Consumer<Object>> consumers = new HashMap<>();
consumers.put("conflicts", o -> internal.setConflicts((String) o));
consumers.put("script", o -> internal.setScript(parseScript((Map<String, Object>)o)));
consumers.put("script", o -> internal.setScript(parseScript(o)));
parseInternalRequest(internal, request, consumers);
@ -76,12 +76,18 @@ public class RestUpdateByQueryAction extends AbstractBulkByQueryRestHandler<Upda
}
@SuppressWarnings("unchecked")
private static Script parseScript(Map<String, Object> config) {
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<String,Object> configMap = (Map<String, Object>) config;
String script = null;
ScriptType type = null;
String lang = DEFAULT_SCRIPT_LANG;
Map<String, Object> params = Collections.emptyMap();
for (Iterator<Map.Entry<String, Object>> itr = config.entrySet().iterator(); itr.hasNext();) {
for (Iterator<Map.Entry<String, Object>> itr = configMap.entrySet().iterator(); itr.hasNext();) {
Map.Entry<String, Object> entry = itr.next();
String parameterName = entry.getKey();
Object parameterValue = entry.getValue();
@ -120,5 +126,8 @@ public class RestUpdateByQueryAction extends AbstractBulkByQueryRestHandler<Upda
assert type != null : "if script is not null, type should definitely not be null";
return new Script(type, lang, script, params);
} else {
throw new IllegalArgumentException("Script value should be a String or a Map");
}
}
}

View File

@ -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: