Skip to content

Commit 8e445ae

Browse files
committed
Support DateMath for DateHistogram ExtendedBounds
Non-binary breaking backport of #2829 Closes #2828
1 parent 94aea2f commit 8e445ae

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
@@ -36,16 +36,21 @@ public interface IDateHistogramAggregation : IBucketAggregation
3636
[JsonProperty("order")]
3737
HistogramOrder Order { get; set; }
3838

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

43+
[JsonProperty("extended_bounds")]
44+
ExtendedBounds<DateMath> ExtendedBoundsDateMath { get; set; }
45+
4246
[JsonProperty("missing")]
4347
DateTime? Missing { get; set; }
4448
}
4549

4650
public class DateHistogramAggregation : BucketAggregationBase, IDateHistogramAggregation
4751
{
4852
private string _format;
53+
private ExtendedBounds<DateTime> _extendedBounds;
4954
public Field Field { get; set; }
5055
public IScript Script { get; set; }
5156
public IDictionary<string, object> Params { get; set; }
@@ -55,7 +60,7 @@ public string Format
5560
{
5661
get => !string.IsNullOrEmpty(_format) &&
5762
!_format.Contains("date_optional_time") &&
58-
(ExtendedBounds != null || Missing.HasValue)
63+
(ExtendedBoundsDateMath != null || Missing.HasValue)
5964
? _format + "||date_optional_time"
6065
: _format;
6166
set => _format = value;
@@ -65,7 +70,24 @@ public string Format
6570
public string TimeZone { get; set; }
6671
public string Offset { get; set; }
6772
public HistogramOrder Order { get; set; }
68-
public ExtendedBounds<DateTime> ExtendedBounds { get; set; }
73+
74+
[Obsolete("Use ExtendedBoundsDateMath that accepts DateMath expressions. Fixed in NEST 6.x")]
75+
public ExtendedBounds<DateTime> ExtendedBounds
76+
{
77+
get => _extendedBounds;
78+
set
79+
{
80+
_extendedBounds = value;
81+
ExtendedBoundsDateMath = new ExtendedBounds<DateMath>
82+
{
83+
Minimum = value.Minimum,
84+
Maximum = value.Maximum
85+
};
86+
}
87+
}
88+
89+
public ExtendedBounds<DateMath> ExtendedBoundsDateMath { get; set; }
90+
6991
public DateTime? Missing { get; set; }
7092

7193
internal DateHistogramAggregation() { }
@@ -81,6 +103,7 @@ public class DateHistogramAggregationDescriptor<T>
81103
where T : class
82104
{
83105
private string _format;
106+
private ExtendedBounds<DateTime> _extendedBounds;
84107
Field IDateHistogramAggregation.Field { get; set; }
85108

86109
IScript IDateHistogramAggregation.Script { get; set; }
@@ -93,7 +116,7 @@ string IDateHistogramAggregation.Format
93116
{
94117
get => !string.IsNullOrEmpty(_format) &&
95118
!_format.Contains("date_optional_time") &&
96-
(Self.ExtendedBounds != null || Self.Missing.HasValue)
119+
(Self.ExtendedBoundsDateMath != null || Self.Missing.HasValue)
97120
? _format + "||date_optional_time"
98121
: _format;
99122
set => _format = value;
@@ -107,7 +130,22 @@ string IDateHistogramAggregation.Format
107130

108131
HistogramOrder IDateHistogramAggregation.Order { get; set; }
109132

110-
ExtendedBounds<DateTime> IDateHistogramAggregation.ExtendedBounds { get; set; }
133+
[Obsolete("Use ExtendedBoundsDateMath that accepts DateMath expressions. Fixed in NEST 6.x")]
134+
ExtendedBounds<DateTime> IDateHistogramAggregation.ExtendedBounds
135+
{
136+
get => _extendedBounds;
137+
set
138+
{
139+
_extendedBounds = value;
140+
Self.ExtendedBoundsDateMath = new ExtendedBounds<DateMath>
141+
{
142+
Minimum = value.Minimum,
143+
Maximum = value.Maximum
144+
};
145+
}
146+
}
147+
148+
ExtendedBounds<DateMath> IDateHistogramAggregation.ExtendedBoundsDateMath { get; set; }
111149

112150
DateTime? IDateHistogramAggregation.Missing { get; set; }
113151

@@ -142,9 +180,13 @@ public DateHistogramAggregationDescriptor<T> OrderAscending(string key) =>
142180
public DateHistogramAggregationDescriptor<T> OrderDescending(string key) =>
143181
Assign(a => a.Order = new HistogramOrder { Key = key, Order = SortOrder.Descending });
144182

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

187+
public DateHistogramAggregationDescriptor<T> ExtendedBoundsDateMath(DateMath min, DateMath max) =>
188+
Assign(a=>a.ExtendedBoundsDateMath = new ExtendedBounds<DateMath> { Minimum = min, Maximum = max });
189+
148190
public DateHistogramAggregationDescriptor<T> Missing(DateTime missing) => Assign(a => a.Missing = missing);
149191
}
150192
}

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)