use of org.elasticsearch.script.ScriptType in project elasticsearch by elastic.
the class RestUpdateByQueryAction method parseScript.
@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.FILE.getParseField().match(parameterName)) {
if (parameterValue instanceof String || parameterValue == null) {
script = (String) parameterValue;
type = ScriptType.FILE;
} 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", ScriptType.INLINE.getParseField().getPreferredName(), ScriptType.FILE.getParseField().getPreferredName(), ScriptType.STORED.getParseField().getPreferredName());
}
assert type != null : "if script is not null, type should definitely not be null";
return new Script(type, lang, script, params);
}
use of org.elasticsearch.script.ScriptType in project elasticsearch by elastic.
the class UpdateRequestTests method testToAndFromXContent.
public void testToAndFromXContent() throws IOException {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.detectNoop(randomBoolean());
if (randomBoolean()) {
XContentType xContentType = randomFrom(XContentType.values());
BytesReference source = RandomObjects.randomSource(random(), xContentType);
updateRequest.doc(new IndexRequest().source(source, xContentType));
updateRequest.docAsUpsert(randomBoolean());
} else {
ScriptType scriptType = randomFrom(ScriptType.values());
String scriptLang = (scriptType != ScriptType.STORED) ? randomAsciiOfLength(10) : null;
String scriptIdOrCode = randomAsciiOfLength(10);
int nbScriptParams = randomIntBetween(0, 5);
Map<String, Object> scriptParams = new HashMap<>(nbScriptParams);
for (int i = 0; i < nbScriptParams; i++) {
scriptParams.put(randomAsciiOfLength(5), randomAsciiOfLength(5));
}
updateRequest.script(new Script(scriptType, scriptLang, scriptIdOrCode, scriptParams));
updateRequest.scriptedUpsert(randomBoolean());
}
if (randomBoolean()) {
XContentType xContentType = randomFrom(XContentType.values());
BytesReference source = RandomObjects.randomSource(random(), xContentType);
updateRequest.upsert(new IndexRequest().source(source, xContentType));
}
if (randomBoolean()) {
String[] fields = new String[randomIntBetween(0, 5)];
for (int i = 0; i < fields.length; i++) {
fields[i] = randomAsciiOfLength(5);
}
updateRequest.fields(fields);
}
if (randomBoolean()) {
if (randomBoolean()) {
updateRequest.fetchSource(randomBoolean());
} else {
String[] includes = new String[randomIntBetween(0, 5)];
for (int i = 0; i < includes.length; i++) {
includes[i] = randomAsciiOfLength(5);
}
String[] excludes = new String[randomIntBetween(0, 5)];
for (int i = 0; i < excludes.length; i++) {
excludes[i] = randomAsciiOfLength(5);
}
if (randomBoolean()) {
updateRequest.fetchSource(includes, excludes);
}
}
}
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = XContentHelper.toXContent(updateRequest, xContentType, humanReadable);
if (randomBoolean()) {
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
originalBytes = shuffleXContent(parser, randomBoolean()).bytes();
}
}
UpdateRequest parsedUpdateRequest = new UpdateRequest();
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
parsedUpdateRequest.fromXContent(parser);
assertNull(parser.nextToken());
}
assertEquals(updateRequest.detectNoop(), parsedUpdateRequest.detectNoop());
assertEquals(updateRequest.docAsUpsert(), parsedUpdateRequest.docAsUpsert());
assertEquals(updateRequest.docAsUpsert(), parsedUpdateRequest.docAsUpsert());
assertEquals(updateRequest.script(), parsedUpdateRequest.script());
assertEquals(updateRequest.scriptedUpsert(), parsedUpdateRequest.scriptedUpsert());
assertArrayEquals(updateRequest.fields(), parsedUpdateRequest.fields());
assertEquals(updateRequest.fetchSource(), parsedUpdateRequest.fetchSource());
BytesReference finalBytes = toXContent(parsedUpdateRequest, xContentType, humanReadable);
assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
}
Aggregations