Skip to content

Commit d6dfe45

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 cf1680f commit d6dfe45

File tree

6 files changed

+111
-6
lines changed

6 files changed

+111
-6
lines changed

src/Nest/Aggregations/AggregateDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public AutoDateHistogramAggregate AutoDateHistogram(string key)
221221
{
222222
Buckets = bucket.Items.OfType<DateHistogramBucket>().ToList(),
223223
Meta = bucket.Meta,
224-
Interval = bucket.Interval
224+
AutoInterval = bucket.AutoInterval
225225
};
226226
}
227227

src/Nest/Aggregations/AggregateFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ private IAggregate GetMultiBucketAggregate(ref JsonReader reader, IJsonFormatter
665665
reader.ReadNext();
666666
propertyName = reader.ReadPropertyNameSegmentRaw();
667667
if (propertyName.EqualsBytes(JsonWriter.GetEncodedPropertyNameWithoutQuotation("interval")))
668-
bucket.Interval = formatterResolver.GetFormatter<Time>().Deserialize(ref reader, formatterResolver);
668+
bucket.AutoInterval = formatterResolver.GetFormatter<DateMathTime>().Deserialize(ref reader, formatterResolver);
669669
else
670670
// skip for now
671671
reader.ReadNextBlock();

src/Nest/Aggregations/Bucket/AutoDateHistogram/AutoDateHistogramBucket.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5-
namespace Nest
5+
using System;
6+
7+
namespace Nest
68
{
79
public class AutoDateHistogramAggregate : MultiBucketAggregate<DateHistogramBucket>
810
{
11+
[Obsolete("Use AutoInterval. This property is incorrectly mapped to the wrong type")]
912
public Time Interval { get; internal set; }
13+
14+
public DateMathTime AutoInterval { get; internal set; }
1015
}
1116
}

src/Nest/Aggregations/Bucket/BucketAggregate.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using System;
56
using System.Collections.Generic;
67
using Elasticsearch.Net;
78

@@ -68,6 +69,10 @@ public class BucketAggregate : IAggregate
6869
public IReadOnlyCollection<IBucket> Items { get; set; } = EmptyReadOnly<IBucket>.Collection;
6970
public IReadOnlyDictionary<string, object> Meta { get; set; } = EmptyReadOnly<string, object>.Dictionary;
7071
public long? SumOtherDocCount { get; set; }
72+
73+
[Obsolete("Use AutoInterval. This property is incorrectly mapped to the wrong type")]
7174
public Time Interval { get; set; }
75+
76+
public DateMathTime AutoInterval { get; set; }
7277
}
7378
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
Func<AutoDateHistogramAggregate> func = () => response.Aggregations.AutoDateHistogram("modDate");
90+
91+
var agg = func.Should().NotThrow().Subject;
92+
agg.AutoInterval.Should().Be("1y");
93+
}
94+
}
95+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
107107

108108
var dateHistogram = response.Aggregations.AutoDateHistogram("projects_started_per_month");
109109
dateHistogram.Should().NotBeNull();
110-
dateHistogram.Interval.Should().NotBeNull();
110+
dateHistogram.AutoInterval.Should().NotBeNull();
111111
dateHistogram.Buckets.Should().NotBeNull();
112112
dateHistogram.Buckets.Count.Should().BeGreaterThan(1);
113113
foreach (var item in dateHistogram.Buckets)
@@ -202,13 +202,13 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
202202
/** ==== Handling responses
203203
* The `AggregateDictionary found on `.Aggregations` on `SearchResponse<T>` has several helper methods
204204
* so we can fetch our aggregation results easily in the correct type.
205-
* <<handling-aggregate-response, Be sure to read more about these helper methods>>
205+
* <<handling-aggregate-response, Be sure to read more about these helper methods>>
206206
*/
207207
response.ShouldBeValid();
208208

209209
var dateHistogram = response.Aggregations.AutoDateHistogram("projects_started_per_month");
210210
dateHistogram.Should().NotBeNull();
211-
dateHistogram.Interval.Should().NotBeNull();
211+
dateHistogram.AutoInterval.Should().NotBeNull();
212212
dateHistogram.Buckets.Should().NotBeNull();
213213
dateHistogram.Buckets.Count.Should().BeGreaterThan(1);
214214
foreach (var item in dateHistogram.Buckets)

0 commit comments

Comments
 (0)