Skip to content

Commit b13e157

Browse files
committed
Append date_optional_time format when extended_bounds are specified
See #1784
1 parent 1e36b41 commit b13e157

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

Diff for: src/Nest/Aggregations/Bucket/DateHistogram/DateHistogramAggregation.cs

+28-2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,25 @@ public interface IDateHistogramAggregation : IBucketAggregation
4848

4949
public class DateHistogramAggregation : BucketAggregationBase, IDateHistogramAggregation
5050
{
51+
private string _format;
5152
public Field Field { get; set; }
5253
public IScript Script { get; set; }
5354
public IDictionary<string, object> Params { get; set; }
5455
public Union<DateInterval, Time> Interval { get; set; }
55-
public string Format { get; set; }
56+
57+
public string Format
58+
{
59+
get
60+
{
61+
return !string.IsNullOrEmpty(_format) &&
62+
!_format.Contains("date_optional_time") &&
63+
ExtendedBounds != null
64+
? _format + "||date_optional_time"
65+
: _format;
66+
}
67+
set { _format = value; }
68+
}
69+
5670
public int? MinimumDocumentCount { get; set; }
5771
public string TimeZone { get; set; }
5872
public int? Factor { get; set; }
@@ -73,6 +87,7 @@ public class DateHistogramAggregationDescriptor<T>
7387
, IDateHistogramAggregation
7488
where T : class
7589
{
90+
private string _format;
7691
Field IDateHistogramAggregation.Field { get; set; }
7792

7893
IScript IDateHistogramAggregation.Script { get; set; }
@@ -81,7 +96,18 @@ public class DateHistogramAggregationDescriptor<T>
8196

8297
Union<DateInterval, Time> IDateHistogramAggregation.Interval { get; set; }
8398

84-
string IDateHistogramAggregation.Format { get; set; }
99+
string IDateHistogramAggregation.Format
100+
{
101+
get
102+
{
103+
return !string.IsNullOrEmpty(_format) &&
104+
!_format.Contains("date_optional_time") &&
105+
Self.ExtendedBounds != null
106+
? _format + "||date_optional_time"
107+
: _format;
108+
}
109+
set { _format = value; }
110+
}
85111

86112
int? IDateHistogramAggregation.MinimumDocumentCount { get; set; }
87113

Diff for: src/Tests/Aggregations/Bucket/DateHistogram/DateHistogramAggregationUsageTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace Tests.Aggregations.Bucket.DateHistogram
1414
* The main difference is that the interval can be specified by date/time expressions.
1515
*
1616
* When specifying a format and extended_bounds, in order for Elasticsearch to be able to parse
17-
* the serialized DateTimes extended_bounds correctly, the date_optional_time format should
18-
* also be specified as an additional format.
17+
* the serialized DateTimes of extended_bounds correctly, the date_optional_time format is included
18+
* as part of the format value.
1919
*
2020
* Be sure to read the elasticsearch documentation {ref}/search-aggregations-bucket-datehistogram-aggregation.html[on this subject here]
2121
*/
@@ -72,7 +72,7 @@ public DateHistogramAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage
7272
.Field(p => p.StartedOn)
7373
.Interval(DateInterval.Month)
7474
.MinimumDocumentCount(2)
75-
.Format("yyyy-MM-dd'T'HH:mm:ss||date_optional_time")
75+
.Format("yyyy-MM-dd'T'HH:mm:ss")
7676
.ExtendedBounds(FixedDate.AddYears(-1), FixedDate.AddYears(1))
7777
.Order(HistogramOrder.CountAscending)
7878
.Missing(FixedDate)
@@ -96,7 +96,7 @@ public DateHistogramAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage
9696
Field = Field<Project>(p => p.StartedOn),
9797
Interval = DateInterval.Month,
9898
MinimumDocumentCount = 2,
99-
Format = "yyyy-MM-dd'T'HH:mm:ss||date_optional_time",
99+
Format = "yyyy-MM-dd'T'HH:mm:ss",
100100
ExtendedBounds = new ExtendedBounds<DateTime>
101101
{
102102
Minimum = FixedDate.AddYears(-1),

0 commit comments

Comments
 (0)