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());
}
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\"}"));
}
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\\\"\"}"));
}
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\":{} }"));
}
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"));
}
Aggregations