Search in sources :

Example 16 with InternalAggregation

use of org.elasticsearch.search.aggregations.InternalAggregation in project elasticsearch by elastic.

the class SerialDiffPipelineAggregator method reduce.

@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    MultiBucketsAggregation histo = (MultiBucketsAggregation) aggregation;
    List<? extends Bucket> buckets = histo.getBuckets();
    HistogramFactory factory = (HistogramFactory) histo;
    List<Bucket> newBuckets = new ArrayList<>();
    EvictingQueue<Double> lagWindow = new EvictingQueue<>(lag);
    int counter = 0;
    for (Bucket bucket : buckets) {
        Double thisBucketValue = resolveBucketValue(histo, bucket, bucketsPaths()[0], gapPolicy);
        Bucket newBucket = bucket;
        counter += 1;
        // Still under the initial lag period, add nothing and move on
        Double lagValue;
        if (counter <= lag) {
            lagValue = Double.NaN;
        } else {
            // Peek here, because we rely on add'ing to always move the window
            lagValue = lagWindow.peek();
        }
        // Normalize null's to NaN
        if (thisBucketValue == null) {
            thisBucketValue = Double.NaN;
        }
        // Both have values, calculate diff and replace the "empty" bucket
        if (!Double.isNaN(thisBucketValue) && !Double.isNaN(lagValue)) {
            double diff = thisBucketValue - lagValue;
            List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
                return (InternalAggregation) p;
            }).collect(Collectors.toList());
            aggs.add(new InternalSimpleValue(name(), diff, formatter, new ArrayList<PipelineAggregator>(), metaData()));
            newBucket = factory.createBucket(factory.getKey(bucket), bucket.getDocCount(), new InternalAggregations(aggs));
        }
        newBuckets.add(newBucket);
        lagWindow.add(thisBucketValue);
    }
    return factory.createAggregation(newBuckets);
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) PipelineAggregator(org.elasticsearch.search.aggregations.pipeline.PipelineAggregator) Nullable(org.elasticsearch.common.Nullable) IOException(java.io.IOException) DocValueFormat(org.elasticsearch.search.DocValueFormat) Collectors(java.util.stream.Collectors) EvictingQueue(org.elasticsearch.common.collect.EvictingQueue) ArrayList(java.util.ArrayList) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) ReduceContext(org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext) List(java.util.List) BucketHelpers.resolveBucketValue(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.resolveBucketValue) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) InternalSimpleValue(org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Map(java.util.Map) GapPolicy(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy) StreamSupport(java.util.stream.StreamSupport) HistogramFactory(org.elasticsearch.search.aggregations.bucket.histogram.HistogramFactory) ArrayList(java.util.ArrayList) HistogramFactory(org.elasticsearch.search.aggregations.bucket.histogram.HistogramFactory) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) InternalSimpleValue(org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) EvictingQueue(org.elasticsearch.common.collect.EvictingQueue)

Example 17 with InternalAggregation

use of org.elasticsearch.search.aggregations.InternalAggregation 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 18 with InternalAggregation

use of org.elasticsearch.search.aggregations.InternalAggregation in project elasticsearch by elastic.

the class SiblingPipelineAggregator method reduce.

