Search in sources :

Example 1 with ScriptEngineService

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

the class CustomMustacheFactoryTests method testDefaultEncoder.

public void testDefaultEncoder() {
    final ScriptEngineService engine = new MustacheScriptEngineService();
    final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, PLAIN_TEXT_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", "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 2 with ScriptEngineService

use of org.elasticsearch.script.ScriptEngineService 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 3 with ScriptEngineService

use of org.elasticsearch.script.ScriptEngineService 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)

Aggregations

Mustache (com.github.mustachejava.Mustache)3 BytesReference (org.elasticsearch.common.bytes.BytesReference)3 CompiledScript (org.elasticsearch.script.CompiledScript)3 ExecutableScript (org.elasticsearch.script.ExecutableScript)3 ScriptEngineService (org.elasticsearch.script.ScriptEngineService)3