Search in sources :

Example 1 with ScriptType

use of org.opensearch.script.ScriptType in project OpenSearch by opensearch-project.

the class BucketScriptTests method createTestAggregatorFactory.

@Override
protected BucketScriptPipelineAggregationBuilder createTestAggregatorFactory() {
    String name = randomAlphaOfLengthBetween(3, 20);
    Map<String, String> bucketsPaths = new HashMap<>();
    int numBucketPaths = randomIntBetween(1, 10);
    for (int i = 0; i < numBucketPaths; i++) {
        bucketsPaths.put(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 40));
    }
    Script script;
    if (randomBoolean()) {
        script = mockScript("script");
    } else {
        Map<String, Object> params = new HashMap<>();
        if (randomBoolean()) {
            params.put("foo", "bar");
        }
        ScriptType type = randomFrom(ScriptType.values());
        script = new Script(type, type == ScriptType.STORED ? null : randomFrom("my_lang", Script.DEFAULT_SCRIPT_LANG), "script", params);
    }
    BucketScriptPipelineAggregationBuilder factory = new BucketScriptPipelineAggregationBuilder(name, bucketsPaths, script);
    if (randomBoolean()) {
        factory.format(randomAlphaOfLengthBetween(1, 10));
    }
    if (randomBoolean()) {
        factory.gapPolicy(randomFrom(GapPolicy.values()));
    }
    return factory;
}
Also used : Script(org.opensearch.script.Script) ScriptType(org.opensearch.script.ScriptType) HashMap(java.util.HashMap)

Example 2 with ScriptType

use of org.opensearch.script.ScriptType in project OpenSearch by opensearch-project.

the class UpdateRequest method updateOrCreateScript.

private void updateOrCreateScript(String scriptContent, ScriptType type, String lang, Map<String, Object> params) {
    Script script = script();
    if (script == null) {
        script = new Script(type == null ? ScriptType.INLINE : type, lang, scriptContent == null ? "" : scriptContent, params);
    } else {
        String newScriptContent = scriptContent == null ? script.getIdOrCode() : scriptContent;
        ScriptType newScriptType = type == null ? script.getType() : type;
        String newScriptLang = lang == null ? script.getLang() : lang;
        Map<String, Object> newScriptParams = params == null ? script.getParams() : params;
        script = new Script(newScriptType, newScriptLang, newScriptContent, newScriptParams);
    }
    script(script);
}
Also used : Script(org.opensearch.script.Script) ScriptType(org.opensearch.script.ScriptType) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject)

Example 3 with ScriptType

use of org.opensearch.script.ScriptType in project OpenSearch by opensearch-project.

the class RoundTripTests method randomScript.

private Script randomScript() {
    ScriptType type = randomFrom(ScriptType.values());
    String lang = random().nextBoolean() ? Script.DEFAULT_SCRIPT_LANG : randomSimpleString(random());
    String idOrCode = randomSimpleString(random());
    Map<String, Object> params = Collections.emptyMap();
    type = ScriptType.STORED;
    return new Script(type, type == ScriptType.STORED ? null : lang, idOrCode, params);
}
Also used : ScriptType(org.opensearch.script.ScriptType) Script(org.opensearch.script.Script) TestUtil.randomSimpleString(org.apache.lucene.util.TestUtil.randomSimpleString)

Example 4 with ScriptType

use of org.opensearch.script.ScriptType in project OpenSearch by opensearch-project.

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) ? randomAlphaOfLength(10) : null;
        String scriptIdOrCode = randomAlphaOfLength(10);
        int nbScriptParams = randomIntBetween(0, 5);
        Map<String, Object> scriptParams = new HashMap<>(nbScriptParams);
        for (int i = 0; i < nbScriptParams; i++) {
            scriptParams.put(randomAlphaOfLength(5), randomAlphaOfLength(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()) {
        if (randomBoolean()) {
            updateRequest.fetchSource(randomBoolean());
        } else {
            String[] includes = new String[randomIntBetween(0, 5)];
            for (int i = 0; i < includes.length; i++) {
                includes[i] = randomAlphaOfLength(5);
            }
            String[] excludes = new String[randomIntBetween(0, 5)];
            for (int i = 0; i < excludes.length; i++) {
                excludes[i] = randomAlphaOfLength(5);
            }
            if (randomBoolean()) {
                updateRequest.fetchSource(includes, excludes);
            }
        }
    }
    XContentType xContentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toShuffledXContent(updateRequest, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
    if (randomBoolean()) {
        try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
            originalBytes = BytesReference.bytes(shuffleXContent(parser, randomBoolean()));
        }
    }
    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.script(), parsedUpdateRequest.script());
    assertEquals(updateRequest.scriptedUpsert(), parsedUpdateRequest.scriptedUpsert());
    assertEquals(updateRequest.fetchSource(), parsedUpdateRequest.fetchSource());
    BytesReference finalBytes = toXContent(parsedUpdateRequest, xContentType, humanReadable);
    assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) ScriptType(org.opensearch.script.ScriptType) MockScriptEngine.mockInlineScript(org.opensearch.script.MockScriptEngine.mockInlineScript) Script(org.opensearch.script.Script) HashMap(java.util.HashMap) IndexRequest(org.opensearch.action.index.IndexRequest) XContentType(org.opensearch.common.xcontent.XContentType) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 5 with ScriptType

use of org.opensearch.script.ScriptType in project OpenSearch by opensearch-project.

the class BucketSelectorTests method createTestAggregatorFactory.

@Override
protected BucketSelectorPipelineAggregationBuilder createTestAggregatorFactory() {
    String name = randomAlphaOfLengthBetween(3, 20);
    Map<String, String> bucketsPaths = new HashMap<>();
    int numBucketPaths = randomIntBetween(1, 10);
    for (int i = 0; i < numBucketPaths; i++) {
        bucketsPaths.put(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 40));
    }
    Script script;
    if (randomBoolean()) {
        script = mockScript("script");
    } else {
        Map<String, Object> params = new HashMap<>();
        if (randomBoolean()) {
            params.put("foo", "bar");
        }
        ScriptType type = randomFrom(ScriptType.values());
        script = new Script(type, type == ScriptType.STORED ? null : randomFrom("my_lang", Script.DEFAULT_SCRIPT_LANG), "script", params);
    }
    BucketSelectorPipelineAggregationBuilder factory = new BucketSelectorPipelineAggregationBuilder(name, bucketsPaths, script);
    if (randomBoolean()) {
        factory.gapPolicy(randomFrom(GapPolicy.values()));
    }
    return factory;
}
Also used : Script(org.opensearch.script.Script) ScriptType(org.opensearch.script.ScriptType) HashMap(java.util.HashMap)

Aggregations

Script (org.opensearch.script.Script)6 ScriptType (org.opensearch.script.ScriptType)6 HashMap (java.util.HashMap)4 TestUtil.randomSimpleString (org.apache.lucene.util.TestUtil.randomSimpleString)1 IndexRequest (org.opensearch.action.index.IndexRequest)1 BytesReference (org.opensearch.common.bytes.BytesReference)1 ToXContentObject (org.opensearch.common.xcontent.ToXContentObject)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 XContentType (org.opensearch.common.xcontent.XContentType)1 MockScriptEngine.mockInlineScript (org.opensearch.script.MockScriptEngine.mockInlineScript)1