Skip to content

[Backport 8.2] Fix GetTerms and sub-aggregations #6630

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 28, 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 @@ -14,24 +14,35 @@ public partial class AggregateDictionary

public bool IsEmptyTerms(string key) => !BackingDictionary.TryGetValue(key, out var agg) || agg is EmptyTermsAggregate;

public bool TryGetStringTerms(string key, out StringTermsAggregate? aggregate)
{
aggregate = null;

if (BackingDictionary.TryGetValue(key, out var agg) && agg is StringTermsAggregate stringTermsAgg)
{
aggregate = stringTermsAgg;
return true;
}

return false;
}

public AvgAggregate? Average(string key) => TryGet<AvgAggregate?>(key);

public TermsAggregate<string> Terms(string key) => Terms<string>(key);

public TermsAggregate<TKey> Terms<TKey>(string key)
// This should be autogenerated if we want to include this.
//public bool TryGetStringTerms(string key, out StringTermsAggregate? aggregate)
//{
// aggregate = null;

// if (BackingDictionary.TryGetValue(key, out var agg) && agg is StringTermsAggregate stringTermsAgg)
// {
// aggregate = stringTermsAgg;
// return true;
// }

// return false;
//}

public AvgAggregate? GetAverage(string key) => TryGet<AvgAggregate?>(key);

/// <summary>
/// WARNING: EXPERIMENTAL API
/// <para>This API provides simplified access to terms aggregations.</para>
/// </summary>
/// <remarks>Experimental APIs are subject to changes or removal and should be used with caution.</remarks>
public TermsAggregate<string> GetTerms(string key) => GetTerms<string>(key);

/// <summary>
/// WARNING: EXPERIMENTAL API
/// <para>This API provides simplified access to terms aggregations.</para>
/// </summary>
/// <remarks>Experimental APIs are subject to changes or removal and should be used with caution.</remarks>
public TermsAggregate<TKey> GetTerms<TKey>(string key)
{
if (!BackingDictionary.TryGetValue(key, out var agg))
{
Expand Down Expand Up @@ -59,12 +70,12 @@ public TermsAggregate<TKey> Terms<TKey>(string key)
{
var key = item.Key;
var value = item.Value;
dict.Add(key, new TermsBucket<TKey> { DocCount = value.DocCount, DocCountError = value.DocCountError, Key = GetKeyFromBucketKey<TKey>(value.Key), KeyAsString = value.Key });
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> { DocCount = b.DocCount, DocCountError = b.DocCountError, Key = GetKeyFromBucketKey<TKey>(b.Key), KeyAsString = b.Key }).ToReadOnlyCollection());
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());
});

return new TermsAggregate<TKey>
Expand All @@ -83,12 +94,12 @@ public TermsAggregate<TKey> Terms<TKey>(string key)
{
var key = item.Key;
var value = item.Value;
dict.Add(key, new TermsBucket<TKey> { DocCount = value.DocCount, DocCountError = value.DocCountError, Key = GetKeyFromBucketKey<TKey>(value.Key), KeyAsString = value.KeyAsString });
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> { DocCount = b.DocCount, DocCountError = b.DocCountError, Key = GetKeyFromBucketKey<TKey>(b.Key), KeyAsString = b.KeyAsString }).ToReadOnlyCollection());
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());
});

return new TermsAggregate<TKey>
Expand All @@ -107,12 +118,12 @@ public TermsAggregate<TKey> Terms<TKey>(string key)
{
var key = item.Key;
var value = item.Value;
dict.Add(key, new TermsBucket<TKey> { DocCount = value.DocCount, DocCountError = value.DocCountError, Key = GetKeyFromBucketKey<TKey>(value.Key), KeyAsString = value.KeyAsString });
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> { DocCount = b.DocCount, DocCountError = b.DocCountError, Key = GetKeyFromBucketKey<TKey>(b.Key), KeyAsString = b.KeyAsString }).ToReadOnlyCollection());
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());
});

return new TermsAggregate<TKey>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Aggregations;

public sealed class TermsBucket<TKey>
public sealed class TermsBucket<TKey> : AggregateDictionary
{
public TermsBucket(IReadOnlyDictionary<string, IAggregate> backingDictionary) : base(backingDictionary)
{
}

public TKey Key { get; init; }

public string? KeyAsString { get; init; }

[JsonInclude]
[JsonPropertyName("doc_count_error")]
public long? DocCountError { get; init; }

[JsonInclude]
[JsonPropertyName("aggregations")]
public Elastic.Clients.Elasticsearch.Aggregations.AggregateDictionary Aggregations { get; init; }

[JsonInclude]
[JsonPropertyName("doc_count")]
public long DocCount { get; init; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public AverageAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
protected override void ExpectResponse(SearchResponse<Project> response)
{
response.ShouldBeValid();
var commitsAvg = response.Aggregations.Average("average_commits");
var commitsAvg = response.Aggregations.GetAverage("average_commits");
commitsAvg.Should().NotBeNull();
commitsAvg.Value.Should().BeGreaterThan(0);
commitsAvg.Meta.Should().NotBeNull().And.HaveCount(1);
Expand Down
2 changes: 1 addition & 1 deletion tests/Tests/AsyncSearch/AsyncSearchApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public async Task AsyncSearchGetResponse() => await Assert<GetAsyncSearchRespons
r.Response.Should().NotBeNull();
r.Response.Took.Should().BeGreaterOrEqualTo(0);
r.Response.Hits.Should().HaveCount(10);
var terms = r.Response.Aggregations.Terms("states");
var terms = r.Response.Aggregations.GetTerms("states");
terms.Should().NotBeNull();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,15 @@ public void CanDeserialize_BasicEmptyTermsAggregate()
}
}

//[U]
//public void TryGetStringTermsAggregate()
//{
// var stream = WrapInStream(@"{""aggregations"":{""terms#my-agg-name"":{""doc_count_error_upper_bound"":10,""sum_other_doc_count"":200,""buckets"":[]}}}");

// var search = _requestResponseSerializer.Deserialize<BasicSearchResponse>(stream);
[U]
public void CanDeserialize_TermsAggregate_WithSubAggregation()
{
var json = @"{""aggregations"":{""terms#my-agg-name"":{""doc_count_error_upper_bound"":0,""sum_other_doc_count"":0,""buckets"":[{""key"":""foo"",""doc_count"":5,""avg#my-sub-agg-name"":{""value"":75.0}}]}}}";

// search.Aggregations.Should().HaveCount(1);
var response = DeserializeJsonString<SearchResponse<object>>(json);

// search.Aggregations.TryGetStringTerms("my-agg-name", out var stringTermsAggregate).Should().BeFalse();
// stringTermsAggregate.Should().BeNull();
//}
var termsAgg = response.Aggregations.GetTerms("my-agg-name");
var avgAgg = termsAgg.Buckets.Item2.Single().GetAverage("my-sub-agg-name");
avgAgg.Value.Should().Be(75.0);
}
}