Skip to content

Support DateMath for DateHistogram ExtendedBounds #2962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ s => s
.Interval(DateInterval.Month)
.MinimumDocumentCount(2)
.Format("yyyy-MM-dd'T'HH:mm:ss")
.ExtendedBounds(FixedDate.AddYears(-1), FixedDate.AddYears(1))
.ExtendedBoundsDateMath(FixedDate.AddYears(-1), FixedDate.AddYears(1))
.Order(HistogramOrder.CountAscending)
.Missing(FixedDate)
.Aggregations(childAggs => childAggs
Expand Down Expand Up @@ -67,7 +67,7 @@ new SearchRequest<Project>
Interval = DateInterval.Month,
MinimumDocumentCount = 2,
Format = "yyyy-MM-dd'T'HH:mm:ss",
ExtendedBounds = new ExtendedBounds<DateTime>
ExtendedBoundsDateMath = new ExtendedBounds<DateMath>
{
Minimum = FixedDate.AddYears(-1),
Maximum = FixedDate.AddYears(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@ public interface IDateHistogramAggregation : IBucketAggregation
[JsonProperty("order")]
HistogramOrder Order { get; set; }

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

[JsonProperty("extended_bounds")]
ExtendedBounds<DateMath> ExtendedBoundsDateMath { get; set; }

[JsonProperty("missing")]
DateTime? Missing { get; set; }
}

public class DateHistogramAggregation : BucketAggregationBase, IDateHistogramAggregation
{
private string _format;
private ExtendedBounds<DateTime> _extendedBounds;
public Field Field { get; set; }
public IScript Script { get; set; }
public IDictionary<string, object> Params { get; set; }
Expand All @@ -55,7 +60,7 @@ public string Format
{
get => !string.IsNullOrEmpty(_format) &&
!_format.Contains("date_optional_time") &&
(ExtendedBounds != null || Missing.HasValue)
(ExtendedBoundsDateMath != null || Missing.HasValue)
? _format + "||date_optional_time"
: _format;
set => _format = value;
Expand All @@ -65,7 +70,24 @@ public string Format
public string TimeZone { get; set; }
public string Offset { get; set; }
public HistogramOrder Order { get; set; }
public ExtendedBounds<DateTime> ExtendedBounds { get; set; }

[Obsolete("Use ExtendedBoundsDateMath that accepts DateMath expressions. Fixed in NEST 6.x")]
public ExtendedBounds<DateTime> ExtendedBounds
{
get => _extendedBounds;
set
{
_extendedBounds = value;
ExtendedBoundsDateMath = new ExtendedBounds<DateMath>
{
Minimum = value.Minimum,
Maximum = value.Maximum
};
}
}

public ExtendedBounds<DateMath> ExtendedBoundsDateMath { get; set; }

public DateTime? Missing { get; set; }

internal DateHistogramAggregation() { }
Expand All @@ -81,6 +103,7 @@ public class DateHistogramAggregationDescriptor<T>
where T : class
{
private string _format;
private ExtendedBounds<DateTime> _extendedBounds;
Field IDateHistogramAggregation.Field { get; set; }

IScript IDateHistogramAggregation.Script { get; set; }
Expand All @@ -93,7 +116,7 @@ string IDateHistogramAggregation.Format
{
get => !string.IsNullOrEmpty(_format) &&
!_format.Contains("date_optional_time") &&
(Self.ExtendedBounds != null || Self.Missing.HasValue)
(Self.ExtendedBoundsDateMath != null || Self.Missing.HasValue)
? _format + "||date_optional_time"
: _format;
set => _format = value;
Expand All @@ -107,7 +130,22 @@ string IDateHistogramAggregation.Format

HistogramOrder IDateHistogramAggregation.Order { get; set; }

ExtendedBounds<DateTime> IDateHistogramAggregation.ExtendedBounds { get; set; }
[Obsolete("Use ExtendedBoundsDateMath that accepts DateMath expressions. Fixed in NEST 6.x")]
ExtendedBounds<DateTime> IDateHistogramAggregation.ExtendedBounds
{
get => _extendedBounds;
set
{
_extendedBounds = value;
Self.ExtendedBoundsDateMath = new ExtendedBounds<DateMath>
{
Minimum = value.Minimum,
Maximum = value.Maximum
};
}
}

ExtendedBounds<DateMath> IDateHistogramAggregation.ExtendedBoundsDateMath { get; set; }

DateTime? IDateHistogramAggregation.Missing { get; set; }

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

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

public DateHistogramAggregationDescriptor<T> ExtendedBoundsDateMath(DateMath min, DateMath max) =>
Assign(a=>a.ExtendedBoundsDateMath = new ExtendedBounds<DateMath> { Minimum = min, Maximum = max });

public DateHistogramAggregationDescriptor<T> Missing(DateTime missing) => Assign(a => a.Missing = missing);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public DateHistogramAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage
.Interval(DateInterval.Month)
.MinimumDocumentCount(2)
.Format("yyyy-MM-dd'T'HH:mm:ss")
.ExtendedBounds(FixedDate.AddYears(-1), FixedDate.AddYears(1))
.ExtendedBoundsDateMath(FixedDate.AddYears(-1), FixedDate.AddYears(1))
.Order(HistogramOrder.CountAscending)
.Missing(FixedDate)
.Aggregations(childAggs => childAggs
Expand All @@ -99,7 +99,7 @@ public DateHistogramAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage
Interval = DateInterval.Month,
MinimumDocumentCount = 2,
Format = "yyyy-MM-dd'T'HH:mm:ss",
ExtendedBounds = new ExtendedBounds<DateTime>
ExtendedBoundsDateMath = new ExtendedBounds<DateMath>
{
Minimum = FixedDate.AddYears(-1),
Maximum = FixedDate.AddYears(1),
Expand Down