Skip to content

Commit 72f8ffd

Browse files
[Backport master] Fix deserialisation of aggs in Translate response (#5232)
* 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. * Fix for master namespaces Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent f0b2e5d commit 72f8ffd

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-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
@@ -183,8 +183,15 @@ public partial interface ISearchRequest<TInferDocument> : ISearchRequest { }
183183
[DataContract]
184184
public partial class SearchRequest
185185
{
186+
// This is currently used to support deserializing the response from SQL Translate,
187+
// which forms a response which uses "aggregations", rather than "aggs". Longer term
188+
// it would be preferred to address that in Elasticsearch itself.
189+
[DataMember(Name = "aggregations")]
190+
private AggregationDictionary _aggs;
191+
186192
/// <inheritdoc />
187-
public AggregationDictionary Aggregations { get; set; }
193+
// ReSharper disable once ConvertToAutoProperty
194+
public AggregationDictionary Aggregations { get => _aggs; set => _aggs = value; }
188195
/// <inheritdoc />
189196
public IFieldCollapse Collapse { get; set; }
190197
/// <inheritdoc />
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 Elastic.Transport;
10+
using Elasticsearch.Net;
11+
using FluentAssertions;
12+
using Nest;
13+
14+
namespace Tests.Reproduce
15+
{
16+
public class GitHubIssue5201
17+
{
18+
private static readonly byte[] ResponseBytes = Encoding.UTF8.GetBytes(@"{
19+
""size"" : 0,
20+
""_source"" : false,
21+
""stored_fields"" : ""_none_"",
22+
""aggregations"" : {
23+
""groupby"" : {
24+
""composite"" : {
25+
""size"" : 1000,
26+
""sources"" : [
27+
{
28+
""ccf51bfa"" : {
29+
""terms"" : {
30+
""field"" : ""id"",
31+
""missing_bucket"" : true,
32+
""order"" : ""asc""
33+
}
34+
}
35+
}
36+
]
37+
}
38+
}
39+
}
40+
}");
41+
42+
[U] public async Task DeserializeAggregations()
43+
{
44+
var pool = new SingleNodeConnectionPool(new Uri($"http://localhost:9200"));
45+
var settings = new ConnectionSettings(pool, new InMemoryConnection(ResponseBytes));
46+
var client = new ElasticClient(settings);
47+
48+
var translateResponseAsync = await client.Sql.TranslateAsync(t => t.Query("select UDFvarchar1, count(1) from interactions group by UDFvarchar1 order by UDFvarchar1"));
49+
translateResponseAsync.Result.Aggregations.Should().NotBeNull();
50+
51+
var translateResponse = client.Sql.Translate(t => t.Query("select UDFvarchar1, count(1) from interactions group by UDFvarchar1 order by UDFvarchar1"));
52+
translateResponse.Result.Aggregations.Should().NotBeNull();
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)