Skip to content

Commit a693da3

Browse files
authored
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
1 parent e007934 commit a693da3

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

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.Interval = 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
{
77
public class AutoDateHistogramAggregate : MultiBucketAggregate<DateHistogramBucket>
88
{
9-
public Time Interval { get; internal set; }
9+
public DateMathTime Interval { get; internal set; }
1010
}
1111
}

src/Nest/Aggregations/Bucket/BucketAggregate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ public class BucketAggregate : IAggregate
6868
public IReadOnlyCollection<IBucket> Items { get; set; } = EmptyReadOnly<IBucket>.Collection;
6969
public IReadOnlyDictionary<string, object> Meta { get; set; } = EmptyReadOnly<string, object>.Dictionary;
7070
public long? SumOtherDocCount { get; set; }
71-
public Time Interval { get; set; }
71+
public DateMathTime Interval { get; set; }
7272
}
7373
}
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.Interval.Should().Be("1y");
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)