Java Code Examples for org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram#Factory

The following examples show how to use org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram#Factory . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CumulativeSumPipelineAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    InternalHistogram histo = (InternalHistogram) aggregation;
    List<? extends InternalHistogram.Bucket> buckets = histo.getBuckets();
    InternalHistogram.Factory<? extends InternalHistogram.Bucket> factory = histo.getFactory();

    List newBuckets = new ArrayList<>();
    double sum = 0;
    for (InternalHistogram.Bucket bucket : buckets) {
        Double thisBucketValue = resolveBucketValue(histo, bucket, bucketsPaths()[0], GapPolicy.INSERT_ZEROS);
        sum += thisBucketValue;
        List<InternalAggregation> aggs = new ArrayList<>(eagerTransform(bucket.getAggregations().asList(),
                AGGREGATION_TRANFORM_FUNCTION));
        aggs.add(new InternalSimpleValue(name(), sum, formatter, new ArrayList<PipelineAggregator>(), metaData()));
        InternalHistogram.Bucket newBucket = factory.createBucket(bucket.getKey(), bucket.getDocCount(),
                new InternalAggregations(aggs), bucket.getKeyed(), bucket.getFormatter());
        newBuckets.add(newBucket);
    }
    return factory.create(newBuckets, histo);
}
 
Example 2
Source File: DerivativePipelineAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    InternalHistogram histo = (InternalHistogram) aggregation;
    List<? extends InternalHistogram.Bucket> buckets = histo.getBuckets();
    InternalHistogram.Factory<? extends InternalHistogram.Bucket> factory = histo.getFactory();

    List newBuckets = new ArrayList<>();
    Long lastBucketKey = null;
    Double lastBucketValue = null;
    for (InternalHistogram.Bucket bucket : buckets) {
        Long thisBucketKey = resolveBucketKeyAsLong(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 - lastBucketKey) / xAxisUnits;
            }
            List<InternalAggregation> aggs = new ArrayList<>(eagerTransform(bucket.getAggregations().asList(),
                    AGGREGATION_TRANFORM_FUNCTION));
            aggs.add(new InternalDerivative(name(), gradient, xDiff, formatter, new ArrayList<PipelineAggregator>(), metaData()));
            InternalHistogram.Bucket newBucket = factory.createBucket(bucket.getKey(), bucket.getDocCount(), new InternalAggregations(
                    aggs), bucket.getKeyed(), bucket.getFormatter());
            newBuckets.add(newBucket);
        } else {
            newBuckets.add(bucket);
        }
        lastBucketKey = thisBucketKey;
        lastBucketValue = thisBucketValue;
    }
    return factory.create(newBuckets, histo);
}
 
Example 3
Source File: SerialDiffPipelineAggregator.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    InternalHistogram histo = (InternalHistogram) aggregation;
    List<? extends InternalHistogram.Bucket> buckets = histo.getBuckets();
    InternalHistogram.Factory<? extends InternalHistogram.Bucket> factory = histo.getFactory();

    List newBuckets = new ArrayList<>();
    EvictingQueue<Double> lagWindow = EvictingQueue.create(lag);
    int counter = 0;

    for (InternalHistogram.Bucket bucket : buckets) {
        Double thisBucketValue = resolveBucketValue(histo, bucket, bucketsPaths()[0], gapPolicy);
        InternalHistogram.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 {
            lagValue = lagWindow.peek();  // Peek here, because we rely on add'ing to always move the window
        }

        // 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 = new ArrayList<>(eagerTransform(bucket.getAggregations().asList(), AGGREGATION_TRANFORM_FUNCTION));
            aggs.add(new InternalSimpleValue(name(), diff, formatter, new ArrayList<PipelineAggregator>(), metaData()));
            newBucket = factory.createBucket(bucket.getKey(), bucket.getDocCount(), new InternalAggregations(
                    aggs), bucket.getKeyed(), bucket.getFormatter());
        }


        newBuckets.add(newBucket);
        lagWindow.add(thisBucketValue);

    }
    return factory.create(newBuckets, histo);
}