Search in sources :

Example 6 with InternalMultiBucketAggregation

use of org.elasticsearch.search.aggregations.InternalMultiBucketAggregation 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

InternalMultiBucketAggregation (org.elasticsearch.search.aggregations.InternalMultiBucketAggregation)6 Map (java.util.Map)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Aggregation (org.elasticsearch.search.aggregations.Aggregation)3 InternalAggregation (org.elasticsearch.search.aggregations.InternalAggregation)3 InternalSingleBucketAggregation (org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Collectors (java.util.stream.Collectors)2 StreamSupport (java.util.stream.StreamSupport)2 StreamInput (org.elasticsearch.common.io.stream.StreamInput)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 CompiledScript (org.elasticsearch.script.CompiledScript)2 ExecutableScript (org.elasticsearch.script.ExecutableScript)2 ReduceContext (org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext)2 InternalAggregations (org.elasticsearch.search.aggregations.InternalAggregations)2