use of org.elasticsearch.script.CompiledScript in project elasticsearch by elastic.
the class AbstractSortTestCase method init.
@BeforeClass
public static void init() throws IOException {
Path genericConfigFolder = createTempDir();
Settings baseSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put(Environment.PATH_CONF_SETTING.getKey(), genericConfigFolder).build();
Environment environment = new Environment(baseSettings);
ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(new TestEngineService()));
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
scriptService = new ScriptService(baseSettings, environment, new ResourceWatcherService(baseSettings, null), scriptEngineRegistry, scriptContextRegistry, scriptSettings) {
@Override
public CompiledScript compile(Script script, ScriptContext scriptContext) {
return new CompiledScript(ScriptType.INLINE, "mockName", "test", script);
}
};
SearchModule searchModule = new SearchModule(Settings.EMPTY, false, emptyList());
namedWriteableRegistry = new NamedWriteableRegistry(searchModule.getNamedWriteables());
xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
}
use of org.elasticsearch.script.CompiledScript in project elasticsearch by elastic.
the class MoreExpressionTests method testExecutableScripts.
// series of unit test for using expressions as executable scripts
public void testExecutableScripts() throws Exception {
assumeTrue("test creates classes directly, cannot run with security manager", System.getSecurityManager() == null);
Map<String, Object> vars = new HashMap<>();
vars.put("a", 2.5);
vars.put("b", 3);
vars.put("xyz", -1);
Expression expr = JavascriptCompiler.compile("a+b+xyz");
CompiledScript compiledScript = new CompiledScript(ScriptType.INLINE, "", "expression", expr);
ExpressionExecutableScript ees = new ExpressionExecutableScript(compiledScript, vars);
assertEquals((Double) ees.run(), 4.5, 0.001);
ees.setNextVar("b", -2.5);
assertEquals((Double) ees.run(), -1, 0.001);
ees.setNextVar("a", -2.5);
ees.setNextVar("b", -2.5);
ees.setNextVar("xyz", -2.5);
assertEquals((Double) ees.run(), -7.5, 0.001);
String message;
try {
vars = new HashMap<>();
vars.put("a", 1);
ees = new ExpressionExecutableScript(compiledScript, vars);
ees.run();
fail("An incorrect number of variables were allowed to be used in an expression.");
} catch (GeneralScriptException se) {
message = se.getMessage();
assertThat(message + " should have contained number of variables", message.contains("number of variables"), equalTo(true));
}
try {
vars = new HashMap<>();
vars.put("a", 1);
vars.put("b", 3);
vars.put("c", -1);
ees = new ExpressionExecutableScript(compiledScript, vars);
ees.run();
fail("A variable was allowed to be set that does not exist in the expression.");
} catch (GeneralScriptException se) {
message = se.getMessage();
assertThat(message + " should have contained does not exist in", message.contains("does not exist in"), equalTo(true));
}
try {
vars = new HashMap<>();
vars.put("a", 1);
vars.put("b", 3);
vars.put("xyz", "hello");
ees = new ExpressionExecutableScript(compiledScript, vars);
ees.run();
fail("A non-number was allowed to be use in the expression.");
} catch (GeneralScriptException se) {
message = se.getMessage();
assertThat(message + " should have contained process numbers", message.contains("process numbers"), equalTo(true));
}
}
use of org.elasticsearch.script.CompiledScript in project elasticsearch by elastic.
the class MustacheScriptEngineTests method testSimple.
public void testSimple() throws IOException {
String templateString = "{" + "\"inline\":{\"match_{{template}}\": {}}," + "\"params\":{\"template\":\"all\"}" + "}";
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 testBasics.
public void testBasics() {
String template = "GET _search {\"query\": " + "{\"boosting\": {" + "\"positive\": {\"match\": {\"body\": \"gift\"}}," + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" + "}}, \"negative_boost\": {{boost_val}} } }}";
Map<String, Object> params = Collections.singletonMap("boost_val", "0.2");
Mustache mustache = (Mustache) engine.compile(null, template, Collections.emptyMap());
CompiledScript compiledScript = new CompiledScript(INLINE, "my-name", "mustache", mustache);
ExecutableScript result = engine.executable(compiledScript, params);
assertEquals("Mustache templating broken", "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.2 } }}", ((BytesReference) result.run()).utf8ToString());
}
use of org.elasticsearch.script.CompiledScript in project elasticsearch by elastic.
the class MustacheTests method testSizeAccessForCollectionsAndArrays.
public void testSizeAccessForCollectionsAndArrays() throws Exception {
String[] randomArrayValues = generateRandomStringArray(10, 20, false);
List<String> randomList = Arrays.asList(generateRandomStringArray(10, 20, false));
String template = "{{data.array.size}} {{data.list.size}}";
CompiledScript mustache = new CompiledScript(INLINE, "inline", "mustache", engine.compile(null, template, Collections.emptyMap()));
Map<String, Object> data = new HashMap<>();
data.put("array", randomArrayValues);
data.put("list", randomList);
Map<String, Object> vars = new HashMap<>();
vars.put("data", data);
Object output = engine.executable(mustache, vars).run();
assertThat(output, notNullValue());
assertThat(output, instanceOf(BytesReference.class));
BytesReference bytes = (BytesReference) output;
String expectedString = String.format(Locale.ROOT, "%s %s", randomArrayValues.length, randomList.size());
assertThat(bytes.utf8ToString(), equalTo(expectedString));
}
Aggregations