use of org.elasticsearch.script.Script in project elasticsearch by elastic.
the class InternalTemplateService method compile.
@Override
public Template compile(String template) {
int mustacheStart = template.indexOf("{{");
int mustacheEnd = template.indexOf("}}");
if (mustacheStart != -1 && mustacheEnd != -1 && mustacheStart < mustacheEnd) {
Script script = new Script(ScriptType.INLINE, "mustache", template, Collections.emptyMap());
CompiledScript compiledScript = scriptService.compile(script, ScriptContext.Standard.INGEST);
return new Template() {
@Override
public String execute(Map<String, Object> model) {
ExecutableScript executableScript = scriptService.executable(compiledScript, model);
Object result = executableScript.run();
if (result instanceof BytesReference) {
return ((BytesReference) result).utf8ToString();
}
return String.valueOf(result);
}
@Override
public String getKey() {
return template;
}
};
} else {
return new StringTemplate(template);
}
}
use of org.elasticsearch.script.Script in project elasticsearch by elastic.
the class ValuesSourceAggregationBuilder method read.
/**
* Read from a stream.
*/
private void read(StreamInput in) throws IOException {
field = in.readOptionalString();
if (in.readBoolean()) {
script = new Script(in);
}
if (in.readBoolean()) {
valueType = ValueType.readFromStream(in);
}
format = in.readOptionalString();
missing = in.readGenericValue();
if (in.readBoolean()) {
timeZone = DateTimeZone.forID(in.readString());
}
}
use of org.elasticsearch.script.Script in project elasticsearch by elastic.
the class QueryShardContext method getLazySearchScript.
/**
* Returns a lazily created {@link SearchScript} that is compiled immediately but can be pulled later once all
* parameters are available.
*/
public final Function<Map<String, Object>, SearchScript> getLazySearchScript(Script script, ScriptContext context) {
failIfFrozen();
CompiledScript compile = scriptService.compile(script, context);
return (p) -> scriptService.search(lookup(), compile, p);
}
use of org.elasticsearch.script.Script in project elasticsearch by elastic.
the class ScriptScoreFunctionBuilder method fromXContent.
public static ScriptScoreFunctionBuilder fromXContent(QueryParseContext parseContext) throws IOException, ParsingException {
XContentParser parser = parseContext.parser();
Script script = null;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else {
if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
script = Script.parse(parser);
} else {
throw new ParsingException(parser.getTokenLocation(), NAME + " query does not support [" + currentFieldName + "]");
}
}
}
if (script == null) {
throw new ParsingException(parser.getTokenLocation(), NAME + " requires 'script' field");
}
return new ScriptScoreFunctionBuilder(script);
}
use of org.elasticsearch.script.Script in project elasticsearch by elastic.
the class ScriptQueryBuilder method fromXContent.
public static ScriptQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
XContentParser parser = parseContext.parser();
// also, when caching, since its isCacheable is false, will result in loading all bit set...
Script script = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
XContentParser.Token token;
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (parseContext.isDeprecatedSetting(currentFieldName)) {
// skip
} else if (token == XContentParser.Token.START_OBJECT) {
if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
script = Script.parse(parser);
} else {
throw new ParsingException(parser.getTokenLocation(), "[script] query does not support [" + currentFieldName + "]");
}
} else if (token.isValue()) {
if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
queryName = parser.text();
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
boost = parser.floatValue();
} else if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
script = Script.parse(parser);
} else {
throw new ParsingException(parser.getTokenLocation(), "[script] query does not support [" + currentFieldName + "]");
}
}
}
if (script == null) {
throw new ParsingException(parser.getTokenLocation(), "script must be provided with a [script] filter");
}
return new ScriptQueryBuilder(script).boost(boost).queryName(queryName);
}
Aggregations