org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram. 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: DerivativePipelineAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private Long resolveBucketKeyAsLong(InternalHistogram.Bucket bucket) {
    Object key = bucket.getKey();
    if (key instanceof DateTime) {
        return ((DateTime) key).getMillis();
    } else if (key instanceof Number) {
        return ((Number) key).longValue();
    } else {
        throw new AggregationExecutionException("Bucket keys must be either a Number or a DateTime for aggregation " + name()
                + ". Found bucket with key " + key);
    }
}
 
Example #4
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void reverseToRootGroupByOnNestedFieldWithFilterTestWithReverseNestedOnHistogram() throws Exception {
    Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY  nested(message.info),filter('myFilter',message.info = 'a'),histogram('field'='myNum','reverse_nested'='','interval'='2' , 'alias' = 'someAlias' )", TEST_INDEX_NESTED_TYPE));
    InternalNested nested = result.get("message.info@NESTED");
    InternalFilter filter = nested.getAggregations().get("myFilter@FILTER");
    Terms infos = filter.getAggregations().get("message.info");
    Assert.assertEquals(1,infos.getBuckets().size());
    for(Terms.Bucket bucket : infos.getBuckets()) {
        InternalReverseNested reverseNested = bucket.getAggregations().get("someAlias@NESTED");
        InternalHistogram histogram = reverseNested.getAggregations().get("someAlias");
        Assert.assertEquals(3, histogram.getBuckets().size());

    }
}
 
Example #5
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void reverseAnotherNestedGroupByOnNestedFieldWithFilterTestWithReverseNestedOnHistogram() throws Exception {
    Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY  nested(message.info),filter('myFilter',message.info = 'a'),histogram('field'='comment.likes','reverse_nested'='~comment','interval'='2' , 'alias' = 'someAlias' )", TEST_INDEX_NESTED_TYPE));
    InternalNested nested = result.get("message.info@NESTED");
    InternalFilter filter = nested.getAggregations().get("myFilter@FILTER");
    Terms infos = filter.getAggregations().get("message.info");
    Assert.assertEquals(1,infos.getBuckets().size());
    for(Terms.Bucket bucket : infos.getBuckets()) {
        InternalReverseNested reverseNested = bucket.getAggregations().get("~comment@NESTED_REVERSED");
        InternalNested innerNested = reverseNested.getAggregations().get("~comment@NESTED");
        InternalHistogram histogram = innerNested.getAggregations().get("someAlias");
        Assert.assertEquals(2, histogram.getBuckets().size());

    }
}
 
Example #6
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);
}
 
Example #7
Source File: ElasticsearchGraphQueryIterable.java    From vertexium with Apache License 2.0 4 votes vote down vote up
private static AggregationResult reduceAggregationResults(ElasticsearchSearchQueryBase query, org.vertexium.query.Aggregation queryAgg, List<Aggregation> resultAggs) {
    if (resultAggs.size() == 0) {
        throw new VertexiumException("Cannot reduce zero sized aggregation list");
    }

    Object aggType = queryAgg;
    if (queryAgg == null) {
        Aggregation first = resultAggs.get(0);
        if (first.getName().endsWith(AGGREGATION_HAS_NOT_SUFFIX)) {
            if (resultAggs.size() > 1) {
                first = resultAggs.get(1);
            } else {
                throw new VertexiumException("Unhandled aggregation. Found HasNot filter with no associated aggregations and no query aggregation.");
            }
        }
        aggType = first;
    }

    if (aggType instanceof HistogramAggregation || aggType instanceof CalendarFieldAggregation ||
        aggType instanceof InternalHistogram || aggType instanceof InternalDateHistogram) {
        return reduceHistogramResults(query, resultAggs);
    }
    if (aggType instanceof RangeAggregation || aggType instanceof InternalRange) {
        return reduceRangeResults(query, resultAggs);
    }
    if (aggType instanceof PercentilesAggregation || aggType instanceof Percentiles) {
        return reducePercentilesResults(query, resultAggs);
    }
    if (aggType instanceof TermsAggregation || aggType instanceof InternalTerms) {
        return reduceTermsResults(query, resultAggs);
    }
    if (aggType instanceof GeohashAggregation || aggType instanceof InternalGeoHashGrid) {
        return reduceGeohashResults(query, resultAggs);
    }
    if (aggType instanceof StatisticsAggregation || aggType instanceof InternalExtendedStats) {
        return reduceStatisticsResults(resultAggs);
    }
    if (aggType instanceof CardinalityAggregation || aggType instanceof InternalCardinality) {
        return reduceCardinalityResults(query, resultAggs);
    }
    throw new VertexiumException("Unhandled aggregation type: " + aggType.getClass().getName());
}
 
Example #8
Source File: ElasticsearchGraphQueryIterable.java    From vertexium with Apache License 2.0 4 votes vote down vote up
private static AggregationResult reduceAggregationResults(ElasticsearchSearchQueryBase query, org.vertexium.query.Aggregation queryAgg, List<Aggregation> resultAggs) {
    if (resultAggs.size() == 0) {
        throw new VertexiumException("Cannot reduce zero sized aggregation list");
    }

    Object aggType = queryAgg;
    if (queryAgg == null) {
        Aggregation first = resultAggs.get(0);
        if (first.getName().endsWith(AGGREGATION_HAS_NOT_SUFFIX)) {
            if (resultAggs.size() > 1) {
                first = resultAggs.get(1);
            } else {
                throw new VertexiumException("Unhandled aggregation. Found HasNot filter with no associated aggregations and no query aggregation.");
            }
        }
        aggType = first;
    }

    if (aggType instanceof HistogramAggregation || aggType instanceof CalendarFieldAggregation ||
        aggType instanceof InternalHistogram || aggType instanceof InternalDateHistogram) {
        return reduceHistogramResults(query, resultAggs);
    }
    if (aggType instanceof RangeAggregation || aggType instanceof InternalRange) {
        return reduceRangeResults(query, resultAggs);
    }
    if (aggType instanceof PercentilesAggregation || aggType instanceof Percentiles) {
        return reducePercentilesResults(query, resultAggs);
    }
    if (aggType instanceof TermsAggregation || aggType instanceof InternalTerms) {
        return reduceTermsResults(query, resultAggs);
    }
    if (aggType instanceof GeohashAggregation || aggType instanceof InternalGeoHashGrid) {
        return reduceGeohashResults(query, resultAggs);
    }
    if (aggType instanceof StatisticsAggregation || aggType instanceof InternalExtendedStats) {
        return reduceStatisticsResults(resultAggs);
    }
    if (aggType instanceof CardinalityAggregation || aggType instanceof InternalCardinality) {
        return reduceCardinalityResults(query, resultAggs);
    }
    throw new VertexiumException("Unhandled aggregation type: " + aggType.getClass().getName());
}