Skip to content

Fix buckets type for multi-bucket aggregates #6639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,19 @@ public TermsAggregate<TKey> GetTerms<TKey>(string key)
return null;
}

Buckets<TermsBucket<TKey>> buckets = null;

switch (agg)
{
case EmptyTermsAggregate empty:
return new TermsAggregate<TKey>
{
Buckets = new Buckets<TermsBucket<TKey>>(Array.Empty<TermsBucket<TKey>>()),
Buckets = Array.Empty<TermsBucket<TKey>>().ToReadOnlyCollection(),
Meta = empty.Meta,
DocCountErrorUpperBound = empty.DocCountErrorUpperBound,
SumOtherDocCount = empty.SumOtherDocCount
};

case StringTermsAggregate stringTerms:
stringTerms.Buckets.Match(a =>
{
var dict = new Dictionary<string, TermsBucket<TKey>>();
foreach (var item in a)
{
var key = item.Key;
var value = item.Value;
dict.Add(key, new TermsBucket<TKey>(value.BackingDictionary) { DocCount = value.DocCount, DocCountError = value.DocCountError, Key = GetKeyFromBucketKey<TKey>(value.Key), KeyAsString = value.Key });
}
buckets = new(dict);
}, a =>
{
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());
});

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();
return new TermsAggregate<TKey>
{
Buckets = buckets,
Expand All @@ -87,48 +71,20 @@ public TermsAggregate<TKey> GetTerms<TKey>(string key)
};

case DoubleTermsAggregate doubleTerms:
doubleTerms.Buckets.Match(a =>
{
var dict = new Dictionary<string, TermsBucket<TKey>>();
foreach (var item in a)
{
var key = item.Key;
var value = item.Value;
dict.Add(key, new TermsBucket<TKey>(value.BackingDictionary) { DocCount = value.DocCount, DocCountError = value.DocCountError, Key = GetKeyFromBucketKey<TKey>(value.Key), KeyAsString = value.KeyAsString });
}
buckets = new(dict);
}, a =>
{
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());
});

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();
return new TermsAggregate<TKey>
{
Buckets = buckets,
Buckets = doubleTermsBuckets,
Meta = doubleTerms.Meta,
DocCountErrorUpperBound = doubleTerms.DocCountErrorUpperBound,
SumOtherDocCount = doubleTerms.SumOtherDocCount
};

