diff --git a/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregator.java b/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregator.java index 14d820d1ce879..4dc4c40138719 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregator.java +++ b/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregator.java @@ -162,7 +162,7 @@ private AutoDateHistogramAggregator( // Create the filters for fast aggregation only if the query is instance // of point range query and there aren't any parent/sub aggregations - if (parent() == null && subAggregators.length == 0) { + if (parent() == null && subAggregators.length == 0 && valuesSourceConfig.missing() == null && valuesSourceConfig.script() == null) { final FieldContext fieldContext = valuesSourceConfig.fieldContext(); if (fieldContext != null) { final String fieldName = fieldContext.field(); diff --git a/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/DateHistogramAggregator.java b/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/DateHistogramAggregator.java index 59c508fa3a5ea..659cb54d3affe 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/DateHistogramAggregator.java +++ b/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/DateHistogramAggregator.java @@ -121,7 +121,7 @@ class DateHistogramAggregator extends BucketsAggregator implements SizedBucketAg // Create the filters for fast aggregation only if the query is instance // of point range query and there aren't any parent/sub aggregations - if (parent() == null && subAggregators.length == 0) { + if (parent() == null && subAggregators.length == 0 && valuesSourceConfig.missing() == null && valuesSourceConfig.script() == null) { final FieldContext fieldContext = valuesSourceConfig.fieldContext(); if (fieldContext != null) { final String fieldName = fieldContext.field(); @@ -129,6 +129,13 @@ class DateHistogramAggregator extends BucketsAggregator implements SizedBucketAg if (bounds != null) { assert fieldContext.fieldType() instanceof DateFieldMapper.DateFieldType; fieldType = (DateFieldMapper.DateFieldType) fieldContext.fieldType(); + + // Update min/max limit if user specified any hard bounds + if (hardBounds != null) { + bounds[0] = Math.max(bounds[0], hardBounds.getMin()); + bounds[1] = Math.min(bounds[1], hardBounds.getMax()); + } + filters = FilterRewriteHelper.createFilterForAggregations( context, rounding,