Search in sources :

Example 6 with InternalAggregation

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

the class InternalExtendedStats method doReduce.

@Override
public InternalExtendedStats doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    double sumOfSqrs = 0;
    for (InternalAggregation aggregation : aggregations) {
        InternalExtendedStats stats = (InternalExtendedStats) aggregation;
        if (stats.sigma != sigma) {
            throw new IllegalStateException("Cannot reduce other stats aggregations that have a different sigma");
        }
        sumOfSqrs += stats.getSumOfSquares();
    }
    final InternalStats stats = super.doReduce(aggregations, reduceContext);
    return new InternalExtendedStats(name, stats.getCount(), stats.getSum(), stats.getMin(), stats.getMax(), sumOfSqrs, sigma, format, pipelineAggregators(), getMetaData());
}
Also used : InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) InternalStats(org.elasticsearch.search.aggregations.metrics.stats.InternalStats)

Example 7 with InternalAggregation

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

the class CumulativeSumPipelineAggregator 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<>();
    double sum = 0;
    for (Bucket bucket : buckets) {
        Double thisBucketValue = resolveBucketValue(histo, bucket, bucketsPaths()[0], GapPolicy.INSERT_ZEROS);
        sum += thisBucketValue;
        List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
            return (InternalAggregation) p;
        }).collect(Collectors.toList());
        aggs.add(new InternalSimpleValue(name(), sum, formatter, new ArrayList<PipelineAggregator>(), metaData()));
        Bucket newBucket = factory.createBucket(factory.getKey(bucket), bucket.getDocCount(), new InternalAggregations(aggs));
        newBuckets.add(newBucket);
    }
    return factory.createAggregation(newBuckets);
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) PipelineAggregator(org.elasticsearch.search.aggregations.pipeline.PipelineAggregator) IOException(java.io.IOException) DocValueFormat(org.elasticsearch.search.DocValueFormat) Collectors(java.util.stream.Collectors) 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) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) 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)

Example 8 with InternalAggregation

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

the class DerivativePipelineAggregator 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<>();
    Number lastBucketKey = null;
    Double lastBucketValue = null;
    for (Bucket bucket : buckets) {
        Number thisBucketKey = factory.getKey(bucket);
        Double thisBucketValue = resolveBucketValue(histo, bucket, bucketsPaths()[0], gapPolicy);
        if (lastBucketValue != null && thisBucketValue != null) {
            double gradient = thisBucketValue - lastBucketValue;
            double xDiff = -1;
            if (xAxisUnits != null) {
                xDiff = (thisBucketKey.doubleValue() - lastBucketKey.doubleValue()) / xAxisUnits;
            }
            final List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
                return (InternalAggregation) p;
            }).collect(Collectors.toList());
            aggs.add(new InternalDerivative(name(), gradient, xDiff, formatter, new ArrayList<PipelineAggregator>(), metaData()));
            Bucket newBucket = factory.createBucket(factory.getKey(bucket), bucket.getDocCount(), new InternalAggregations(aggs));
            newBuckets.add(newBucket);
        } else {
            newBuckets.add(bucket);
        }
        lastBucketKey = thisBucketKey;
        lastBucketValue = thisBucketValue;
    }
    return factory.createAggregation(newBuckets);
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) PipelineAggregator(org.elasticsearch.search.aggregations.pipeline.PipelineAggregator) IOException(java.io.IOException) DocValueFormat(org.elasticsearch.search.DocValueFormat) Collectors(java.util.stream.Collectors) 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) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) 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) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)

Example 9 with InternalAggregation

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

the class MovAvgPipelineAggregator 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> values = new EvictingQueue<>(this.window);
    Number lastValidKey = 0;
    int lastValidPosition = 0;
    int counter = 0;
    // Do we need to fit the model parameters to the data?
    if (minimize) {
        assert (model.canBeMinimized());
        model = minimize(buckets, histo, model);
    }
    for (Bucket bucket : buckets) {
        Double thisBucketValue = resolveBucketValue(histo, bucket, bucketsPaths()[0], gapPolicy);
        // Default is to reuse existing bucket.  Simplifies the rest of the logic,
        // since we only change newBucket if we can add to it
        Bucket newBucket = bucket;
        if (!(thisBucketValue == null || thisBucketValue.equals(Double.NaN))) {
            // Some models (e.g. HoltWinters) have certain preconditions that must be met
            if (model.hasValue(values.size())) {
                double movavg = model.next(values);
                List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
                    return (InternalAggregation) p;
                }).collect(Collectors.toList());
                aggs.add(new InternalSimpleValue(name(), movavg, formatter, new ArrayList<PipelineAggregator>(), metaData()));
                newBucket = factory.createBucket(factory.getKey(bucket), bucket.getDocCount(), new InternalAggregations(aggs));
            }
            if (predict > 0) {
                lastValidKey = factory.getKey(bucket);
                lastValidPosition = counter;
            }
            values.offer(thisBucketValue);
        }
        counter += 1;
        newBuckets.add(newBucket);
    }
    if (buckets.size() > 0 && predict > 0) {
        double[] predictions = model.predict(values, predict);
        for (int i = 0; i < predictions.length; i++) {
            List<InternalAggregation> aggs;
            Number newKey = factory.nextKey(lastValidKey);
            if (lastValidPosition + i + 1 < newBuckets.size()) {
                Bucket bucket = newBuckets.get(lastValidPosition + i + 1);
                // Get the existing aggs in the bucket so we don't clobber data
                aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
                    return (InternalAggregation) p;
                }).collect(Collectors.toList());
                aggs.add(new InternalSimpleValue(name(), predictions[i], formatter, new ArrayList<PipelineAggregator>(), metaData()));
                Bucket newBucket = factory.createBucket(newKey, 0, new InternalAggregations(aggs));
                // Overwrite the existing bucket with the new version
                newBuckets.set(lastValidPosition + i + 1, newBucket);
            } else {
                // Not seen before, create fresh
                aggs = new ArrayList<>();
                aggs.add(new InternalSimpleValue(name(), predictions[i], formatter, new ArrayList<PipelineAggregator>(), metaData()));
                Bucket newBucket = factory.createBucket(newKey, 0, new InternalAggregations(aggs));
                // Since this is a new bucket, simply append it
                newBuckets.add(newBucket);
            }
            lastValidKey = newKey;
        }
    }
    return factory.createAggregation(newBuckets);
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) PipelineAggregator(org.elasticsearch.search.aggregations.pipeline.PipelineAggregator) ListIterator(java.util.ListIterator) IOException(java.io.IOException) DocValueFormat(org.elasticsearch.search.DocValueFormat) MovAvgModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel) 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) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) 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 10 with InternalAggregation

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

the class InternalStats method doReduce.

@Override
public InternalStats doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    long count = 0;
    double min = Double.POSITIVE_INFINITY;
    double max = Double.NEGATIVE_INFINITY;
    double sum = 0;
    for (InternalAggregation aggregation : aggregations) {
        InternalStats stats = (InternalStats) aggregation;
        count += stats.getCount();
        min = Math.min(min, stats.getMin());
        max = Math.max(max, stats.getMax());
        sum += stats.getSum();
    }
    return new InternalStats(name, count, sum, min, max, 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