Skip to content

Commit c2277b6

Browse files
committed
Support DateMath for DateHistogram ExtendedBounds
Non-binary breaking backport of #2829 Closes #2828 (cherry picked from commit f640b05)
1 parent 2e04be8 commit c2277b6

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

docs/aggregations/bucket/date-histogram/date-histogram-aggregation-usage.asciidoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ s => s
3939
.Interval(DateInterval.Month)
4040
.MinimumDocumentCount(2)
4141
.Format("yyyy-MM-dd'T'HH:mm:ss")
42-
.ExtendedBounds(FixedDate.AddYears(-1), FixedDate.AddYears(1))
42+
.ExtendedBoundsDateMath(FixedDate.AddYears(-1), FixedDate.AddYears(1))
4343
.Order(HistogramOrder.CountAscending)
4444
.Missing(FixedDate)
4545
.Aggregations(childAggs => childAggs
@@ -67,7 +67,7 @@ new SearchRequest<Project>
6767
Interval = DateInterval.Month,
6868
MinimumDocumentCount = 2,
6969
Format = "yyyy-MM-dd'T'HH:mm:ss",
70-
ExtendedBounds = new ExtendedBounds<DateTime>
70+
ExtendedBoundsDateMath = new ExtendedBounds<DateMath>
7171
{
7272
Minimum = FixedDate.AddYears(-1),
7373
Maximum = FixedDate.AddYears(1),

src/Nest/Aggregations/Bucket/DateHistogram/DateHistogramAggregation.cs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,21 @@ public interface IDateHistogramAggregation : IBucketAggregation
4040
[JsonProperty("order")]
4141
HistogramOrder Order { get; set; }
4242

43-
[JsonProperty("extended_bounds")]
43+
[JsonIgnore]
44+
[Obsolete("Use ExtendedBoundsDateMath that accepts DateMath expressions. Fixed in NEST 6.x")]
4445
ExtendedBounds<DateTime> ExtendedBounds { get; set; }
4546

47+
[JsonProperty("extended_bounds")]
48+
ExtendedBounds<DateMath> ExtendedBoundsDateMath { get; set; }
49+
4650
[JsonProperty("missing")]
4751
DateTime? Missing { get; set; }
4852
}
4953

5054
public class DateHistogramAggregation : BucketAggregationBase, IDateHistogramAggregation
5155
{
5256
private string _format;
57+
private ExtendedBounds<DateTime> _extendedBounds;
5358
public Field Field { get; set; }
5459
public IScript Script { get; set; }
5560
public IDictionary<string, object> Params { get; set; }
@@ -59,7 +64,7 @@ public string Format
5964
{
6065
get => !string.IsNullOrEmpty(_format) &&
6166
!_format.Contains("date_optional_time") &&
62-
(ExtendedBounds != null || Missing.HasValue)
67+
(ExtendedBoundsDateMath != null || Missing.HasValue)
6368
? _format + "||date_optional_time"
6469
: _format;
6570
set => _format = value;
@@ -73,7 +78,24 @@ public string Format
7378

7479
public string Offset { get; set; }
7580
public HistogramOrder Order { get; set; }
76-
public ExtendedBounds<DateTime> ExtendedBounds { get; set; }
81+
82+
[Obsolete("Use ExtendedBoundsDateMath that accepts DateMath expressions. Fixed in NEST 6.x")]
83+
public ExtendedBounds<DateTime> ExtendedBounds
84+
{
85+
get => _extendedBounds;
86+
set
87+
{
88+
_extendedBounds = value;
89+
ExtendedBoundsDateMath = new ExtendedBounds<DateMath>
90+
{
91+
Minimum = value.Minimum,
92+
Maximum = value.Maximum
93+
};
94+
}
95+
}
96+
97+
public ExtendedBounds<DateMath> ExtendedBoundsDateMath { get; set; }
98+
7799
public DateTime? Missing { get; set; }
78100

79101
internal DateHistogramAggregation() { }
@@ -89,6 +111,7 @@ public class DateHistogramAggregationDescriptor<T>
89111
where T : class
90112
{
91113
private string _format;
114+
private ExtendedBounds<DateTime> _extendedBounds;
92115
Field IDateHistogramAggregation.Field { get; set; }
93116

94117
IScript IDateHistogramAggregation.Script { get; set; }
@@ -101,7 +124,7 @@ string IDateHistogramAggregation.Format
101124
{
102125
get => !string.IsNullOrEmpty(_format) &&
103126
!_format.Contains("date_optional_time") &&
104-
(Self.ExtendedBounds != null || Self.Missing.HasValue)
127+
(Self.ExtendedBoundsDateMath != null || Self.Missing.HasValue)
105128
? _format + "||date_optional_time"
106129
: _format;
107130
set => _format = value;
@@ -117,7 +140,22 @@ string IDateHistogramAggregation.Format
117140

118141
HistogramOrder IDateHistogramAggregation.Order { get; set; }
119142

120-
ExtendedBounds<DateTime> IDateHistogramAggregation.ExtendedBounds { get; set; }
143+
[Obsolete("Use ExtendedBoundsDateMath that accepts DateMath expressions. Fixed in NEST 6.x")]
144+
ExtendedBounds<DateTime> IDateHistogramAggregation.ExtendedBounds
145+
{
146+
get => _extendedBounds;
147+
set
148+
{
149+
_extendedBounds = value;
150+
Self.ExtendedBoundsDateMath = new ExtendedBounds<DateMath>
151+
{
152+
Minimum = value.Minimum,
153+
Maximum = value.Maximum
154+
};
155+
}
156+
}
157+
158+
ExtendedBounds<DateMath> IDateHistogramAggregation.ExtendedBoundsDateMath { get; set; }
121159

122160
DateTime? IDateHistogramAggregation.Missing { get; set; }
123161

@@ -155,9 +193,13 @@ public DateHistogramAggregationDescriptor<T> OrderAscending(string key) =>
155193
public DateHistogramAggregationDescriptor<T> OrderDescending(string key) =>
156194
Assign(a => a.Order = new HistogramOrder { Key = key, Order = SortOrder.Descending });
157195

196+
[Obsolete("Use ExtendedBoundsDateMath() that accepts DateMath expressions. Fixed in NEST 6.x")]
158197
public DateHistogramAggregationDescriptor<T> ExtendedBounds(DateTime min, DateTime max) =>
159198
Assign(a=>a.ExtendedBounds = new ExtendedBounds<DateTime> { Minimum = min, Maximum = max });
160199

200+
public DateHistogramAggregationDescriptor<T> ExtendedBoundsDateMath(DateMath min, DateMath max) =>
201+
Assign(a=>a.ExtendedBoundsDateMath = new ExtendedBounds<DateMath> { Minimum = min, Maximum = max });
202+
161203
public DateHistogramAggregationDescriptor<T> Missing(DateTime missing) => Assign(a => a.Missing = missing);
162204
}
163205
}

src/Tests/Aggregations/Bucket/DateHistogram/DateHistogramAggregationUsageTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public DateHistogramAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage
7575
.Interval(DateInterval.Month)
7676
.MinimumDocumentCount(2)
7777
.Format("yyyy-MM-dd'T'HH:mm:ss")
78-
.ExtendedBounds(FixedDate.AddYears(-1), FixedDate.AddYears(1))
78+
.ExtendedBoundsDateMath(FixedDate.AddYears(-1), FixedDate.AddYears(1))
7979
.Order(HistogramOrder.CountAscending)
8080
.Missing(FixedDate)
8181
.Aggregations(childAggs => childAggs
@@ -99,7 +99,7 @@ public DateHistogramAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage
9999
Interval = DateInterval.Month,
100100
MinimumDocumentCount = 2,
101101
Format = "yyyy-MM-dd'T'HH:mm:ss",
102-
ExtendedBounds = new ExtendedBounds<DateTime>
102+
ExtendedBoundsDateMath = new ExtendedBounds<DateMath>
103103
{
104104
Minimum = FixedDate.AddYears(-1),
105105
Maximum = FixedDate.AddYears(1),

0 commit comments

Comments
 (0)