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