Search in sources :

Example 6 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 7 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)

Example 8 with CompiledScript

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

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

the class NeedsScoreTests method testNeedsScores.

public void testNeedsScores() {
    IndexService index = createIndex("test", Settings.EMPTY, "type", "d", "type=double");
    PainlessScriptEngineService service = new PainlessScriptEngineService(Settings.EMPTY);
    SearchLookup lookup = new SearchLookup(index.mapperService(), index.fieldData(), null);
    Object compiled = service.compile(null, "1.2", Collections.emptyMap());
    SearchScript ss = service.search(new CompiledScript(ScriptType.INLINE, "randomName", "painless", compiled), lookup, Collections.<String, Object>emptyMap());
    assertFalse(ss.needsScores());
    compiled = service.compile(null, "doc['d'].value", Collections.emptyMap());
    ss = service.search(new CompiledScript(ScriptType.INLINE, "randomName", "painless", compiled), lookup, Collections.<String, Object>emptyMap());
    assertFalse(ss.needsScores());
    compiled = service.compile(null, "1/_score", Collections.emptyMap());
    ss = service.search(new CompiledScript(ScriptType.INLINE, "randomName", "painless", compiled), lookup, Collections.<String, Object>emptyMap());
    assertTrue(ss.needsScores());
    compiled = service.compile(null, "doc['d'].value * _score", Collections.emptyMap());
    ss = service.search(new CompiledScript(ScriptType.INLINE, "randomName", "painless", compiled), lookup, Collections.<String, Object>emptyMap());
    assertTrue(ss.needsScores());
    service.close();
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) SearchScript(org.elasticsearch.script.SearchScript) IndexService(org.elasticsearch.index.IndexService) SearchLookup(org.elasticsearch.search.lookup.SearchLookup)

Example 10 with CompiledScript

use of org.elasticsearch.script.CompiledScript 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)

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