Search in sources :

Example 1 with ScriptType

use of org.elasticsearch.script.ScriptType in project elasticsearch by elastic.

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.elasticsearch.script.Script) ScriptType(org.elasticsearch.script.ScriptType) ToXContentObject(org.elasticsearch.common.xcontent.ToXContentObject)

Example 2 with ScriptType

use of org.elasticsearch.script.ScriptType in project elasticsearch by elastic.

the class BucketSelectorTests method createTestAggregatorFactory.

@Override
protected BucketSelectorPipelineAggregationBuilder createTestAggregatorFactory() {
    String name = randomAsciiOfLengthBetween(3, 20);
    Map<String, String> bucketsPaths = new HashMap<>();
    int numBucketPaths = randomIntBetween(1, 10);
    for (int i = 0; i < numBucketPaths; i++) {
        bucketsPaths.put(randomAsciiOfLengthBetween(1, 20), randomAsciiOfLengthBetween(1, 40));
    }
    Script script;
    if (randomBoolean()) {
        script = new Script("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.elasticsearch.script.Script) ScriptType(org.elasticsearch.script.ScriptType) HashMap(java.util.HashMap) BucketSelectorPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketselector.BucketSelectorPipelineAggregationBuilder)

Example 3 with ScriptType

use of org.elasticsearch.script.ScriptType in project elasticsearch by elastic.

the class BucketScriptTests method createTestAggregatorFactory.

@Override
protected BucketScriptPipelineAggregationBuilder createTestAggregatorFactory() {
    String name = randomAsciiOfLengthBetween(3, 20);
    Map<String, String> bucketsPaths = new HashMap<>();
    int numBucketPaths = randomIntBetween(1, 10);
    for (int i = 0; i < numBucketPaths; i++) {
        bucketsPaths.put(randomAsciiOfLengthBetween(1, 20), randomAsciiOfLengthBetween(1, 40));
    }
    Script script;
    if (randomBoolean()) {
        script = new Script("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(randomAsciiOfLengthBetween(1, 10));
    }
    if (randomBoolean()) {
        factory.gapPolicy(randomFrom(GapPolicy.values()));
    }
    return factory;
}
Also used : Script(org.elasticsearch.script.Script) ScriptType(org.elasticsearch.script.ScriptType) BucketScriptPipelineAggregationBuilder(org.elasticsearch.search.aggregations.pipeline.bucketscript.BucketScriptPipelineAggregationBuilder) HashMap(java.util.HashMap)

Example 4 with ScriptType

use of org.elasticsearch.script.ScriptType in project elasticsearch by elastic.

the class InnerHitBuilderTests method randomScript.

static SearchSourceBuilder.ScriptField randomScript() {
    ScriptType randomScriptType = randomFrom(ScriptType.values());
    Map<String, Object> randomMap = new HashMap<>();
    if (randomBoolean()) {
        int numEntries = randomIntBetween(0, 32);
        for (int i = 0; i < numEntries; i++) {
            randomMap.put(String.valueOf(i), randomAsciiOfLength(16));
        }
    }
    Script script = new Script(randomScriptType, randomScriptType == ScriptType.STORED ? null : randomAsciiOfLengthBetween(1, 4), randomAsciiOfLength(128), randomMap);
    return new SearchSourceBuilder.ScriptField(randomAsciiOfLengthBetween(1, 32), script, randomBoolean());
}
Also used : ScriptType(org.elasticsearch.script.ScriptType) Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap)

Example 5 with ScriptType

use of org.elasticsearch.script.ScriptType in project elasticsearch by elastic.

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();
    return new Script(type, lang, idOrCode, params);
}
Also used : ScriptType(org.elasticsearch.script.ScriptType) Script(org.elasticsearch.script.Script) TestUtil.randomSimpleString(org.apache.lucene.util.TestUtil.randomSimpleString)

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