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