Skip to content

Commit d8f5269

Browse files
committed
AutoDateHistogramAggregate Interval should be DateMathTime (#4725)
This commit fixes a bug with AutoDateHistogramAggregate Interval property that uses the Time type to represent valid intervals. The Time type however does not support Months and Years, which are valid units for the AutoDateHistogram interval. Fixes #4333 (cherry picked from commit a693da3)
1 parent de3d07a commit d8f5269

File tree

8 files changed

+116
-6
lines changed

8 files changed

+116
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ response.ShouldBeValid();
109109
110110
var dateHistogram = response.Aggregations.AutoDateHistogram("projects_started_per_month");
111111
dateHistogram.Should().NotBeNull();
112-
dateHistogram.Interval.Should().NotBeNull();
112+
dateHistogram.AutoInterval.Should().NotBeNull();
113113
dateHistogram.Buckets.Should().NotBeNull();
114114
dateHistogram.Buckets.Count.Should().BeGreaterThan(1);
115115
foreach (var item in dateHistogram.Buckets)

docs/aggregations/pipeline/serial-differencing/serial-differencing-aggregation-usage.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ projectsPerMonth.Buckets.Count.Should().BeGreaterThan(0);
9191
9292
var differenceCount = 0;
9393
94-
bool atleastOneSecondDifference = false;
94+
var atleastOneSecondDifference = false;
9595
foreach (var item in projectsPerMonth.Buckets)
9696
{
9797
differenceCount++;

src/Nest/Aggregations/AggregateDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public AutoDateHistogramAggregate AutoDateHistogram(string key)
188188
{
189189
Buckets = bucket.Items.OfType<DateHistogramBucket>().ToList(),
190190
Meta = bucket.Meta,
191-
Interval = bucket.Interval
191+
AutoInterval = bucket.AutoInterval
192192
};
193193
}
194194

src/Nest/Aggregations/AggregateJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ private IAggregate GetMultiBucketAggregate(JsonReader reader, JsonSerializer ser
504504
if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == Parser.Interval)
505505
{
506506
var interval = reader.ReadAsString();
507-
bucket.Interval = new Time(interval);
507+
bucket.AutoInterval = new DateMathTime(interval);
508508
}
509509

510510
return bucket;
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
namespace Nest
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System;
6+
7+
namespace Nest
28
{
39
public class AutoDateHistogramAggregate : MultiBucketAggregate<DateHistogramBucket>
410
{
11+
[Obsolete("Use AutoInterval. This property is incorrectly mapped to the wrong type")]
512
public Time Interval { get; internal set; }
13+
14+
public DateMathTime AutoInterval { get; internal set; }
615
}
716
}

src/Nest/Aggregations/Bucket/BucketAggregate.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
15
using System;
26
using System.Collections.Generic;
37

@@ -72,6 +76,10 @@ public class BucketAggregate : IAggregate
7276
public IReadOnlyCollection<IBucket> Items { get; set; } = EmptyReadOnly<IBucket>.Collection;
7377
public IReadOnlyDictionary<string, object> Meta { get; set; } = EmptyReadOnly<string, object>.Dictionary;
7478
public long? SumOtherDocCount { get; set; }
79+
80+
[Obsolete("Use AutoInterval. This property is incorrectly mapped to the wrong type")]
7581
public Time Interval { get; set; }
82+
83+
public DateMathTime AutoInterval { get; set; }
7684
}
7785
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System;
6+
using System.Text;
7+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
8+
using Elasticsearch.Net;
9+
using FluentAssertions;
10+
using Nest;
11+
12+
namespace Tests.Reproduce
13+
{
14+
public class GitHubIssue4333
15+
{
16+
[U]
17+
public void CanDeserializeYearIntervalUnit()
18+
{
19+
var json = @"{
20+
""took"" : 162,
21+
""timed_out"" : false,
22+
""_shards"" : {
23+
""total"" : 3,
24+
""successful"" : 3,
25+
""skipped"" : 0,
26+
""failed"" : 0
27+
},
28+
""hits"" : {
29+
""total"" : 4089559,
30+
""max_score"" : 0.0,
31+
""hits"" : [ ]
32+
},
33+
""aggregations"" : {
34+
""modDate"" : {
35+
""buckets"" : [
36+
{
37+
""key_as_string"" : ""2013-01-01T00:00:00.000Z"",
38+
""key"" : 1356998400000,
39+
""doc_count"" : 32
40+
},
41+
{
42+
""key_as_string"" : ""2014-01-01T00:00:00.000Z"",
43+
""key"" : 1388534400000,
44+
""doc_count"" : 617
45+
},
46+
{
47+
""key_as_string"" : ""2015-01-01T00:00:00.000Z"",
48+
""key"" : 1420070400000,
49+
""doc_count"" : 183
50+
},
51+
{
52+
""key_as_string"" : ""2016-01-01T00:00:00.000Z"",
53+
""key"" : 1451606400000,
54+
""doc_count"" : 3479
55+
},
56+
{
57+
""key_as_string"" : ""2017-01-01T00:00:00.000Z"",
58+
""key"" : 1483228800000,
59+
""doc_count"" : 1948427
60+
},
61+
{
62+
""key_as_string"" : ""2018-01-01T00:00:00.000Z"",
63+
""key"" : 1514764800000,
64+
""doc_count"" : 555748
65+
},
66+
{
67+
""key_as_string"" : ""2019-01-01T00:00:00.000Z"",
68+
""key"" : 1546300800000,
69+
""doc_count"" : 1268034
70+
},
71+
{
72+
""key_as_string"" : ""2020-01-01T00:00:00.000Z"",
73+
""key"" : 1577836800000,
74+
""doc_count"" : 313039
75+
}
76+
],
77+
""interval"" : ""1y""
78+
}
79+
}
80+
}";
81+
82+
var bytes = Encoding.UTF8.GetBytes(json);
83+
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
84+
var connectionSettings = new ConnectionSettings(pool, new InMemoryConnection(bytes)).DefaultIndex("default_index");
85+
var client = new ElasticClient(connectionSettings);
86+
87+
var response = client.Search<object>();
88+
89+
var agg = response.Aggregations.AutoDateHistogram("modDate");
90+
agg.AutoInterval.Should().Be("1y");
91+
}
92+
}
93+
}

src/Tests/Tests/Aggregations/Bucket/AutoDateHistogram/AutoDateHistogramAggregationUsageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
104104

105105
var dateHistogram = response.Aggregations.AutoDateHistogram("projects_started_per_month");
106106
dateHistogram.Should().NotBeNull();
107-
dateHistogram.Interval.Should().NotBeNull();
107+
dateHistogram.AutoInterval.Should().NotBeNull();
108108
dateHistogram.Buckets.Should().NotBeNull();
109109
dateHistogram.Buckets.Count.Should().BeGreaterThan(1);
110110
foreach (var item in dateHistogram.Buckets)

0 commit comments

Comments
 (0)