Search in sources :

Example 1 with Script

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);
    }
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) BytesReference(org.elasticsearch.common.bytes.BytesReference) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) Map(java.util.Map)

Example 2 with Script

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());
    }
}
Also used : Script(org.elasticsearch.script.Script)

Example 3 with Script

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);
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) Query(org.apache.lucene.search.Query) BitsetFilterCache(org.elasticsearch.index.cache.bitset.BitsetFilterCache) Arrays(java.util.Arrays) LongSupplier(java.util.function.LongSupplier) ParsingException(org.elasticsearch.common.ParsingException) IndexFieldDataService(org.elasticsearch.index.fielddata.IndexFieldDataService) IndexAnalyzers(org.elasticsearch.index.analysis.IndexAnalyzers) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) MapperQueryParser(org.apache.lucene.queryparser.classic.MapperQueryParser) Function(java.util.function.Function) ScriptContext(org.elasticsearch.script.ScriptContext) Strings(org.elasticsearch.common.Strings) NestedScope(org.elasticsearch.index.query.support.NestedScope) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Similarity(org.apache.lucene.search.similarities.Similarity) Map(java.util.Map) IndexSettings(org.elasticsearch.index.IndexSettings) QueryParserSettings(org.apache.lucene.queryparser.classic.QueryParserSettings) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) SearchScript(org.elasticsearch.script.SearchScript) SearchLookup(org.elasticsearch.search.lookup.SearchLookup) ObjectMapper(org.elasticsearch.index.mapper.ObjectMapper) Script(org.elasticsearch.script.Script) SetOnce(org.apache.lucene.util.SetOnce) Mapper(org.elasticsearch.index.mapper.Mapper) Analyzer(org.apache.lucene.analysis.Analyzer) Client(org.elasticsearch.client.Client) Collection(java.util.Collection) IOException(java.io.IOException) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper) BytesReference(org.elasticsearch.common.bytes.BytesReference) SimilarityService(org.elasticsearch.index.similarity.SimilarityService) CheckedFunction(org.elasticsearch.common.CheckedFunction) MapperService(org.elasticsearch.index.mapper.MapperService) Version(org.elasticsearch.Version) CompiledScript(org.elasticsearch.script.CompiledScript) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) Queries(org.elasticsearch.common.lucene.search.Queries) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) ExecutableScript(org.elasticsearch.script.ExecutableScript) ContentPath(org.elasticsearch.index.mapper.ContentPath) ScriptService(org.elasticsearch.script.ScriptService) IndexReader(org.apache.lucene.index.IndexReader)

Example 4 with Script

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);
}
Also used : Script(org.elasticsearch.script.Script) SearchScript(org.elasticsearch.script.SearchScript) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 5 with 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);
}
Also used : Script(org.elasticsearch.script.Script) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

Script (org.elasticsearch.script.Script)312 SearchResponse (org.elasticsearch.action.search.SearchResponse)225 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)206 HashMap (java.util.HashMap)91 ExecutableScript (org.elasticsearch.script.ExecutableScript)52 CompiledScript (org.elasticsearch.script.CompiledScript)46 SearchScript (org.elasticsearch.script.SearchScript)41 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)32 ArrayList (java.util.ArrayList)31 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)26 LeafSearchScript (org.elasticsearch.script.LeafSearchScript)25 Matchers.containsString (org.hamcrest.Matchers.containsString)23 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)22 List (java.util.List)20 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)18 PercentileRanks (org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks)18 Percentiles (org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles)18 Map (java.util.Map)17 XContentParser (org.elasticsearch.common.xcontent.XContentParser)16 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)16