Skip to content

Fix deserialisation of aggs in Translate response #5228

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
Jan 5, 2021
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
10 changes: 9 additions & 1 deletion src/Nest/Aggregations/AggregationContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,15 @@ public class AggregationContainer : IAggregationContainer
{
public IAdjacencyMatrixAggregation AdjacencyMatrix { get; set; }

public AggregationDictionary Aggregations { get; set; }
// This is currently used to support deserializing the response from SQL Translate,
// which forms a response which uses "aggregations", rather than "aggs". Longer term
// it would be preferred to address that in Elasticsearch itself.
[DataMember(Name = "aggregations")]
private AggregationDictionary _aggs;

// ReSharper disable once ConvertToAutoProperty
public AggregationDictionary Aggregations { get => _aggs; set => _aggs = value; }

public IAverageAggregation Average { get; set; }

public IAverageBucketAggregation AverageBucket { get; set; }
Expand Down
9 changes: 8 additions & 1 deletion src/Nest/Search/Search/SearchRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,15 @@ public partial interface ISearchRequest<TInferDocument> : ISearchRequest { }
[DataContract]
public partial class SearchRequest
{
// This is currently used to support deserializing the response from SQL Translate,
// which forms a response which uses "aggregations", rather than "aggs". Longer term
// it would be preferred to address that in Elasticsearch itself.
[DataMember(Name = "aggregations")]
private AggregationDictionary _aggs;

/// <inheritdoc />
public AggregationDictionary Aggregations { get; set; }
// ReSharper disable once ConvertToAutoProperty
public AggregationDictionary Aggregations { get => _aggs; set => _aggs = value; }
/// <inheritdoc />
public IFieldCollapse Collapse { get; set; }
/// <inheritdoc />
Expand Down
54 changes: 54 additions & 0 deletions tests/Tests.Reproduce/GithubIssue5201.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// 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;
using System.Text;
using System.Threading.Tasks;
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;

namespace Tests.Reproduce
{
public class GitHubIssue5201
{
private static readonly byte[] ResponseBytes = Encoding.UTF8.GetBytes(@"{
""size"" : 0,
""_source"" : false,
""stored_fields"" : ""_none_"",
""aggregations"" : {
""groupby"" : {
""composite"" : {
""size"" : 1000,
""sources"" : [
{
""ccf51bfa"" : {
""terms"" : {
""field"" : ""id"",
""missing_bucket"" : true,
""order"" : ""asc""
}
}
}
]
}
}
}
}");

[U] public async Task DeserializeAggregations()
{
var pool = new SingleNodeConnectionPool(new Uri($"http://localhost:9200"));
var settings = new ConnectionSettings(pool, new InMemoryConnection(ResponseBytes));
var client = new ElasticClient(settings);

var translateResponseAsync = await client.Sql.TranslateAsync(t => t.Query("select UDFvarchar1, count(1) from interactions group by UDFvarchar1 order by UDFvarchar1"));
translateResponseAsync.Result.Aggregations.Should().NotBeNull();

var translateResponse = client.Sql.Translate(t => t.Query("select UDFvarchar1, count(1) from interactions group by UDFvarchar1 order by UDFvarchar1"));
translateResponse.Result.Aggregations.Should().NotBeNull();
}
}
}