Skip to content

Commit 54d14ac

Browse files
Fix buckets type for multi-bucket aggregates (#6639) (#6640)
Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent 09398ae commit 54d14ac

20 files changed

+31
-75
lines changed

src/Elastic.Clients.Elasticsearch/Types/Aggregations/AggregateDictionary.cs

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,19 @@ public TermsAggregate<TKey> GetTerms<TKey>(string key)
4949
return null;
5050
}
5151

52-
Buckets<TermsBucket<TKey>> buckets = null;
53-
5452
switch (agg)
5553
{
5654
case EmptyTermsAggregate empty:
5755
return new TermsAggregate<TKey>
5856
{
59-
Buckets = new Buckets<TermsBucket<TKey>>(Array.Empty<TermsBucket<TKey>>()),
57+
Buckets = Array.Empty<TermsBucket<TKey>>().ToReadOnlyCollection(),
6058
Meta = empty.Meta,
6159
DocCountErrorUpperBound = empty.DocCountErrorUpperBound,
6260
SumOtherDocCount = empty.SumOtherDocCount
6361
};
6462

6563
case StringTermsAggregate stringTerms:
66-
stringTerms.Buckets.Match(a =>
67-
{
68-
var dict = new Dictionary<string, TermsBucket<TKey>>();
69-
foreach (var item in a)
70-
{
71-
var key = item.Key;
72-
var value = item.Value;
73-
dict.Add(key, new TermsBucket<TKey>(value.BackingDictionary) { DocCount = value.DocCount, DocCountError = value.DocCountError, Key = GetKeyFromBucketKey<TKey>(value.Key), KeyAsString = value.Key });
74-
}
75-
buckets = new(dict);
76-
}, a =>
77-
{
78-
buckets = new(a.Select(b => new TermsBucket<TKey>(b.BackingDictionary) { DocCount = b.DocCount, DocCountError = b.DocCountError, Key = GetKeyFromBucketKey<TKey>(b.Key), KeyAsString = b.Key }).ToReadOnlyCollection());
79-
});
80-
64+
var buckets = stringTerms.Buckets.Select(b => new TermsBucket<TKey>(b.BackingDictionary) { DocCount = b.DocCount, DocCountError = b.DocCountError, Key = GetKeyFromBucketKey<TKey>(b.Key), KeyAsString = b.Key }).ToReadOnlyCollection();
8165
return new TermsAggregate<TKey>
8266
{
8367
Buckets = buckets,
@@ -87,48 +71,20 @@ public TermsAggregate<TKey> GetTerms<TKey>(string key)
8771
};
8872

8973
case DoubleTermsAggregate doubleTerms:
90-
doubleTerms.Buckets.Match(a =>
91-
{
92-
var dict = new Dictionary<string, TermsBucket<TKey>>();
93-
foreach (var item in a)
94-
{
95-
var key = item.Key;
96-
var value = item.Value;
97-
dict.Add(key, new TermsBucket<TKey>(value.BackingDictionary) { DocCount = value.DocCount, DocCountError = value.DocCountError, Key = GetKeyFromBucketKey<TKey>(value.Key), KeyAsString = value.KeyAsString });
98-
}
99-
buckets = new(dict);
100-
}, a =>
101-
{
102-
buckets = new(a.Select(b => new TermsBucket<TKey>(b.BackingDictionary) { DocCount = b.DocCount, DocCountError = b.DocCountError, Key = GetKeyFromBucketKey<TKey>(b.Key), KeyAsString = b.KeyAsString }).ToReadOnlyCollection());
103-
});
104-
74+
var doubleTermsBuckets = doubleTerms.Buckets.Select(b => new TermsBucket<TKey>(b.BackingDictionary) { DocCount = b.DocCount, DocCountError = b.DocCountError, Key = GetKeyFromBucketKey<TKey>(b.Key), KeyAsString = b.KeyAsString }).ToReadOnlyCollection();
10575
return new TermsAggregate<TKey>
10676
{
107-
Buckets = buckets,
77+
Buckets = doubleTermsBuckets,
10878
Meta = doubleTerms.Meta,
10979
DocCountErrorUpperBound = doubleTerms.DocCountErrorUpperBound,
11080
SumOtherDocCount = doubleTerms.SumOtherDocCount
11181
};
11282

11383
case LongTermsAggregate longTerms:
114-
longTerms.Buckets.Match(a =>
115-
{
116-
var dict = new Dictionary<string, TermsBucket<TKey>>();
117-
foreach (var item in a)
118-
{
119-
var key = item.Key;
120-
var value = item.Value;
121-
dict.Add(key, new TermsBucket<TKey>(value.BackingDictionary) { DocCount = value.DocCount, DocCountError = value.DocCountError, Key = GetKeyFromBucketKey<TKey>(value.Key), KeyAsString = value.KeyAsString });
122-
}
123-
buckets = new(dict);
124-
}, a =>
125-
{
126-
buckets = new(a.Select(b => new TermsBucket<TKey>(b.BackingDictionary) { DocCount = b.DocCount, DocCountError = b.DocCountError, Key = GetKeyFromBucketKey<TKey>(b.Key), KeyAsString = b.KeyAsString }).ToReadOnlyCollection());
127-
});
128-
84+
var longTermsBuckets = longTerms.Buckets.Select(b => new TermsBucket<TKey>(b.BackingDictionary) { DocCount = b.DocCount, DocCountError = b.DocCountError, Key = GetKeyFromBucketKey<TKey>(b.Key), KeyAsString = b.KeyAsString }).ToReadOnlyCollection();
12985
return new TermsAggregate<TKey>
13086
{
131-
Buckets = buckets,
87+
Buckets = longTermsBuckets,
13288
Meta = longTerms.Meta,
13389
DocCountErrorUpperBound = longTerms.DocCountErrorUpperBound,
13490
SumOtherDocCount = longTerms.SumOtherDocCount

src/Elastic.Clients.Elasticsearch/Types/Aggregations/TermsAggregate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class TermsAggregate<TKey> : IAggregate
1919

2020
[JsonInclude]
2121
[JsonPropertyName("buckets")]
22-
public Buckets<TermsBucket<TKey>> Buckets { get; init; }
22+
public IReadOnlyCollection<TermsBucket<TKey>> Buckets { get; init; }
2323

2424
[JsonInclude]
2525
[JsonPropertyName("meta")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/AdjacencyMatrixAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class AdjacencyMatrixAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.AdjacencyMatrixBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.AdjacencyMatrixBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("meta")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/AutoDateHistogramAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class AutoDateHistogramAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.DateHistogramBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.DateHistogramBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("interval")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/CompositeAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public sealed partial class CompositeAggregate : IAggregate
3232

3333
[JsonInclude]
3434
[JsonPropertyName("buckets")]
35-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.CompositeBucket> Buckets { get; init; }
35+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.CompositeBucket> Buckets { get; init; }
3636

3737
[JsonInclude]
3838
[JsonPropertyName("meta")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/DateHistogramAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class DateHistogramAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.DateHistogramBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.DateHistogramBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("meta")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/DateRangeAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class DateRangeAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.RangeBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.RangeBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("meta")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/DoubleTermsAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class DoubleTermsAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.DoubleTermsBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.DoubleTermsBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("doc_count_error_upper_bound")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/FiltersAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class FiltersAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.FiltersBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.FiltersBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("meta")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/HistogramAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class HistogramAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.HistogramBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.HistogramBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("meta")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/IpRangeAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class IpRangeAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.IpRangeBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.IpRangeBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("meta")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/LongTermsAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class LongTermsAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.LongTermsBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.LongTermsBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("doc_count_error_upper_bound")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/MultiTermsAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class MultiTermsAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.MultiTermsBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.MultiTermsBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("doc_count_error_upper_bound")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/RangeAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class RangeAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.RangeBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.RangeBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("meta")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/StringTermsAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class StringTermsAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.StringTermsBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.StringTermsBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("doc_count_error_upper_bound")]

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/VariableWidthHistogramAggregate.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class VariableWidthHistogramAggregate : IAggregate
2828
{
2929
[JsonInclude]
3030
[JsonPropertyName("buckets")]
31-
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.VariableWidthHistogramBucket> Buckets { get; init; }
31+
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.VariableWidthHistogramBucket> Buckets { get; init; }
3232

3333
[JsonInclude]
3434
[JsonPropertyName("meta")]

tests/Tests/Aggregations/Bucket/TermsAggregationUsageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected override void ExpectResponse(SearchResponse<Project> response)
9797
states.SumOtherDocCount.Should().BeGreaterOrEqualTo(0);
9898
states.Buckets.Should().NotBeNull();
9999

100-
var bucketsCollection = states.Buckets.Item2;
100+
var bucketsCollection = states.Buckets;
101101

102102
bucketsCollection.Count.Should().BeGreaterThan(0);
103103
foreach (var item in bucketsCollection)

tests/Tests/Serialization/Aggregations/BucketSubAggregationSerializationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void DeserializeSubAggsForDateHistorgram()
2020

2121
dateHistogramAgg.Should().NotBeNull();
2222

23-
var bucketsCollection = dateHistogramAgg.Buckets.Item2;
23+
var bucketsCollection = dateHistogramAgg.Buckets;
2424
bucketsCollection.Should().HaveCount(1);
2525

2626
var dateBucket = bucketsCollection.First();

tests/Tests/Serialization/Aggregations/ChildrenAggregateSerializationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public void DeserialisesCorrectly()
1616
var searchResponse = DeserializeJsonString<SearchResponse<object>>(json);
1717

1818
var topTagsTermsAggregate = searchResponse.Aggregations.GetTerms("top-tags");
19-
var firstTopTagsBucket = topTagsTermsAggregate.Buckets.Item2.Single();
19+
var firstTopTagsBucket = topTagsTermsAggregate.Buckets.Single();
2020
var childrenAggregate = firstTopTagsBucket.GetChildren("to-answers");
2121
var topNamesAggregate = childrenAggregate.GetTerms("top-names");
22-
var firstTopNameBucket = topNamesAggregate.Buckets.Item2.First();
22+
var firstTopNameBucket = topNamesAggregate.Buckets.First();
2323
firstTopNameBucket.Key.Should().Be("Sam");
2424
}
2525
}

tests/Tests/Serialization/Aggregations/TermsAggregateDeserializationTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void CanDeserialize_BasicStringTermsAggregate()
2424
var termsAgg = search.Aggregations.GetStringTerms("my-agg-name");
2525
termsAgg.DocCountErrorUpperBound.Should().Be(10);
2626
termsAgg.SumOtherDocCount.Should().Be(200);
27-
var bucketCollection = termsAgg.Buckets.Item2;
27+
var bucketCollection = termsAgg.Buckets;
2828
bucketCollection.Should().HaveCount(2);
2929

3030
var firstBucket = bucketCollection.First();
@@ -54,7 +54,7 @@ public void CanDeserialize_BasicLongTermsAggregate()
5454
termsAgg.DocCountErrorUpperBound.Should().Be(10);
5555
termsAgg.SumOtherDocCount.Should().Be(200);
5656

57-
var bucketCollection = termsAgg.Buckets.Item2;
57+
var bucketCollection = termsAgg.Buckets;
5858
bucketCollection.Should().HaveCount(2);
5959

6060
var firstBucket = bucketCollection.First();
@@ -86,7 +86,7 @@ public void CanDeserialize_BasicDoubleTermsAggregate()
8686
termsAgg.DocCountErrorUpperBound.Should().Be(10);
8787
termsAgg.SumOtherDocCount.Should().Be(200);
8888

89-
var bucketCollection = termsAgg.Buckets.Item2;
89+
var bucketCollection = termsAgg.Buckets;
9090
bucketCollection.Should().HaveCount(2);
9191

9292
var firstBucket = bucketCollection.First();
@@ -119,7 +119,7 @@ public void CanDeserialize_StringBased_MultiTermsAggregate()
119119
termsAgg.DocCountErrorUpperBound.Should().Be(10);
120120
termsAgg.SumOtherDocCount.Should().Be(200);
121121

122-
var bucketCollection = termsAgg.Buckets.Item2;
122+
var bucketCollection = termsAgg.Buckets;
123123

124124
var firstBucket = bucketCollection.First();
125125
firstBucket.Key.Should().HaveCount(2);
@@ -154,7 +154,7 @@ public void CanDeserialize_BasicDoubleTerms_AndAccessAsTermsAggregate()
154154
termsAgg.DocCountErrorUpperBound.Should().Be(10);
155155
termsAgg.SumOtherDocCount.Should().Be(200);
156156

157-
var bucketCollection = termsAgg.Buckets.Item2;
157+
var bucketCollection = termsAgg.Buckets;
158158

159159
bucketCollection.Should().HaveCount(2);
160160

@@ -201,7 +201,7 @@ public void CanDeserialize_TermsAggregate_WithSubAggregation()
201201
var response = DeserializeJsonString<SearchResponse<object>>(json);
202202

203203
var termsAgg = response.Aggregations.GetTerms("my-agg-name");
204-
var avgAgg = termsAgg.Buckets.Item2.Single().GetAverage("my-sub-agg-name");
204+
var avgAgg = termsAgg.Buckets.Single().GetAverage("my-sub-agg-name");
205205
avgAgg.Value.Should().Be(75.0);
206206
}
207207
}

0 commit comments

Comments
 (0)