Skip to content

Commit

Permalink
Update by Query is modified to accept short script parameter. (elas…
Browse files Browse the repository at this point in the history
…tic#26841)

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

Closes issue elastic#24898
  • Loading branch information
pozhidaevak authored and nik9000 committed Oct 11, 2017
1 parent f394279 commit cee9640
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 41 deletions.
Expand Up @@ -67,7 +67,7 @@ protected UpdateByQueryRequest buildRequest(RestRequest request) throws IOExcept

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);

Expand All @@ -76,49 +76,58 @@ protected UpdateByQueryRequest buildRequest(RestRequest request) throws IOExcept
}

@SuppressWarnings("unchecked")
private static Script parseScript(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();) {
Map.Entry<String, Object> 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<String, Object>) 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<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 = configMap.entrySet().iterator(); itr.hasNext();) {
Map.Entry<String, Object> 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<String, Object>) 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");
}
}
}
Expand Up @@ -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:
Expand Down

0 comments on commit cee9640

Please sign in to comment.