Skip to content

Commit 640248b

Browse files
Fix deserialisation of aggs in Translate response (#5228) (#5231)
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. Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent ff60075 commit 640248b

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
@@ -294,7 +294,15 @@ public class AggregationContainer : IAggregationContainer
294294
{
295295
public IAdjacencyMatrixAggregation AdjacencyMatrix { get; set; }
296296

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

300308
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
@@ -179,8 +179,15 @@ public partial interface ISearchRequest<TInferDocument> : ISearchRequest { }
179179
[DataContract]
180180
public partial class SearchRequest
181181
{
182+
// This is currently used to support deserializing the response from SQL Translate,
183+
// which forms a response which uses "aggregations", rather than "aggs". Longer term
184+
// it would be preferred to address that in Elasticsearch itself.
185+
[DataMember(Name = "aggregations")]
186+
private AggregationDictionary _aggs;
187+
182188
/// <inheritdoc />
183-
public AggregationDictionary Aggregations { get; set; }
189+
// ReSharper disable once ConvertToAutoProperty
190+
public AggregationDictionary Aggregations { get => _aggs; set => _aggs = value; }
184191
/// <inheritdoc />
185192
public IFieldCollapse Collapse { get; set; }
186193
/// <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)