Search in sources :

Example 6 with TemplateScript

use of org.opensearch.script.TemplateScript in project OpenSearch by opensearch-project.

the class MustacheScriptEngineTests method testSimple.

public void testSimple() throws IOException {
    String templateString = "{" + "\"source\":{\"match_{{template}}\": {}}," + "\"params\":{\"template\":\"all\"}" + "}";
    XContentParser parser = createParser(JsonXContent.jsonXContent, templateString);
    Script script = Script.parse(parser);
    TemplateScript.Factory compiled = qe.compile(null, script.getIdOrCode(), TemplateScript.CONTEXT, Collections.emptyMap());
    TemplateScript TemplateScript = compiled.newInstance(script.getParams());
    assertThat(TemplateScript.execute(), equalTo("{\"match_all\":{}}"));
}
Also used : Script(org.opensearch.script.Script) TemplateScript(org.opensearch.script.TemplateScript) XContentParser(org.opensearch.common.xcontent.XContentParser) TemplateScript(org.opensearch.script.TemplateScript)

Example 7 with TemplateScript

use of org.opensearch.script.TemplateScript in project OpenSearch by opensearch-project.

the class CustomMustacheFactoryTests method testJsonEscapeEncoder.

public void testJsonEscapeEncoder() {
    final ScriptEngine engine = new MustacheScriptEngine();
    final Map<String, String> params = randomBoolean() ? singletonMap(Script.CONTENT_TYPE_OPTION, JSON_MIME_TYPE) : emptyMap();
    TemplateScript.Factory compiled = engine.compile(null, "{\"field\": \"{{value}}\"}", TemplateScript.CONTEXT, params);
    TemplateScript executable = compiled.newInstance(singletonMap("value", "a \"value\""));
    assertThat(executable.execute(), equalTo("{\"field\": \"a \\\"value\\\"\"}"));
}
Also used : ScriptEngine(org.opensearch.script.ScriptEngine) TemplateScript(org.opensearch.script.TemplateScript)

Example 8 with TemplateScript

use of org.opensearch.script.TemplateScript in project OpenSearch by opensearch-project.

the class CustomMustacheFactoryTests method testDefaultEncoder.

public void testDefaultEncoder() {
    final ScriptEngine engine = new MustacheScriptEngine();
    final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, PLAIN_TEXT_MIME_TYPE);
    TemplateScript.Factory compiled = engine.compile(null, "{\"field\": \"{{value}}\"}", TemplateScript.CONTEXT, params);
    TemplateScript executable = compiled.newInstance(singletonMap("value", "a \"value\""));
    assertThat(executable.execute(), equalTo("{\"field\": \"a \"value\"\"}"));
}
Also used : ScriptEngine(org.opensearch.script.ScriptEngine) TemplateScript(org.opensearch.script.TemplateScript)

Example 9 with TemplateScript

use of org.opensearch.script.TemplateScript in project OpenSearch by opensearch-project.

the class TransportSearchTemplateAction method convert.

static SearchRequest convert(SearchTemplateRequest searchTemplateRequest, SearchTemplateResponse response, ScriptService scriptService, NamedXContentRegistry xContentRegistry) throws IOException {
    Script script = new Script(searchTemplateRequest.getScriptType(), searchTemplateRequest.getScriptType() == ScriptType.STORED ? null : TEMPLATE_LANG, searchTemplateRequest.getScript(), searchTemplateRequest.getScriptParams() == null ? Collections.emptyMap() : searchTemplateRequest.getScriptParams());
    TemplateScript compiledScript = scriptService.compile(script, TemplateScript.CONTEXT).newInstance(script.getParams());
    String source = compiledScript.execute();
    response.setSource(new BytesArray(source));
    SearchRequest searchRequest = searchTemplateRequest.getRequest();
    if (searchTemplateRequest.isSimulate()) {
        return null;
    }
    try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, source)) {
        SearchSourceBuilder builder = SearchSourceBuilder.searchSource();
        builder.parseXContent(parser, false);
        builder.explain(searchTemplateRequest.isExplain());
        builder.profile(searchTemplateRequest.isProfile());
        checkRestTotalHitsAsInt(searchRequest, builder);
        searchRequest.source(builder);
    }
    return searchRequest;
}
Also used : Script(org.opensearch.script.Script) TemplateScript(org.opensearch.script.TemplateScript) SearchRequest(org.opensearch.action.search.SearchRequest) BytesArray(org.opensearch.common.bytes.BytesArray) TemplateScript(org.opensearch.script.TemplateScript) XContentParser(org.opensearch.common.xcontent.XContentParser) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Aggregations

TemplateScript (org.opensearch.script.TemplateScript)9 XContentParser (org.opensearch.common.xcontent.XContentParser)4 Script (org.opensearch.script.Script)4 ScriptEngine (org.opensearch.script.ScriptEngine)3 SearchRequest (org.opensearch.action.search.SearchRequest)2 BytesArray (org.opensearch.common.bytes.BytesArray)2 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.emptyOrNullString (org.hamcrest.Matchers.emptyOrNullString)1 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)1