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")]

0 commit comments

Comments
 (0)