Skip to content

Commit 5dec33d

Browse files
authored
Assign instance to Aggregations property (#3287)
* Assign instance to Aggregations property This commit fixes a bug with SingleBucketAggregate having an Aggregations property; the property should not be there. Mark as obsolete and assign the instance itself to the property, to at least mitigate null reference exception. Closes #3286
1 parent fdfdb5b commit 5dec33d

File tree

3 files changed

+148
-3
lines changed

3 files changed

+148
-3
lines changed

src/Nest/Aggregations/Bucket/BucketAggregate.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ protected BucketAggregateBase(IReadOnlyDictionary<string, IAggregate> aggregatio
1212

1313
public class SingleBucketAggregate : BucketAggregateBase
1414
{
15-
public SingleBucketAggregate(IReadOnlyDictionary<string, IAggregate> aggregations) : base(aggregations) { }
16-
15+
public SingleBucketAggregate(IReadOnlyDictionary<string, IAggregate> aggregations) : base(aggregations)
16+
{
17+
#pragma warning disable 618
18+
// TODO: Remove in NEST 7.x.
19+
Aggregations = this;
20+
#pragma warning restore 618
21+
}
22+
23+
[Obsolete("Use methods on this instance to access sub aggregations. Will be removed in NEST 7.x")]
1724
public AggregateDictionary Aggregations { get; protected internal set; }
1825

1926
public long DocCount { get; internal set; }
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Elastic.Xunit.XunitPlumbing;
5+
using Elasticsearch.Net;
6+
using FluentAssertions;
7+
using Nest;
8+
using Tests.Framework.ManagedElasticsearch.Clusters;
9+
10+
namespace Tests.Reproduce
11+
{
12+
public class GithubIssue3286 : IClusterFixture<WritableCluster>
13+
{
14+
private readonly WritableCluster _cluster;
15+
16+
public GithubIssue3286(WritableCluster cluster)
17+
{
18+
_cluster = cluster;
19+
}
20+
21+
[I]
22+
public void AggregationsShouldNotBeNullOnNestedAggregation()
23+
{
24+
var client = _cluster.Client;
25+
var index = Guid.NewGuid().ToString("N").Substring(0, 8);
26+
27+
if (client.IndexExists(index).Exists)
28+
client.DeleteIndex(index);
29+
30+
client.CreateIndex(index, c => c
31+
.Mappings(m => m
32+
.Map<MyDocument>(mm => mm
33+
.AutoMap()
34+
.Properties(p => p
35+
.Nested<Rate>(n => n
36+
.AutoMap()
37+
.Name(nn => nn.Rates)
38+
)
39+
)
40+
)
41+
)
42+
);
43+
44+
client.Bulk(b => b
45+
.Index(index)
46+
.IndexMany(new[]
47+
{
48+
new MyDocument
49+
{
50+
Name = "doc 1",
51+
Rates = new[]
52+
{
53+
new Rate
54+
{
55+
Start = new DateTime(2018, 6, 9),
56+
End = new DateTime(2018, 6, 16),
57+
WeeklyRate = 100
58+
},
59+
new Rate
60+
{
61+
Start = new DateTime(2018, 6, 16),
62+
End = new DateTime(2018, 6, 23),
63+
WeeklyRate = 200
64+
}
65+
}
66+
},
67+
new MyDocument
68+
{
69+
Name = "doc 2",
70+
Rates = new[]
71+
{
72+
new Rate
73+
{
74+
Start = new DateTime(2018, 6, 9),
75+
End = new DateTime(2018, 6, 16),
76+
WeeklyRate = 120
77+
},
78+
new Rate
79+
{
80+
Start = new DateTime(2018, 6, 16),
81+
End = new DateTime(2018, 6, 23),
82+
WeeklyRate = 250
83+
}
84+
}
85+
}
86+
})
87+
.Refresh(Refresh.WaitFor)
88+
);
89+
90+
var searchResponse = client.Search<MyDocument>(s => s
91+
.Index(index)
92+
.Size(0)
93+
.Aggregations(a => a
94+
.Nested("nested_start_dates", n => n
95+
.Path(f => f.Rates)
96+
.Aggregations(aa => aa
97+
.DateHistogram("start_dates", dh => dh
98+
.Field(f => f.Rates.First().Start)
99+
.Interval(DateInterval.Day)
100+
.MinimumDocumentCount(1)
101+
.Aggregations(aaa => aaa
102+
.Min("min_rate", m => m
103+
.Field(f => f.Rates.First().WeeklyRate)
104+
)
105+
.Max("max_rate", m => m
106+
.Field(f => f.Rates.First().WeeklyRate)
107+
)
108+
)
109+
)
110+
)
111+
)
112+
)
113+
);
114+
115+
searchResponse.IsValid.Should().BeTrue();
116+
117+
var nested = searchResponse.Aggregations.Nested("nested_start_dates");
118+
119+
#pragma warning disable 618
120+
nested.Aggregations.Should().NotBeNull();
121+
nested.Should().BeSameAs(nested.Aggregations);
122+
#pragma warning restore 618
123+
}
124+
}
125+
126+
public class MyDocument
127+
{
128+
public string Name { get; set; }
129+
public IEnumerable<Rate> Rates { get; set; }
130+
}
131+
132+
public class Rate
133+
{
134+
public DateTime Start { get; set; }
135+
public DateTime End { get; set; }
136+
public double WeeklyRate { get; set; }
137+
}
138+
}

src/Tests/tests.default.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
mode: u
99
# the elasticsearch version that should be started
1010
# Can be a snapshot version of sonatype or "latest" to get the latest snapshot of sonatype
11-
elasticsearch_version: 6.2.3
11+
elasticsearch_version: 6.2.4
1212
# cluster filter allows you to only run the integration tests of a particular cluster (cluster suffix not needed)
1313
# cluster_filter:
1414
# whether we want to forcefully reseed on the node, if you are starting the tests with a node already running

0 commit comments

Comments
 (0)