use of org.opensearch.search.aggregations.metrics.StatsAggregationBuilder in project fess by codelibs.
the class EsAbstractConditionAggregation method regStatsA.
protected StatsAggregationBuilder regStatsA(String name, String field) {
StatsAggregationBuilder builder = AggregationBuilders.stats(name).field(field);
regA(builder);
return builder;
}
use of org.opensearch.search.aggregations.metrics.StatsAggregationBuilder 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.StatsAggregationBuilder in project OpenSearch by opensearch-project.
the class VariableWidthHistogramAggregatorTests method testMultipleSegments.
public void testMultipleSegments() throws IOException {
final List<Number> dataset = Arrays.asList(1001, 1002, 1, 2, 1003, 3, 1004, 1005, 4, 5);
// There should be two clusters: (1, 2, 3, 4, 5) and (1001, 1002, 1003, 1004, 1005)
// We can't enable multiple segments per index for many of the tests above, because the clusters are too close.
// Slight randomization --> different caches in the aggregator --> different clusters
// However, these two clusters are so far apart that even if a doc from one ends up in the other,
// the centroids will not change much.
// To account for this case of a document switching clusters, we check that each cluster centroid is within
// a certain range, rather than asserting exact values.
testSearchCase(DEFAULT_QUERY, dataset, true, aggregation -> aggregation.field(NUMERIC_FIELD).setNumBuckets(2).setInitialBuffer(4).setShardSize(3).subAggregation(new StatsAggregationBuilder("stats").field(NUMERIC_FIELD)), histogram -> {
final List<InternalVariableWidthHistogram.Bucket> buckets = histogram.getBuckets();
double deltaError = 1d / 10000d;
assertEquals(2, buckets.size());
// The lower cluster
assertThat(buckets.get(0).getDocCount(), both(greaterThanOrEqualTo(4L)).and(lessThanOrEqualTo(7L)));
assertThat(buckets.get(0).centroid(), both(greaterThanOrEqualTo(0.0)).and(lessThanOrEqualTo(300.0)));
assertEquals(1, buckets.get(0).min(), deltaError);
// The higher cluster
assertThat(buckets.get(1).getDocCount(), equalTo(dataset.size() - buckets.get(0).getDocCount()));
assertThat(buckets.get(1).centroid(), both(greaterThanOrEqualTo(800.0)).and(lessThanOrEqualTo(1005.0)));
assertEquals(1005, buckets.get(1).max(), deltaError);
});
}
use of org.opensearch.search.aggregations.metrics.StatsAggregationBuilder in project OpenSearch by opensearch-project.
the class DerivativeAggregatorTests method testMultiValueAggDerivative.
public void testMultiValueAggDerivative() throws IOException {
setupValueCounts();
Query query = new MatchAllDocsQuery();
HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval);
aggBuilder.subAggregation(new StatsAggregationBuilder("stats").field(SINGLE_VALUED_FIELD_NAME));
aggBuilder.subAggregation(new DerivativePipelineAggregationBuilder("deriv", "stats.sum"));
executeTestCase(query, aggBuilder, histogram -> {
assertThat(histogram, notNullValue());
assertThat(histogram.getName(), equalTo("histo"));
List<? extends Histogram.Bucket> buckets = ((Histogram) histogram).getBuckets();
assertThat(buckets.size(), equalTo(numValueBuckets));
assertThat(histogram, notNullValue());
assertThat(histogram.getName(), equalTo("histo"));
Object[] propertiesKeys = (Object[]) ((InternalAggregation) histogram).getProperty("_key");
Object[] propertiesDocCounts = (Object[]) ((InternalAggregation) histogram).getProperty("_count");
Object[] propertiesSumCounts = (Object[]) ((InternalAggregation) histogram).getProperty("stats.sum");
// start value, gets
Long expectedSumPreviousBucket = Long.MIN_VALUE;
// overwritten
for (int i = 0; i < numValueBuckets; ++i) {
Histogram.Bucket bucket = buckets.get(i);
checkBucketKeyAndDocCount("InternalBucket " + i, bucket, i * interval, valueCounts[i]);
Stats stats = bucket.getAggregations().get("stats");
assertThat(stats, notNullValue());
long expectedSum = valueCounts[i] * (i * interval);
assertThat(stats.getSum(), equalTo((double) expectedSum));
SimpleValue sumDeriv = bucket.getAggregations().get("deriv");
if (i > 0) {
assertThat(sumDeriv, notNullValue());
long sumDerivValue = expectedSum - expectedSumPreviousBucket;
assertThat(sumDeriv.value(), equalTo((double) sumDerivValue));
assertThat(((InternalMultiBucketAggregation.InternalBucket) bucket).getProperty("histo", AggregationPath.parse("deriv.value").getPathElementsAsStringList()), equalTo((double) sumDerivValue));
} else {
assertThat(sumDeriv, nullValue());
}
expectedSumPreviousBucket = expectedSum;
assertThat(propertiesKeys[i], equalTo((double) i * interval));
assertThat((long) propertiesDocCounts[i], equalTo(valueCounts[i]));
assertThat((double) propertiesSumCounts[i], equalTo((double) expectedSum));
}
});
}
use of org.opensearch.search.aggregations.metrics.StatsAggregationBuilder in project fess by codelibs.
the class EsAbstractConditionAggregation method regStatsA.
protected StatsAggregationBuilder regStatsA(String name, String field) {
StatsAggregationBuilder builder = AggregationBuilders.stats(name).field(field);
regA(builder);
return builder;
}
Aggregations