Skip to content

Commit d973534

Browse files
stevejgordongithub-actions[bot]
authored andcommitted
Fix deserialisation of aggs in Translate response (#5228)
The Translate API in elasticsearch returns aggregations using the key "aggregations" rather than "aggs". This results in aggregations not appearing in the SearchRequest instance. This fix adds a field so that aggregations deserialise correctly using UTF8Json.
1 parent daa9b77 commit d973534

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

src/Nest/Aggregations/AggregationContainer.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,15 @@ public class AggregationContainer : IAggregationContainer
295295
{
296296
public IAdjacencyMatrixAggregation AdjacencyMatrix { get; set; }
297297

298-
public AggregationDictionary Aggregations { get; set; }
298+
// This is currently used to support deserializing the response from SQL Translate,
299+
// which forms a response which uses "aggregations", rather than "aggs". Longer term
300+
// it would be preferred to address that in Elasticsearch itself.
301+
[DataMember(Name = "aggregations")]
302+
private AggregationDictionary _aggs;
303+
304+
// ReSharper disable once ConvertToAutoProperty
305+
public AggregationDictionary Aggregations { get => _aggs; set => _aggs = value; }
306+
299307
public IAverageAggregation Average { get; set; }
300308

301309
public IAverageBucketAggregation AverageBucket { get; set; }

src/Nest/Search/Search/SearchRequest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,15 @@ public partial interface ISearchRequest<TInferDocument> : ISearchRequest { }
180180
[DataContract]
181181
public partial class SearchRequest
182182
{
183+
// This is currently used to support deserializing the response from SQL Translate,
184+
// which forms a response which uses "aggregations", rather than "aggs". Longer term
185+
// it would be preferred to address that in Elasticsearch itself.
186+
[DataMember(Name = "aggregations")]
187+
private AggregationDictionary _aggs;
188+
183189
/// <inheritdoc />
184-
public AggregationDictionary Aggregations { get; set; }
190+
// ReSharper disable once ConvertToAutoProperty
191+
public AggregationDictionary Aggregations { get => _aggs; set => _aggs = value; }
185192
/// <inheritdoc />
186193
public IFieldCollapse Collapse { get; set; }
187194
/// <inheritdoc />
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
9+
using Elasticsearch.Net;
10+
using FluentAssertions;
11+
using Nest;
12+
13+
namespace Tests.Reproduce
14+
{
15+
public class GitHubIssue5201
16+
{
17+
private static readonly byte[] ResponseBytes = Encoding.UTF8.GetBytes(@"{
18+
""size"" : 0,
19+
""_source"" : false,
20+
""stored_fields"" : ""_none_"",
21+
""aggregations"" : {
22+
""groupby"" : {
23+
""composite"" : {
24+
""size"" : 1000,
25+
""sources"" : [
26+
{
27+
""ccf51bfa"" : {
28+
""terms"" : {
29+
""field"" : ""id"",
30+
""missing_bucket"" : true,
31+
""order"" : ""asc""
32+
}
33+
}
34+
}
35+
]
36+
}
37+
}
38+
}
39+
}");
40+
41+
[U] public async Task DeserializeAggregations()
42+
{
43+
var pool = new SingleNodeConnectionPool(new Uri($"http://localhost:9200"));
44+
var settings = new ConnectionSettings(pool, new InMemoryConnection(ResponseBytes));
45+
var client = new ElasticClient(settings);
46+
47+
var translateResponseAsync = await client.Sql.TranslateAsync(t => t.Query("select UDFvarchar1, count(1) from interactions group by UDFvarchar1 order by UDFvarchar1"));
48+
translateResponseAsync.Result.Aggregations.Should().NotBeNull();
49+
50+
var translateResponse = client.Sql.Translate(t => t.Query("select UDFvarchar1, count(1) from interactions group by UDFvarchar1 order by UDFvarchar1"));
51+
translateResponse.Result.Aggregations.Should().NotBeNull();
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)