Search in sources :

Example 11 with ExecutableScript

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

the class UpdateHelper method executeScript.

private Map<String, Object> executeScript(Script script, Map<String, Object> ctx) {
    try {
        if (scriptService != null) {
            ExecutableScript executableScript = scriptService.executable(script, ScriptContext.Standard.UPDATE);
            executableScript.setNextVar("ctx", ctx);
            executableScript.run();
            // we need to unwrap the ctx...
            ctx = (Map<String, Object>) executableScript.unwrap(ctx);
        }
    } catch (Exception e) {
        throw new IllegalArgumentException("failed to execute script", e);
    }
    return ctx;
}
Also used : ExecutableScript(org.elasticsearch.script.ExecutableScript) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) DocumentSourceMissingException(org.elasticsearch.index.engine.DocumentSourceMissingException)

Example 12 with ExecutableScript

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

the class InternalScriptedMetric method doReduce.

@Override
public InternalAggregation doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    List<Object> aggregationObjects = new ArrayList<>();
    for (InternalAggregation aggregation : aggregations) {
        InternalScriptedMetric mapReduceAggregation = (InternalScriptedMetric) aggregation;
        aggregationObjects.addAll(mapReduceAggregation.aggregation);
    }
    InternalScriptedMetric firstAggregation = ((InternalScriptedMetric) aggregations.get(0));
    List<Object> aggregation;
    if (firstAggregation.reduceScript != null && reduceContext.isFinalReduce()) {
        Map<String, Object> vars = new HashMap<>();
        vars.put("_aggs", aggregationObjects);
        if (firstAggregation.reduceScript.getParams() != null) {
            vars.putAll(firstAggregation.reduceScript.getParams());
        }
        CompiledScript compiledScript = reduceContext.scriptService().compile(firstAggregation.reduceScript, ScriptContext.Standard.AGGS);
        ExecutableScript script = reduceContext.scriptService().executable(compiledScript, vars);
        aggregation = Collections.singletonList(script.run());
    } else if (reduceContext.isFinalReduce()) {
        aggregation = Collections.singletonList(aggregationObjects);
    } else {
        // if we are not an final reduce we have to maintain all the aggs from all the incoming one
        // until we hit the final reduce phase.
        aggregation = aggregationObjects;
    }
    return new InternalScriptedMetric(firstAggregation.getName(), aggregation, firstAggregation.reduceScript, pipelineAggregators(), getMetaData());
}
Also used : InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) CompiledScript(org.elasticsearch.script.CompiledScript) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutableScript(org.elasticsearch.script.ExecutableScript)

Example 13 with ExecutableScript

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

the class ScriptedMetricAggregationBuilder method doBuild.

@Override
protected ScriptedMetricAggregatorFactory doBuild(SearchContext context, AggregatorFactory<?> parent, Builder subfactoriesBuilder) throws IOException {
    QueryShardContext queryShardContext = context.getQueryShardContext();
    Function<Map<String, Object>, ExecutableScript> executableInitScript;
    if (initScript != null) {
        executableInitScript = queryShardContext.getLazyExecutableScript(initScript, ScriptContext.Standard.AGGS);
    } else {
        executableInitScript = (p) -> null;
    }
    Function<Map<String, Object>, SearchScript> searchMapScript = queryShardContext.getLazySearchScript(mapScript, ScriptContext.Standard.AGGS);
    Function<Map<String, Object>, ExecutableScript> executableCombineScript;
    if (combineScript != null) {
        executableCombineScript = queryShardContext.getLazyExecutableScript(combineScript, ScriptContext.Standard.AGGS);
    } else {
        executableCombineScript = (p) -> null;
    }
    return new ScriptedMetricAggregatorFactory(name, searchMapScript, executableInitScript, executableCombineScript, reduceScript, params, context, parent, subfactoriesBuilder, metaData);
}
Also used : SearchScript(org.elasticsearch.script.SearchScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Map(java.util.Map)

Example 14 with ExecutableScript

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

the class ScriptedMetricAggregatorFactory method createInternal.

@Override
public Aggregator createInternal(Aggregator parent, boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
    if (collectsFromSingleBucket == false) {
        return asMultiBucketAggregator(this, context, parent);
    }
    Map<String, Object> params = this.params;
    if (params != null) {
        params = deepCopyParams(params, context);
    } else {
        params = new HashMap<>();
        params.put("_agg", new HashMap<String, Object>());
    }
    final ExecutableScript initScript = this.initScript.apply(params);
    final SearchScript mapScript = this.mapScript.apply(params);
    final ExecutableScript combineScript = this.combineScript.apply(params);
    final Script reduceScript = deepCopyScript(this.reduceScript, context);
    if (initScript != null) {
        initScript.run();
    }
    return new ScriptedMetricAggregator(name, mapScript, combineScript, reduceScript, params, context, parent, pipelineAggregators, metaData);
}
Also used : Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchScript(org.elasticsearch.script.SearchScript) SearchScript(org.elasticsearch.script.SearchScript) ExecutableScript(org.elasticsearch.script.ExecutableScript)

Example 15 with ExecutableScript

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

the class BucketSelectorPipelineAggregator method reduce.

@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket> originalAgg = (InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket>) aggregation;
    List<? extends Bucket> buckets = originalAgg.getBuckets();
    CompiledScript compiledScript = reduceContext.scriptService().compile(script, ScriptContext.Standard.AGGS);
    List newBuckets = new ArrayList<>();
    for (Bucket bucket : buckets) {
        Map<String, Object> vars = new HashMap<>();
        if (script.getParams() != null) {
            vars.putAll(script.getParams());
        }
        for (Map.Entry<String, String> entry : bucketsPathsMap.entrySet()) {
            String varName = entry.getKey();
            String bucketsPath = entry.getValue();
            Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy);
            vars.put(varName, value);
        }
        ExecutableScript executableScript = reduceContext.scriptService().executable(compiledScript, vars);
        Object scriptReturnValue = executableScript.run();
        final boolean keepBucket;
        // TODO: WTF!!!!!
        if ("expression".equals(script.getLang())) {
            double scriptDoubleValue = (double) scriptReturnValue;
            keepBucket = scriptDoubleValue == 1.0;
        } else {
            keepBucket = (boolean) scriptReturnValue;
        }
        if (keepBucket) {
            newBuckets.add(bucket);
        }
    }
    return originalAgg.create(newBuckets);
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) ExecutableScript(org.elasticsearch.script.ExecutableScript) ArrayList(java.util.ArrayList) List(java.util.List) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) HashMap(java.util.HashMap) Map(java.util.Map)

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