Search in sources :

Example 21 with ExecutableScript

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

the class TransportSearchTemplateAction method doExecute.

@Override
protected void doExecute(SearchTemplateRequest request, ActionListener<SearchTemplateResponse> listener) {
    final SearchTemplateResponse response = new SearchTemplateResponse();
    try {
        Script script = new Script(request.getScriptType(), TEMPLATE_LANG, request.getScript(), request.getScriptParams() == null ? Collections.emptyMap() : request.getScriptParams());
        ExecutableScript executable = scriptService.executable(script, SEARCH);
        BytesReference source = (BytesReference) executable.run();
        response.setSource(source);
        if (request.isSimulate()) {
            listener.onResponse(response);
            return;
        }
        // Executes the search
        SearchRequest searchRequest = request.getRequest();
        //we can assume the template is always json as we convert it before compiling it
        try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(xContentRegistry, source)) {
            SearchSourceBuilder builder = SearchSourceBuilder.searchSource();
            builder.parseXContent(new QueryParseContext(parser));
            builder.explain(request.isExplain());
            builder.profile(request.isProfile());
            searchRequest.source(builder);
            searchAction.execute(searchRequest, new ActionListener<SearchResponse>() {

                @Override
                public void onResponse(SearchResponse searchResponse) {
                    try {
                        response.setResponse(searchResponse);
                        listener.onResponse(response);
                    } catch (Exception t) {
                        listener.onFailure(t);
                    }
                }

                @Override
                public void onFailure(Exception t) {
                    listener.onFailure(t);
                }
            });
        }
    } catch (Exception t) {
        listener.onFailure(t);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchRequest(org.elasticsearch.action.search.SearchRequest) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) ExecutableScript(org.elasticsearch.script.ExecutableScript) XContentParser(org.elasticsearch.common.xcontent.XContentParser) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 22 with ExecutableScript

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

the class CustomMustacheFactoryTests method testUrlEncoder.

public void testUrlEncoder() {
    final ScriptEngineService engine = new MustacheScriptEngineService();
    final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, X_WWW_FORM_URLENCODED_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", "tilde~ AND date:[2016 FROM*]"));
    BytesReference result = (BytesReference) executable.run();
    assertThat(result.utf8ToString(), equalTo("{\"field\": \"tilde%7E+AND+date%3A%5B2016+FROM*%5D\"}"));
}
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 23 with ExecutableScript

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

the class CustomMustacheFactoryTests method testJsonEscapeEncoder.

public void testJsonEscapeEncoder() {
    final ScriptEngineService engine = new MustacheScriptEngineService();
    final Map<String, String> params = randomBoolean() ? singletonMap(Script.CONTENT_TYPE_OPTION, JSON_MIME_TYPE) : emptyMap();
    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 24 with ExecutableScript

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

the class MustacheScriptEngineTests method testParseTemplateAsSingleStringWithConditionalClause.

public void testParseTemplateAsSingleStringWithConditionalClause() throws IOException {
    String templateString = "{" + "  \"inline\" : \"{ \\\"match_{{#use_it}}{{template}}{{/use_it}}\\\":{} }\"," + "  \"params\":{" + "    \"template\":\"all\"," + "    \"use_it\": true" + "  }" + "}";
    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)

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