Search in sources :

Example 1 with CompiledScript

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());
}
Also used : Path(java.nio.file.Path) ContentPath(org.elasticsearch.index.mapper.ContentPath) CompiledScript(org.elasticsearch.script.CompiledScript) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ScriptContext(org.elasticsearch.script.ScriptContext) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry) ScriptService(org.elasticsearch.script.ScriptService) ScriptSettings(org.elasticsearch.script.ScriptSettings) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) Environment(org.elasticsearch.env.Environment) SearchModule(org.elasticsearch.search.SearchModule) TestEngineService(org.elasticsearch.script.ScriptServiceTests.TestEngineService) ResourceWatcherService(org.elasticsearch.watcher.ResourceWatcherService) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Settings(org.elasticsearch.common.settings.Settings) ScriptSettings(org.elasticsearch.script.ScriptSettings) IndexSettings(org.elasticsearch.index.IndexSettings) BeforeClass(org.junit.BeforeClass)

Example 2 with CompiledScript

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));
    }
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) HashMap(java.util.HashMap) Expression(org.apache.lucene.expressions.Expression) GeneralScriptException(org.elasticsearch.script.GeneralScriptException)

Example 3 with CompiledScript

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\":{}}"));
}
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 4 with CompiledScript

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());
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) Mustache(com.github.mustachejava.Mustache) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 5 with CompiledScript

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));
}
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