diff --git a/server/src/main/java/org/opensearch/search/aggregations/metrics/MaxAggregator.java b/server/src/main/java/org/opensearch/search/aggregations/metrics/MaxAggregator.java index 5cd2bfe41069c..49aaf5e0670bb 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/metrics/MaxAggregator.java +++ b/server/src/main/java/org/opensearch/search/aggregations/metrics/MaxAggregator.java @@ -271,20 +271,12 @@ public StarTreeBucketCollector getStarTreeBucketCollector( MetricStat.MAX.getTypeName(), valuesSource, parentCollector, - (bucket) -> maxes = context.bigArrays().grow(maxes, bucket + 1), - // (bucket, metricValue) -> maxes.set(bucket, Math.max(maxes.get(bucket), )) - (bucket, metricValue) -> { - // Retrieve the current bucket value (it could be NaN initially) - double currentMax = maxes.get(bucket); - - // Check if the currentMax is NaN or not set, and initialize it with the first metric value - if (Double.isNaN(currentMax)) { - maxes.set(bucket, NumericUtils.sortableLongToDouble(metricValue)); // Set the first value if the bucket was - // uninitialized - } else { - maxes.set(bucket, Math.max(currentMax, NumericUtils.sortableLongToDouble(metricValue))); // Perform the max comparison - } - } + (bucket) -> { + long from = maxes.size(); + maxes = context.bigArrays().grow(maxes, bucket + 1); + maxes.fill(from, maxes.size(), Double.NEGATIVE_INFINITY); + }, + (bucket, metricValue) -> maxes.set(bucket, Math.max(maxes.get(bucket), (NumericUtils.sortableLongToDouble(metricValue)))) ); } } diff --git a/server/src/main/java/org/opensearch/search/aggregations/metrics/MinAggregator.java b/server/src/main/java/org/opensearch/search/aggregations/metrics/MinAggregator.java index 83d2de4440d8d..febb227dd4e2a 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/metrics/MinAggregator.java +++ b/server/src/main/java/org/opensearch/search/aggregations/metrics/MinAggregator.java @@ -265,8 +265,12 @@ public StarTreeBucketCollector getStarTreeBucketCollector( MetricStat.MIN.getTypeName(), valuesSource, parentCollector, - (bucket) -> mins = context.bigArrays().grow(mins, bucket + 1), - (bucket, metricValue) -> mins.set(bucket, Math.max(mins.get(bucket), NumericUtils.sortableLongToDouble(metricValue))) + (bucket) -> { + long from = mins.size(); + mins = context.bigArrays().grow(mins, bucket + 1); + mins.fill(from, mins.size(), Double.POSITIVE_INFINITY); + }, + (bucket, metricValue) -> mins.set(bucket, Math.min(mins.get(bucket), NumericUtils.sortableLongToDouble(metricValue))) ); } }