case LongTermsAggregate longTerms:
longTerms.Buckets.Match(a =>
{
var dict = new Dictionary<string, TermsBucket<TKey>>();
foreach (var item in a)
{
var key = item.Key;
var value = item.Value;
dict.Add(key, new TermsBucket<TKey>(value.BackingDictionary) { DocCount = value.DocCount, DocCountError = value.DocCountError, Key = GetKeyFromBucketKey<TKey>(value.Key), KeyAsString = value.KeyAsString });
}
buckets = new(dict);
}, a =>
{
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());
});

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();
return new TermsAggregate<TKey>
{
Buckets = buckets,
Buckets = longTermsBuckets,
Meta = longTerms.Meta,
DocCountErrorUpperBound = longTerms.DocCountErrorUpperBound,
SumOtherDocCount = longTerms.SumOtherDocCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class TermsAggregate<TKey> : IAggregate

[JsonInclude]
[JsonPropertyName("buckets")]
public Buckets<TermsBucket<TKey>> Buckets { get; init; }
public IReadOnlyCollection<TermsBucket<TKey>> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("meta")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class AdjacencyMatrixAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.AdjacencyMatrixBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.AdjacencyMatrixBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("meta")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class AutoDateHistogramAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.DateHistogramBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.DateHistogramBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("interval")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed partial class CompositeAggregate : IAggregate

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

[JsonInclude]
[JsonPropertyName("meta")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class DateHistogramAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.DateHistogramBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.DateHistogramBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("meta")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class DateRangeAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.RangeBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.RangeBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("meta")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class DoubleTermsAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.DoubleTermsBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.DoubleTermsBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("doc_count_error_upper_bound")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class FiltersAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.FiltersBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.FiltersBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("meta")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class HistogramAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.HistogramBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.HistogramBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("meta")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class IpRangeAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.IpRangeBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.IpRangeBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("meta")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class LongTermsAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.LongTermsBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.LongTermsBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("doc_count_error_upper_bound")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class MultiTermsAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.MultiTermsBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.MultiTermsBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("doc_count_error_upper_bound")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class RangeAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.RangeBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.RangeBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("meta")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class StringTermsAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.StringTermsBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.StringTermsBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("doc_count_error_upper_bound")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class VariableWidthHistogramAggregate : IAggregate
{
[JsonInclude]
[JsonPropertyName("buckets")]
public Elastic.Clients.Elasticsearch.Aggregations.Buckets<Elastic.Clients.Elasticsearch.Aggregations.VariableWidthHistogramBucket> Buckets { get; init; }
public IReadOnlyCollection<Elastic.Clients.Elasticsearch.Aggregations.VariableWidthHistogramBucket> Buckets { get; init; }

[JsonInclude]
[JsonPropertyName("meta")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected override void ExpectResponse(SearchResponse<Project> response)
states.SumOtherDocCount.Should().BeGreaterOrEqualTo(0);
states.Buckets.Should().NotBeNull();

var bucketsCollection = states.Buckets.Item2;
var bucketsCollection = states.Buckets;

bucketsCollection.Count.Should().BeGreaterThan(0);
foreach (var item in bucketsCollection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void DeserializeSubAggsForDateHistorgram()

dateHistogramAgg.Should().NotBeNull();

var bucketsCollection = dateHistogramAgg.Buckets.Item2;
var bucketsCollection = dateHistogramAgg.Buckets;
bucketsCollection.Should().HaveCount(1);

var dateBucket = bucketsCollection.First();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public void DeserialisesCorrectly()
var searchResponse = DeserializeJsonString<SearchResponse<object>>(json);

var topTagsTermsAggregate = searchResponse.Aggregations.GetTerms("top-tags");
var firstTopTagsBucket = topTagsTermsAggregate.Buckets.Item2.Single();
var firstTopTagsBucket = topTagsTermsAggregate.Buckets.Single();
var childrenAggregate = firstTopTagsBucket.GetChildren("to-answers");
var topNamesAggregate = childrenAggregate.GetTerms("top-names");
var firstTopNameBucket = topNamesAggregate.Buckets.Item2.First();
var firstTopNameBucket = topNamesAggregate.Buckets.First();
firstTopNameBucket.Key.Should().Be("Sam");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void CanDeserialize_BasicStringTermsAggregate()
var termsAgg = search.Aggregations.GetStringTerms("my-agg-name");
termsAgg.DocCountErrorUpperBound.Should().Be(10);
termsAgg.SumOtherDocCount.Should().Be(200);
var bucketCollection = termsAgg.Buckets.Item2;
var bucketCollection = termsAgg.Buckets;
bucketCollection.Should().HaveCount(2);

var firstBucket = bucketCollection.First();
Expand Down Expand Up @@ -54,7 +54,7 @@ public void CanDeserialize_BasicLongTermsAggregate()
termsAgg.DocCountErrorUpperBound.Should().Be(10);
termsAgg.SumOtherDocCount.Should().Be(200);

var bucketCollection = termsAgg.Buckets.Item2;
var bucketCollection = termsAgg.Buckets;
bucketCollection.Should().HaveCount(2);

var firstBucket = bucketCollection.First();
Expand Down Expand Up @@ -86,7 +86,7 @@ public void CanDeserialize_BasicDoubleTermsAggregate()
termsAgg.DocCountErrorUpperBound.Should().Be(10);
termsAgg.SumOtherDocCount.Should().Be(200);

var bucketCollection = termsAgg.Buckets.Item2;
var bucketCollection = termsAgg.Buckets;
bucketCollection.Should().HaveCount(2);

var firstBucket = bucketCollection.First();
Expand Down Expand Up @@ -119,7 +119,7 @@ public void CanDeserialize_StringBased_MultiTermsAggregate()
termsAgg.DocCountErrorUpperBound.Should().Be(10);
termsAgg.SumOtherDocCount.Should().Be(200);

var bucketCollection = termsAgg.Buckets.Item2;
var bucketCollection = termsAgg.Buckets;

var firstBucket = bucketCollection.First();
firstBucket.Key.Should().HaveCount(2);
Expand Down Expand Up @@ -154,7 +154,7 @@ public void CanDeserialize_BasicDoubleTerms_AndAccessAsTermsAggregate()
termsAgg.DocCountErrorUpperBound.Should().Be(10);
termsAgg.SumOtherDocCount.Should().Be(200);

var bucketCollection = termsAgg.Buckets.Item2;
var bucketCollection = termsAgg.Buckets;

bucketCollection.Should().HaveCount(2);

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

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