@SuppressWarnings("unchecked")
@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    if (aggregation instanceof InternalMultiBucketAggregation) {
        @SuppressWarnings("rawtypes") InternalMultiBucketAggregation multiBucketsAgg = (InternalMultiBucketAggregation) aggregation;
        List<? extends Bucket> buckets = multiBucketsAgg.getBuckets();
        List<Bucket> newBuckets = new ArrayList<>();
        for (int i = 0; i < buckets.size(); i++) {
            InternalMultiBucketAggregation.InternalBucket bucket = (InternalMultiBucketAggregation.InternalBucket) buckets.get(i);
            InternalAggregation aggToAdd = doReduce(bucket.getAggregations(), reduceContext);
            List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
                return (InternalAggregation) p;
            }).collect(Collectors.toList());
            aggs.add(aggToAdd);
            InternalMultiBucketAggregation.InternalBucket newBucket = multiBucketsAgg.createBucket(new InternalAggregations(aggs), bucket);
            newBuckets.add(newBucket);
        }
        return multiBucketsAgg.create(newBuckets);
    } else if (aggregation instanceof InternalSingleBucketAggregation) {
        InternalSingleBucketAggregation singleBucketAgg = (InternalSingleBucketAggregation) aggregation;
        InternalAggregation aggToAdd = doReduce(singleBucketAgg.getAggregations(), reduceContext);
        List<InternalAggregation> aggs = StreamSupport.stream(singleBucketAgg.getAggregations().spliterator(), false).map((p) -> {
            return (InternalAggregation) p;
        }).collect(Collectors.toList());
        aggs.add(aggToAdd);
        return singleBucketAgg.create(new InternalAggregations(aggs));
    } else {
        throw new IllegalStateException("Aggregation [" + aggregation.getName() + "] must be a bucket aggregation [" + aggregation.getWriteableName() + "]");
    }
}
Also used : InternalSingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation) Aggregations(org.elasticsearch.search.aggregations.Aggregations) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) ReduceContext(org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) List(java.util.List) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) ArrayList(java.util.ArrayList) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) InternalSingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation) ArrayList(java.util.ArrayList) List(java.util.List) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation)

Example 19 with InternalAggregation

use of org.elasticsearch.search.aggregations.InternalAggregation in project elasticsearch by elastic.

the class BucketMetricsPipelineAggregator method doReduce.

@Override
public final InternalAggregation doReduce(Aggregations aggregations, ReduceContext context) {
    preCollection();
    List<String> bucketsPath = AggregationPath.parse(bucketsPaths()[0]).getPathElementsAsStringList();
    for (Aggregation aggregation : aggregations) {
        if (aggregation.getName().equals(bucketsPath.get(0))) {
            bucketsPath = bucketsPath.subList(1, bucketsPath.size());
            InternalMultiBucketAggregation<?, ?> multiBucketsAgg = (InternalMultiBucketAggregation<?, ?>) aggregation;
            List<? extends Bucket> buckets = multiBucketsAgg.getBuckets();
            for (int i = 0; i < buckets.size(); i++) {
                Bucket bucket = buckets.get(i);
                Double bucketValue = BucketHelpers.resolveBucketValue(multiBucketsAgg, bucket, bucketsPath, gapPolicy);
                if (bucketValue != null && !Double.isNaN(bucketValue)) {
                    collectBucketValue(bucket.getKeyAsString(), bucketValue);
                }
            }
        }
    }
    return buildAggregation(Collections.emptyList(), metaData());
}
Also used : InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation)

Example 20 with InternalAggregation

use of org.elasticsearch.search.aggregations.InternalAggregation in project elasticsearch by elastic.

the class InternalAvg method doReduce.

@Override
public InternalAvg doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    long count = 0;
    double sum = 0;
    for (InternalAggregation aggregation : aggregations) {
        count += ((InternalAvg) aggregation).count;
        sum += ((InternalAvg) aggregation).sum;
    }
    return new InternalAvg(getName(), sum, count, format, pipelineAggregators(), getMetaData());
}
Also used : InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation)

Aggregations

InternalAggregation (org.elasticsearch.search.aggregations.InternalAggregation)33 ArrayList (java.util.ArrayList)23 List (java.util.List)16 Map (java.util.Map)10 InternalAggregations (org.elasticsearch.search.aggregations.InternalAggregations)10 IOException (java.io.IOException)8 Collectors (java.util.stream.Collectors)8 PipelineAggregator (org.elasticsearch.search.aggregations.pipeline.PipelineAggregator)8 StreamSupport (java.util.stream.StreamSupport)7 StreamInput (org.elasticsearch.common.io.stream.StreamInput)7 ReduceContext (org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext)7 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)7 InternalSimpleValue (org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue)6 HashMap (java.util.HashMap)5 DocValueFormat (org.elasticsearch.search.DocValueFormat)5 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)4 Settings (org.elasticsearch.common.settings.Settings)4 SignificanceHeuristic (org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristic)4 GapPolicy (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)4 BucketHelpers.resolveBucketValue (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.resolveBucketValue)4