Search in sources :

Example 16 with CompiledScript

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

the class ScriptEngineTests method testChangingVarsCrossExecution2.

public void testChangingVarsCrossExecution2() {
    Map<String, Object> vars = new HashMap<>();
    Object compiledScript = scriptEngine.compile(null, "return params['value'];", Collections.emptyMap());
    ExecutableScript script = scriptEngine.executable(new CompiledScript(ScriptType.INLINE, "testChangingVarsCrossExecution2", "painless", compiledScript), vars);
    script.setNextVar("value", 1);
    Object value = script.run();
    assertEquals(1, ((Number) value).intValue());
    script.setNextVar("value", 2);
    value = script.run();
    assertEquals(2, ((Number) value).intValue());
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) HashMap(java.util.HashMap) ExecutableScript(org.elasticsearch.script.ExecutableScript)

Example 17 with CompiledScript

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

the class CustomMustacheFactoryTests method testUrlEncoder.

public void testUrlEncoder() {
    final ScriptEngineService engine = new MustacheScriptEngineService();
    final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, X_WWW_FORM_URLENCODED_MIME_TYPE);
    Mustache script = (Mustache) engine.compile(null, "{\"field\": \"{{value}}\"}", params);
    CompiledScript compiled = new CompiledScript(INLINE, null, MustacheScriptEngineService.NAME, script);
    ExecutableScript executable = engine.executable(compiled, singletonMap("value", "tilde~ AND date:[2016 FROM*]"));
    BytesReference result = (BytesReference) executable.run();
    assertThat(result.utf8ToString(), equalTo("{\"field\": \"tilde%7E+AND+date%3A%5B2016+FROM*%5D\"}"));
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) BytesReference(org.elasticsearch.common.bytes.BytesReference) ExecutableScript(org.elasticsearch.script.ExecutableScript) Mustache(com.github.mustachejava.Mustache) ScriptEngineService(org.elasticsearch.script.ScriptEngineService)

Example 18 with CompiledScript

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

the class CustomMustacheFactoryTests method testJsonEscapeEncoder.

public void testJsonEscapeEncoder() {
    final ScriptEngineService engine = new MustacheScriptEngineService();
    final Map<String, String> params = randomBoolean() ? singletonMap(Script.CONTENT_TYPE_OPTION, JSON_MIME_TYPE) : emptyMap();
    Mustache script = (Mustache) engine.compile(null, "{\"field\": \"{{value}}\"}", params);
    CompiledScript compiled = new CompiledScript(INLINE, null, MustacheScriptEngineService.NAME, script);
    ExecutableScript executable = engine.executable(compiled, singletonMap("value", "a \"value\""));
    BytesReference result = (BytesReference) executable.run();
    assertThat(result.utf8ToString(), equalTo("{\"field\": \"a \\\"value\\\"\"}"));
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) BytesReference(org.elasticsearch.common.bytes.BytesReference) ExecutableScript(org.elasticsearch.script.ExecutableScript) Mustache(com.github.mustachejava.Mustache) ScriptEngineService(org.elasticsearch.script.ScriptEngineService)

Example 19 with CompiledScript

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

the class MustacheScriptEngineTests method testParseTemplateAsSingleStringWithConditionalClause.

public void testParseTemplateAsSingleStringWithConditionalClause() throws IOException {
    String templateString = "{" + "  \"inline\" : \"{ \\\"match_{{#use_it}}{{template}}{{/use_it}}\\\":{} }\"," + "  \"params\":{" + "    \"template\":\"all\"," + "    \"use_it\": true" + "  }" + "}";
    XContentParser parser = createParser(JsonXContent.jsonXContent, templateString);
    Script script = Script.parse(parser);
    CompiledScript compiledScript = new CompiledScript(ScriptType.INLINE, null, "mustache", qe.compile(null, script.getIdOrCode(), Collections.emptyMap()));
    ExecutableScript executableScript = qe.executable(compiledScript, script.getParams());
    assertThat(((BytesReference) executableScript.run()).utf8ToString(), equalTo("{ \"match_all\":{} }"));
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 20 with CompiledScript

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

the class MustacheTests method testArrayInArrayAccess.

public void testArrayInArrayAccess() throws Exception {
    String template = "{{data.0.0}} {{data.0.1}}";
    CompiledScript mustache = new CompiledScript(INLINE, "inline", "mustache", engine.compile(null, template, Collections.emptyMap()));
    Map<String, Object> vars = new HashMap<>();
    Object data = randomFrom(new String[][] { new String[] { "foo", "bar" } }, Collections.singletonList(new String[] { "foo", "bar" }), singleton(new String[] { "foo", "bar" }));
    vars.put("data", data);
    Object output = engine.executable(mustache, vars).run();
    assertThat(output, notNullValue());
    assertThat(output, instanceOf(BytesReference.class));
    BytesReference bytes = (BytesReference) output;
    assertThat(bytes.utf8ToString(), equalTo("foo bar"));
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) BytesReference(org.elasticsearch.common.bytes.BytesReference) HashMap(java.util.HashMap) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

CompiledScript (org.elasticsearch.script.CompiledScript)22 ExecutableScript (org.elasticsearch.script.ExecutableScript)15 HashMap (java.util.HashMap)12 BytesReference (org.elasticsearch.common.bytes.BytesReference)10 Script (org.elasticsearch.script.Script)7 Map (java.util.Map)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)5 Mustache (com.github.mustachejava.Mustache)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 NamedXContentRegistry (org.elasticsearch.common.xcontent.NamedXContentRegistry)3 IndexSettings (org.elasticsearch.index.IndexSettings)3 ScriptContext (org.elasticsearch.script.ScriptContext)3 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Function (java.util.function.Function)2