use of org.opensearch.search.aggregations.metrics.InternalStats in project OpenSearch by opensearch-project.
the class VariableWidthHistogramAggregatorTests method testComplexSubAggregations.
public void testComplexSubAggregations() throws IOException {
final List<Number> dataset = Arrays.asList(5, 4, 3, 2, 1, 0, 6, 7, 8, 9, 10, 11);
testSearchCase(DEFAULT_QUERY, dataset, false, aggregation -> aggregation.field(NUMERIC_FIELD).setNumBuckets(3).setInitialBuffer(12).setShardSize(4).subAggregation(new StatsAggregationBuilder("stats").field(NUMERIC_FIELD)), histogram -> {
double deltaError = 1d / 10000d;
// Expected clusters: [ (0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11) ]
InternalStats stats = histogram.getBuckets().get(0).getAggregations().get("stats");
assertEquals(0d, stats.getMin(), deltaError);
assertEquals(3L, stats.getMax(), deltaError);
assertEquals(4L, stats.getCount());
assertTrue(AggregationInspectionHelper.hasValue(stats));
stats = histogram.getBuckets().get(1).getAggregations().get("stats");
assertEquals(4d, stats.getMin(), deltaError);
assertEquals(7d, stats.getMax(), deltaError);
assertEquals(4L, stats.getCount());
assertTrue(AggregationInspectionHelper.hasValue(stats));
stats = histogram.getBuckets().get(2).getAggregations().get("stats");
assertEquals(8d, stats.getMin(), deltaError);
assertEquals(11d, stats.getMax(), deltaError);
assertEquals(4L, stats.getCount());
assertTrue(AggregationInspectionHelper.hasValue(stats));
});
}
use of org.opensearch.search.aggregations.metrics.InternalStats in project OpenSearch by opensearch-project.
the class SearchModule method registerAggregations.
private ValuesSourceRegistry registerAggregations(List<SearchPlugin> plugins) {
ValuesSourceRegistry.Builder builder = new ValuesSourceRegistry.Builder();
registerAggregation(new AggregationSpec(AvgAggregationBuilder.NAME, AvgAggregationBuilder::new, AvgAggregationBuilder.PARSER).addResultReader(InternalAvg::new).setAggregatorRegistrar(AvgAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(WeightedAvgAggregationBuilder.NAME, WeightedAvgAggregationBuilder::new, WeightedAvgAggregationBuilder.PARSER).addResultReader(InternalWeightedAvg::new).setAggregatorRegistrar(WeightedAvgAggregationBuilder::registerUsage), builder);
registerAggregation(new AggregationSpec(SumAggregationBuilder.NAME, SumAggregationBuilder::new, SumAggregationBuilder.PARSER).addResultReader(InternalSum::new).setAggregatorRegistrar(SumAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(MinAggregationBuilder.NAME, MinAggregationBuilder::new, MinAggregationBuilder.PARSER).addResultReader(InternalMin::new).setAggregatorRegistrar(MinAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(MaxAggregationBuilder.NAME, MaxAggregationBuilder::new, MaxAggregationBuilder.PARSER).addResultReader(InternalMax::new).setAggregatorRegistrar(MaxAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(StatsAggregationBuilder.NAME, StatsAggregationBuilder::new, StatsAggregationBuilder.PARSER).addResultReader(InternalStats::new).setAggregatorRegistrar(StatsAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(ExtendedStatsAggregationBuilder.NAME, ExtendedStatsAggregationBuilder::new, ExtendedStatsAggregationBuilder.PARSER).addResultReader(InternalExtendedStats::new).setAggregatorRegistrar(ExtendedStatsAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(ValueCountAggregationBuilder.NAME, ValueCountAggregationBuilder::new, ValueCountAggregationBuilder.PARSER).addResultReader(InternalValueCount::new).setAggregatorRegistrar(ValueCountAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(PercentilesAggregationBuilder.NAME, PercentilesAggregationBuilder::new, PercentilesAggregationBuilder::parse).addResultReader(InternalTDigestPercentiles.NAME, InternalTDigestPercentiles::new).addResultReader(InternalHDRPercentiles.NAME, InternalHDRPercentiles::new).setAggregatorRegistrar(PercentilesAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(PercentileRanksAggregationBuilder.NAME, PercentileRanksAggregationBuilder::new, PercentileRanksAggregationBuilder::parse).addResultReader(InternalTDigestPercentileRanks.NAME, InternalTDigestPercentileRanks::new).addResultReader(InternalHDRPercentileRanks.NAME, InternalHDRPercentileRanks::new).setAggregatorRegistrar(PercentileRanksAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(MedianAbsoluteDeviationAggregationBuilder.NAME, MedianAbsoluteDeviationAggregationBuilder::new, MedianAbsoluteDeviationAggregationBuilder.PARSER).addResultReader(InternalMedianAbsoluteDeviation::new).setAggregatorRegistrar(MedianAbsoluteDeviationAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(CardinalityAggregationBuilder.NAME, CardinalityAggregationBuilder::new, CardinalityAggregationBuilder.PARSER).addResultReader(InternalCardinality::new).setAggregatorRegistrar(CardinalityAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(GlobalAggregationBuilder.NAME, GlobalAggregationBuilder::new, GlobalAggregationBuilder::parse).addResultReader(InternalGlobal::new), builder);
registerAggregation(new AggregationSpec(MissingAggregationBuilder.NAME, MissingAggregationBuilder::new, MissingAggregationBuilder.PARSER).addResultReader(InternalMissing::new).setAggregatorRegistrar(MissingAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(FilterAggregationBuilder.NAME, FilterAggregationBuilder::new, FilterAggregationBuilder::parse).addResultReader(InternalFilter::new), builder);
registerAggregation(new AggregationSpec(FiltersAggregationBuilder.NAME, FiltersAggregationBuilder::new, FiltersAggregationBuilder::parse).addResultReader(InternalFilters::new), builder);
registerAggregation(new AggregationSpec(AdjacencyMatrixAggregationBuilder.NAME, AdjacencyMatrixAggregationBuilder::new, AdjacencyMatrixAggregationBuilder::parse).addResultReader(InternalAdjacencyMatrix::new), builder);
registerAggregation(new AggregationSpec(SamplerAggregationBuilder.NAME, SamplerAggregationBuilder::new, SamplerAggregationBuilder::parse).addResultReader(InternalSampler.NAME, InternalSampler::new).addResultReader(UnmappedSampler.NAME, UnmappedSampler::new), builder);
registerAggregation(new AggregationSpec(DiversifiedAggregationBuilder.NAME, DiversifiedAggregationBuilder::new, DiversifiedAggregationBuilder.PARSER).setAggregatorRegistrar(DiversifiedAggregationBuilder::registerAggregators), /* Reuses result readers from SamplerAggregator*/
builder);
registerAggregation(new AggregationSpec(TermsAggregationBuilder.NAME, TermsAggregationBuilder::new, TermsAggregationBuilder.PARSER).addResultReader(StringTerms.NAME, StringTerms::new).addResultReader(UnmappedTerms.NAME, UnmappedTerms::new).addResultReader(LongTerms.NAME, LongTerms::new).addResultReader(DoubleTerms.NAME, DoubleTerms::new).setAggregatorRegistrar(TermsAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(RareTermsAggregationBuilder.NAME, RareTermsAggregationBuilder::new, RareTermsAggregationBuilder.PARSER).addResultReader(StringRareTerms.NAME, StringRareTerms::new).addResultReader(UnmappedRareTerms.NAME, UnmappedRareTerms::new).addResultReader(LongRareTerms.NAME, LongRareTerms::new).setAggregatorRegistrar(RareTermsAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(SignificantTermsAggregationBuilder.NAME, SignificantTermsAggregationBuilder::new, SignificantTermsAggregationBuilder::parse).addResultReader(SignificantStringTerms.NAME, SignificantStringTerms::new).addResultReader(SignificantLongTerms.NAME, SignificantLongTerms::new).addResultReader(UnmappedSignificantTerms.NAME, UnmappedSignificantTerms::new).setAggregatorRegistrar(SignificantTermsAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(SignificantTextAggregationBuilder.NAME, SignificantTextAggregationBuilder::new, SignificantTextAggregationBuilder::parse), builder);
registerAggregation(new AggregationSpec(RangeAggregationBuilder.NAME, RangeAggregationBuilder::new, RangeAggregationBuilder.PARSER).addResultReader(InternalRange::new).setAggregatorRegistrar(RangeAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(DateRangeAggregationBuilder.NAME, DateRangeAggregationBuilder::new, DateRangeAggregationBuilder.PARSER).addResultReader(InternalDateRange::new).setAggregatorRegistrar(DateRangeAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(IpRangeAggregationBuilder.NAME, IpRangeAggregationBuilder::new, IpRangeAggregationBuilder.PARSER).addResultReader(InternalBinaryRange::new).setAggregatorRegistrar(IpRangeAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(HistogramAggregationBuilder.NAME, HistogramAggregationBuilder::new, HistogramAggregationBuilder.PARSER).addResultReader(InternalHistogram::new).setAggregatorRegistrar(HistogramAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(DateHistogramAggregationBuilder.NAME, DateHistogramAggregationBuilder::new, DateHistogramAggregationBuilder.PARSER).addResultReader(InternalDateHistogram::new).setAggregatorRegistrar(DateHistogramAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(AutoDateHistogramAggregationBuilder.NAME, AutoDateHistogramAggregationBuilder::new, AutoDateHistogramAggregationBuilder.PARSER).addResultReader(InternalAutoDateHistogram::new).setAggregatorRegistrar(AutoDateHistogramAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(VariableWidthHistogramAggregationBuilder.NAME, VariableWidthHistogramAggregationBuilder::new, VariableWidthHistogramAggregationBuilder.PARSER).addResultReader(InternalVariableWidthHistogram::new).setAggregatorRegistrar(VariableWidthHistogramAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(GeoDistanceAggregationBuilder.NAME, GeoDistanceAggregationBuilder::new, GeoDistanceAggregationBuilder::parse).addResultReader(InternalGeoDistance::new).setAggregatorRegistrar(GeoDistanceAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(GeoHashGridAggregationBuilder.NAME, GeoHashGridAggregationBuilder::new, GeoHashGridAggregationBuilder.PARSER).addResultReader(InternalGeoHashGrid::new).setAggregatorRegistrar(GeoHashGridAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(GeoTileGridAggregationBuilder.NAME, GeoTileGridAggregationBuilder::new, GeoTileGridAggregationBuilder.PARSER).addResultReader(InternalGeoTileGrid::new).setAggregatorRegistrar(GeoTileGridAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(NestedAggregationBuilder.NAME, NestedAggregationBuilder::new, NestedAggregationBuilder::parse).addResultReader(InternalNested::new), builder);
registerAggregation(new AggregationSpec(ReverseNestedAggregationBuilder.NAME, ReverseNestedAggregationBuilder::new, ReverseNestedAggregationBuilder::parse).addResultReader(InternalReverseNested::new), builder);
registerAggregation(new AggregationSpec(TopHitsAggregationBuilder.NAME, TopHitsAggregationBuilder::new, TopHitsAggregationBuilder::parse).addResultReader(InternalTopHits::new), builder);
registerAggregation(new AggregationSpec(GeoBoundsAggregationBuilder.NAME, GeoBoundsAggregationBuilder::new, GeoBoundsAggregationBuilder.PARSER).addResultReader(InternalGeoBounds::new).setAggregatorRegistrar(GeoBoundsAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(GeoCentroidAggregationBuilder.NAME, GeoCentroidAggregationBuilder::new, GeoCentroidAggregationBuilder.PARSER).addResultReader(InternalGeoCentroid::new).setAggregatorRegistrar(GeoCentroidAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(ScriptedMetricAggregationBuilder.NAME, ScriptedMetricAggregationBuilder::new, ScriptedMetricAggregationBuilder.PARSER).addResultReader(InternalScriptedMetric::new), builder);
registerAggregation(new AggregationSpec(CompositeAggregationBuilder.NAME, CompositeAggregationBuilder::new, CompositeAggregationBuilder.PARSER).addResultReader(InternalComposite::new).setAggregatorRegistrar(CompositeAggregationBuilder::registerAggregators), builder);
registerFromPlugin(plugins, SearchPlugin::getAggregations, (agg) -> this.registerAggregation(agg, builder));
// after aggs have been registered, see if there are any new VSTypes that need to be linked to core fields
registerFromPlugin(plugins, SearchPlugin::getAggregationExtentions, (registrar) -> {
if (registrar != null) {
registrar.accept(builder);
}
});
return builder.build();
}
use of org.opensearch.search.aggregations.metrics.InternalStats in project OpenSearch by opensearch-project.
the class AutoDateHistogramAggregatorTests method testSubAggregations.
public void testSubAggregations() throws IOException {
testSearchCase(DEFAULT_QUERY, DATES_WITH_TIME, aggregation -> aggregation.setNumBuckets(8).field(DATE_FIELD).subAggregation(AggregationBuilders.stats("stats").field(DATE_FIELD)), histogram -> {
assertTrue(AggregationInspectionHelper.hasValue(histogram));
final List<? extends Histogram.Bucket> buckets = histogram.getBuckets();
assertEquals(8, buckets.size());
Histogram.Bucket bucket = buckets.get(0);
assertEquals("2010-01-01T00:00:00.000Z", bucket.getKeyAsString());
assertEquals(2, bucket.getDocCount());
InternalStats stats = bucket.getAggregations().get("stats");
assertEquals("2010-03-12T01:07:45.000Z", stats.getMinAsString());
assertEquals("2010-04-27T03:43:34.000Z", stats.getMaxAsString());
assertEquals(2L, stats.getCount());
assertTrue(AggregationInspectionHelper.hasValue(stats));
bucket = buckets.get(1);
assertEquals("2011-01-01T00:00:00.000Z", bucket.getKeyAsString());
assertEquals(0, bucket.getDocCount());
stats = bucket.getAggregations().get("stats");
assertTrue(Double.isInfinite(stats.getMin()));
assertTrue(Double.isInfinite(stats.getMax()));
assertEquals(0L, stats.getCount());
assertFalse(AggregationInspectionHelper.hasValue(stats));
bucket = buckets.get(2);
assertEquals("2012-01-01T00:00:00.000Z", bucket.getKeyAsString());
assertEquals(1, bucket.getDocCount());
stats = bucket.getAggregations().get("stats");
assertEquals("2012-05-18T04:11:00.000Z", stats.getMinAsString());
assertEquals("2012-05-18T04:11:00.000Z", stats.getMaxAsString());
assertEquals(1L, stats.getCount());
assertTrue(AggregationInspectionHelper.hasValue(stats));
bucket = buckets.get(3);
assertEquals("2013-01-01T00:00:00.000Z", bucket.getKeyAsString());
assertEquals(2, bucket.getDocCount());
stats = bucket.getAggregations().get("stats");
assertEquals("2013-05-29T05:11:31.000Z", stats.getMinAsString());
assertEquals("2013-10-31T08:24:05.000Z", stats.getMaxAsString());
assertEquals(2L, stats.getCount());
assertTrue(AggregationInspectionHelper.hasValue(stats));
bucket = buckets.get(4);
assertEquals("2014-01-01T00:00:00.000Z", bucket.getKeyAsString());
assertEquals(0, bucket.getDocCount());
stats = bucket.getAggregations().get("stats");
assertTrue(Double.isInfinite(stats.getMin()));
assertTrue(Double.isInfinite(stats.getMax()));
assertEquals(0L, stats.getCount());
assertFalse(AggregationInspectionHelper.hasValue(stats));
bucket = buckets.get(5);
assertEquals("2015-01-01T00:00:00.000Z", bucket.getKeyAsString());
assertEquals(3, bucket.getDocCount());
stats = bucket.getAggregations().get("stats");
assertEquals("2015-02-13T13:09:32.000Z", stats.getMinAsString());
assertEquals("2015-11-13T16:14:34.000Z", stats.getMaxAsString());
assertEquals(3L, stats.getCount());
assertTrue(AggregationInspectionHelper.hasValue(stats));
bucket = buckets.get(6);
assertEquals("2016-01-01T00:00:00.000Z", bucket.getKeyAsString());
assertEquals(1, bucket.getDocCount());
stats = bucket.getAggregations().get("stats");
assertEquals("2016-03-04T17:09:50.000Z", stats.getMinAsString());
assertEquals("2016-03-04T17:09:50.000Z", stats.getMaxAsString());
assertEquals(1L, stats.getCount());
assertTrue(AggregationInspectionHelper.hasValue(stats));
bucket = buckets.get(7);
assertEquals("2017-01-01T00:00:00.000Z", bucket.getKeyAsString());
assertEquals(1, bucket.getDocCount());
stats = bucket.getAggregations().get("stats");
assertEquals("2017-12-12T22:55:46.000Z", stats.getMinAsString());
assertEquals("2017-12-12T22:55:46.000Z", stats.getMaxAsString());
assertEquals(1L, stats.getCount());
assertTrue(AggregationInspectionHelper.hasValue(stats));
});
}
use of org.opensearch.search.aggregations.metrics.InternalStats in project OpenSearch by opensearch-project.
the class VariableWidthHistogramAggregatorTests method testSimpleSubAggregations.
public void testSimpleSubAggregations() throws IOException {
final List<Number> dataset = Arrays.asList(5, 1, 9, 2, 8);
testSearchCase(DEFAULT_QUERY, dataset, false, aggregation -> aggregation.field(NUMERIC_FIELD).setNumBuckets(3).setInitialBuffer(3).setShardSize(4).subAggregation(AggregationBuilders.stats("stats").field(NUMERIC_FIELD)), histogram -> {
double deltaError = 1d / 10000d;
// Expected clusters: [ (1, 2), (5), (8,9) ]
InternalStats stats = histogram.getBuckets().get(0).getAggregations().get("stats");
assertEquals(1, stats.getMin(), deltaError);
assertEquals(2, stats.getMax(), deltaError);
assertEquals(2, stats.getCount());
assertTrue(AggregationInspectionHelper.hasValue(stats));
stats = histogram.getBuckets().get(1).getAggregations().get("stats");
assertEquals(5, stats.getMin(), deltaError);
assertEquals(5, stats.getMax(), deltaError);
assertEquals(1, stats.getCount());
assertTrue(AggregationInspectionHelper.hasValue(stats));
stats = histogram.getBuckets().get(2).getAggregations().get("stats");
assertEquals(8, stats.getMin(), deltaError);
assertEquals(9, stats.getMax(), deltaError);
assertEquals(2, stats.getCount());
assertTrue(AggregationInspectionHelper.hasValue(stats));
});
}
Aggregations