From 56977239ba99d41c0e4e905980a7eeb36da96e10 Mon Sep 17 00:00:00 2001 From: Russ Cam Date: Wed, 10 Jun 2020 16:54:38 +1000 Subject: [PATCH] Add FixedInterval and CalendarInterval to IDateHistogramCompositeAggregationSource This commit adds FixedInterval and CalendarInterval to IDateHistogramCompositeAggregationSource and deprecates Interval. FixedInterval uses Time type as units must be specified and the largest unit supported is Day. CalendarInterval can specify either a DateInterval or a DateMathTime with a fixed unit of 1. Closes #4695. --- .../composite-aggregation-usage.asciidoc | 6 +-- ...DateHistogramCompositeAggregationSource.cs | 39 ++++++++++++++++++- .../CompositeAggregationUsageTests.cs | 14 +++---- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/docs/aggregations/bucket/composite/composite-aggregation-usage.asciidoc b/docs/aggregations/bucket/composite/composite-aggregation-usage.asciidoc index 5cb3cc37f9d..1e333414d47 100644 --- a/docs/aggregations/bucket/composite/composite-aggregation-usage.asciidoc +++ b/docs/aggregations/bucket/composite/composite-aggregation-usage.asciidoc @@ -41,7 +41,7 @@ a => a ) .DateHistogram("started", d => d .Field(f => f.StartedOn) - .Interval(DateInterval.Month) + .CalendarInterval(DateInterval.Month) ) .Histogram("branch_count", h => h .Field(f => f.RequiredBranches) @@ -78,7 +78,7 @@ new CompositeAggregation("my_buckets") new DateHistogramCompositeAggregationSource("started") { Field = Field(f => f.StartedOn), - Interval = DateInterval.Month + CalendarInterval = DateInterval.Month }, new HistogramCompositeAggregationSource("branch_count") { @@ -120,7 +120,7 @@ new CompositeAggregation("my_buckets") "started": { "date_histogram": { "field": "startedOn", - "interval": "month" + "calendar_interval": "month" } } }, diff --git a/src/Nest/Aggregations/Bucket/Composite/DateHistogramCompositeAggregationSource.cs b/src/Nest/Aggregations/Bucket/Composite/DateHistogramCompositeAggregationSource.cs index ff1ea7c4b1d..658f1d8af0d 100644 --- a/src/Nest/Aggregations/Bucket/Composite/DateHistogramCompositeAggregationSource.cs +++ b/src/Nest/Aggregations/Bucket/Composite/DateHistogramCompositeAggregationSource.cs @@ -2,6 +2,7 @@ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information +using System; using System.Runtime.Serialization; namespace Nest @@ -21,11 +22,24 @@ public interface IDateHistogramCompositeAggregationSource : ICompositeAggregatio string Format { get; set; } /// - /// The interval to use when bucketing documents + /// The interval to use when bucketing documents /// [DataMember(Name ="interval")] + [Obsolete("Use FixedInterval or CalendarInterval")] Union Interval { get; set; } + /// + /// The calendar interval to use when bucketing documents + /// + [DataMember(Name ="calendar_interval")] + public Union CalendarInterval { get; set; } + + /// + /// The fixed interval to use when bucketing documents + /// + [DataMember(Name ="fixed_interval")] + public Time FixedInterval { get; set; } + /// /// Used to indicate that bucketing should use a different time zone. /// Time zones may either be specified as an ISO 8601 UTC offset (e.g. +01:00 or -08:00) @@ -44,8 +58,15 @@ public DateHistogramCompositeAggregationSource(string name) : base(name) { } public string Format { get; set; } /// + [Obsolete("Use FixedInterval or CalendarInterval")] public Union Interval { get; set; } + /// + public Union CalendarInterval { get; set; } + + /// + public Time FixedInterval { get; set; } + /// public string TimeZone { get; set; } @@ -62,16 +83,32 @@ public DateHistogramCompositeAggregationSourceDescriptor(string name) : base(nam string IDateHistogramCompositeAggregationSource.Format { get; set; } Union IDateHistogramCompositeAggregationSource.Interval { get; set; } + Union IDateHistogramCompositeAggregationSource.CalendarInterval { get; set; } + Time IDateHistogramCompositeAggregationSource.FixedInterval { get; set; } string IDateHistogramCompositeAggregationSource.TimeZone { get; set; } /// + [Obsolete("Use FixedInterval or CalendarInterval")] public DateHistogramCompositeAggregationSourceDescriptor Interval(DateInterval? interval) => Assign(interval, (a, v) => a.Interval = v); /// + [Obsolete("Use FixedInterval or CalendarInterval")] public DateHistogramCompositeAggregationSourceDescriptor Interval(Time interval) => Assign(interval, (a, v) => a.Interval = v); + /// + public DateHistogramCompositeAggregationSourceDescriptor CalendarInterval(DateInterval? interval) => + Assign(interval, (a, v) => a.CalendarInterval = v); + + /// + public DateHistogramCompositeAggregationSourceDescriptor CalendarInterval(DateMathTime interval) => + Assign(interval, (a, v) => a.CalendarInterval = v); + + /// + public DateHistogramCompositeAggregationSourceDescriptor FixedInterval(Time interval) => + Assign(interval, (a, v) => a.FixedInterval = v); + /// public DateHistogramCompositeAggregationSourceDescriptor TimeZone(string timezone) => Assign(timezone, (a, v) => a.TimeZone = v); diff --git a/tests/Tests/Aggregations/Bucket/Composite/CompositeAggregationUsageTests.cs b/tests/Tests/Aggregations/Bucket/Composite/CompositeAggregationUsageTests.cs index 41154f0aa19..f4d435722d6 100644 --- a/tests/Tests/Aggregations/Bucket/Composite/CompositeAggregationUsageTests.cs +++ b/tests/Tests/Aggregations/Bucket/Composite/CompositeAggregationUsageTests.cs @@ -62,7 +62,7 @@ public CompositeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : date_histogram = new { field = "startedOn", - interval = "month" + calendar_interval = "month" } } }, @@ -118,7 +118,7 @@ public CompositeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ) .DateHistogram("started", d => d .Field(f => f.StartedOn) - .Interval(DateInterval.Month) + .CalendarInterval(DateInterval.Month) ) .Histogram("branch_count", h => h .Field(f => f.RequiredBranches) @@ -151,7 +151,7 @@ public CompositeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : new DateHistogramCompositeAggregationSource("started") { Field = Field(f => f.StartedOn), - Interval = DateInterval.Month + CalendarInterval = DateInterval.Month }, new HistogramCompositeAggregationSource("branch_count") { @@ -344,7 +344,7 @@ protected override void ExpectResponse(ISearchResponse response) } //hide - [SkipVersion("<6.3.0", "Date histogram source only supports format starting from Elasticsearch 6.3.0+")] + [SkipVersion("<7.2.0", "Date histogram source only supports fixed_interval starting from Elasticsearch 7.2.0+")] public class DateFormatCompositeAggregationUsageTests : ProjectsOnlyAggregationUsageTestBase { public DateFormatCompositeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { } @@ -364,7 +364,7 @@ public DateFormatCompositeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage date_histogram = new { field = "startedOn", - interval = "month", + fixed_interval = "30d", format = "yyyy-MM-dd" } } @@ -396,7 +396,7 @@ public DateFormatCompositeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage .Sources(s => s .DateHistogram("started", d => d .Field(f => f.StartedOn) - .Interval(DateInterval.Month) + .FixedInterval("30d") .Format("yyyy-MM-dd") ) ) @@ -418,7 +418,7 @@ public DateFormatCompositeAggregationUsageTests(ReadOnlyCluster i, EndpointUsage new DateHistogramCompositeAggregationSource("started") { Field = Field(f => f.StartedOn), - Interval = DateInterval.Month, + FixedInterval = new Time(30, TimeUnit.Day), Format = "yyyy-MM-dd" }, },