Search in sources :

Example 6 with ScriptType

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);
}
Also used : ScriptType(org.elasticsearch.script.ScriptType) Script(org.elasticsearch.script.Script) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with ScriptType

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ScriptType(org.elasticsearch.script.ScriptType) Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) IndexRequest(org.elasticsearch.action.index.IndexRequest) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

Script (org.elasticsearch.script.Script)7 ScriptType (org.elasticsearch.script.ScriptType)7 HashMap (java.util.HashMap)5 Map (java.util.Map)1 TestUtil.randomSimpleString (org.apache.lucene.util.TestUtil.randomSimpleString)1 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)1 IndexRequest (org.elasticsearch.action.index.IndexRequest)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 ToXContentObject (org.elasticsearch.common.xcontent.ToXContentObject)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1 XContentType (org.elasticsearch.common.xcontent.XContentType)1 BucketScriptPipelineAggregationBuilder (org.elasticsearch.search.aggregations.pipeline.bucketscript.BucketScriptPipelineAggregationBuilder)1 BucketSelectorPipelineAggregationBuilder (org.elasticsearch.search.aggregations.pipeline.bucketselector.BucketSelectorPipelineAggregationBuilder)1