Search in sources :

Example 6 with ExecutableScript

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

use of org.elasticsearch.script.ExecutableScript 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 8 with ExecutableScript

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

the class ScriptTestCase method exec.

/** Compiles and returns the result of {@code script} with access to {@code vars} and compile-time parameters */
public Object exec(String script, Map<String, Object> vars, Map<String, String> compileParams, Scorer scorer, boolean picky) {
    // test for ambiguity errors before running the actual script if picky is true
    if (picky) {
        ScriptInterface scriptInterface = new ScriptInterface(GenericElasticsearchScript.class);
        CompilerSettings pickySettings = new CompilerSettings();
        pickySettings.setPicky(true);
        pickySettings.setRegexesEnabled(CompilerSettings.REGEX_ENABLED.get(scriptEngineSettings()));
        Walker.buildPainlessTree(scriptInterface, getTestName(), script, pickySettings, null);
    }
    // test actual script execution
    Object object = scriptEngine.compile(null, script, compileParams);
    CompiledScript compiled = new CompiledScript(ScriptType.INLINE, getTestName(), "painless", object);
    ExecutableScript executableScript = scriptEngine.executable(compiled, vars);
    if (scorer != null) {
        ((ScorerAware) executableScript).setScorer(scorer);
    }
    return executableScript.run();
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) ScorerAware(org.elasticsearch.common.lucene.ScorerAware) ExecutableScript(org.elasticsearch.script.ExecutableScript)

Example 9 with ExecutableScript

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

the class AbstractAsyncBulkByScrollActionScriptTestCase method applyScript.

@SuppressWarnings("unchecked")
protected <T extends ActionRequest> T applyScript(Consumer<Map<String, Object>> scriptBody) {
    IndexRequest index = new IndexRequest("index", "type", "1").source(singletonMap("foo", "bar"));
    ScrollableHitSource.Hit doc = new ScrollableHitSource.BasicHit("test", "type", "id", 0);
    ExecutableScript executableScript = new SimpleExecutableScript(scriptBody);
    when(scriptService.executable(any(CompiledScript.class), Matchers.<Map<String, Object>>any())).thenReturn(executableScript);
    AbstractAsyncBulkByScrollAction<Request> action = action(scriptService, request().setScript(EMPTY_SCRIPT));
    RequestWrapper<?> result = action.buildScriptApplier().apply(AbstractAsyncBulkByScrollAction.wrap(index), doc);
    return (result != null) ? (T) result.self() : null;
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) ActionRequest(org.elasticsearch.action.ActionRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) ScrollableHitSource(org.elasticsearch.action.bulk.byscroll.ScrollableHitSource) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 10 with ExecutableScript

use of org.elasticsearch.script.ExecutableScript in project elasticsearch-river-rabbitmq by elastic.

the class RabbitmqRiver method buildScript.

/**
 * Build an executable script if provided as settings
 * @param settingName
 * @return
 */
private ExecutableScript buildScript(String settingName) {
    if (settings.settings().containsKey(settingName)) {
        Map<String, Object> scriptSettings = (Map<String, Object>) settings.settings().get(settingName);
        if (scriptSettings.containsKey("script")) {
            String scriptLang = "groovy";
            if (scriptSettings.containsKey("script_lang")) {
                scriptLang = scriptSettings.get("script_lang").toString();
            }
            Map<String, Object> scriptParams = null;
            if (scriptSettings.containsKey("script_params")) {
                scriptParams = (Map<String, Object>) scriptSettings.get("script_params");
            } else {
                scriptParams = Maps.newHashMap();
            }
            return scriptService.executable(new Script(scriptLang, scriptSettings.get("script").toString(), ScriptService.ScriptType.INLINE, scriptParams), ScriptContext.Standard.UPDATE);
        }
    }
    return null;
}
Also used : Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ExecutableScript (org.elasticsearch.script.ExecutableScript)24 CompiledScript (org.elasticsearch.script.CompiledScript)15 HashMap (java.util.HashMap)9 Script (org.elasticsearch.script.Script)9 Map (java.util.Map)8 BytesReference (org.elasticsearch.common.bytes.BytesReference)7 Mustache (com.github.mustachejava.Mustache)4 ArrayList (java.util.ArrayList)4 XContentParser (org.elasticsearch.common.xcontent.XContentParser)4 IOException (java.io.IOException)3 ScriptEngineService (org.elasticsearch.script.ScriptEngineService)3 SearchScript (org.elasticsearch.script.SearchScript)3 List (java.util.List)2 IndexReader (org.apache.lucene.index.IndexReader)2 MapperService (org.elasticsearch.index.mapper.MapperService)2 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)2 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1