Search in sources :

Example 1 with InternalSingleBucketAggregation

use of org.opensearch.search.aggregations.bucket.InternalSingleBucketAggregation in project anomaly-detection by opensearch-project.

the class AbstractRetriever method parseAggregation.

protected double parseAggregation(Aggregation aggregationToParse) {
    Double result = null;
    if (aggregationToParse instanceof InternalSingleBucketAggregation) {
        InternalAggregations bucket = ((InternalSingleBucketAggregation) aggregationToParse).getAggregations();
        if (bucket != null) {
            List<Aggregation> aggrs = bucket.asList();
            if (aggrs.size() == 1) {
                // we only accept a single value as feature
                aggregationToParse = aggrs.get(0);
            }
        }
    }
    final Aggregation aggregation = aggregationToParse;
    if (aggregation instanceof SingleValue) {
        result = ((SingleValue) aggregation).value();
    } else if (aggregation instanceof InternalTDigestPercentiles) {
        Iterator<Percentile> percentile = ((InternalTDigestPercentiles) aggregation).iterator();
        if (percentile.hasNext()) {
            result = percentile.next().getValue();
        }
    }
    return Optional.ofNullable(result).orElseThrow(() -> new EndRunException("Failed to parse aggregation " + aggregation, true).countedInStats(false));
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) InternalSingleBucketAggregation(org.opensearch.search.aggregations.bucket.InternalSingleBucketAggregation) MultiBucketsAggregation(org.opensearch.search.aggregations.bucket.MultiBucketsAggregation) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) SingleValue(org.opensearch.search.aggregations.metrics.NumericMetricsAggregation.SingleValue) EndRunException(org.opensearch.ad.common.exception.EndRunException) InternalTDigestPercentiles(org.opensearch.search.aggregations.metrics.InternalTDigestPercentiles) Iterator(java.util.Iterator) InternalSingleBucketAggregation(org.opensearch.search.aggregations.bucket.InternalSingleBucketAggregation)

Example 2 with InternalSingleBucketAggregation

use of org.opensearch.search.aggregations.bucket.InternalSingleBucketAggregation in project OpenSearch by opensearch-project.

the class InternalSingleBucketAggregationTestCase method assertReduced.

@Override
protected final void assertReduced(T reduced, List<T> inputs) {
    assertEquals(inputs.stream().mapToLong(InternalSingleBucketAggregation::getDocCount).sum(), reduced.getDocCount());
    if (hasInternalMax) {
        double expected = inputs.stream().mapToDouble(i -> {
            InternalMax max = i.getAggregations().get("max");
            return max.getValue();
        }).max().getAsDouble();
        InternalMax reducedMax = reduced.getAggregations().get("max");
        assertEquals(expected, reducedMax.getValue(), 0);
    }
    if (hasInternalMin) {
        double expected = inputs.stream().mapToDouble(i -> {
            InternalMin min = i.getAggregations().get("min");
            return min.getValue();
        }).min().getAsDouble();
        InternalMin reducedMin = reduced.getAggregations().get("min");
        assertEquals(expected, reducedMin.getValue(), 0);
    }
    extraAssertReduced(reduced, inputs);
}
Also used : InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) InternalMin(org.opensearch.search.aggregations.metrics.InternalMin) InternalSingleBucketAggregation(org.opensearch.search.aggregations.bucket.InternalSingleBucketAggregation)

Aggregations

InternalSingleBucketAggregation (org.opensearch.search.aggregations.bucket.InternalSingleBucketAggregation)2 Iterator (java.util.Iterator)1 EndRunException (org.opensearch.ad.common.exception.EndRunException)1 Aggregation (org.opensearch.search.aggregations.Aggregation)1 InternalAggregations (org.opensearch.search.aggregations.InternalAggregations)1 MultiBucketsAggregation (org.opensearch.search.aggregations.bucket.MultiBucketsAggregation)1 InternalMax (org.opensearch.search.aggregations.metrics.InternalMax)1 InternalMin (org.opensearch.search.aggregations.metrics.InternalMin)1 InternalTDigestPercentiles (org.opensearch.search.aggregations.metrics.InternalTDigestPercentiles)1 SingleValue (org.opensearch.search.aggregations.metrics.NumericMetricsAggregation.SingleValue)1