Skip to content

Commit 6ad1e04

Browse files
committed
Deserialize aggregation metadata to Dictionary<string,object> (#3073)
This commit uses the JsonSerializer to deserialize the aggregation metadata to a Dictionary<string,object> from the JsonReader. Add integration test to assert behaviour. Closes #3072 (cherry picked from commit 188d2d7)
1 parent 83c009c commit 6ad1e04

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

src/Nest/Aggregations/AggregateJsonConverter.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private IAggregate ReadAggregate(JsonReader reader, JsonSerializer serializer)
108108
aggregate = GetPercentilesAggregate(reader, serializer, oldFormat: true);
109109

110110
var meta = propertyName == Parser.Meta
111-
? GetMetadata(reader)
111+
? GetMetadata(serializer, reader)
112112
: null;
113113

114114
if (aggregate != null)
@@ -189,19 +189,12 @@ private IBucket ReadBucket(JsonReader reader, JsonSerializer serializer)
189189
return item;
190190
}
191191

192-
private Dictionary<string, object> GetMetadata(JsonReader reader)
192+
private Dictionary<string, object> GetMetadata(JsonSerializer serializer, JsonReader reader)
193193
{
194-
var meta = new Dictionary<string, object>();
194+
// read past "meta" property name to start of object
195195
reader.Read();
196-
reader.Read();
197-
while (reader.TokenType != JsonToken.EndObject)
198-
{
199-
var key = (string) reader.Value;
200-
reader.Read();
201-
var value = reader.Value;
202-
meta.Add(key, value);
203-
reader.Read();
204-
}
196+
var meta = serializer.Deserialize<Dictionary<string, object>>(reader);
197+
// read past the closing end object of "meta" object
205198
reader.Read();
206199
return meta;
207200
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using FluentAssertions;
4+
using Nest;
5+
using Tests.Framework;
6+
using Tests.Framework.Integration;
7+
using Tests.Framework.ManagedElasticsearch.Clusters;
8+
using Tests.Framework.MockData;
9+
10+
namespace Tests.Aggregations
11+
{
12+
/**
13+
*=== Aggregation Metadata
14+
* Metadata can be provided per aggregation, and will be returned in the aggregation response
15+
*/
16+
public class AggregationMetaUsageTests : AggregationUsageTestBase
17+
{
18+
public AggregationMetaUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
19+
20+
protected override object AggregationJson => new
21+
{
22+
min_last_activity = new
23+
{
24+
min = new
25+
{
26+
field = "lastActivity"
27+
},
28+
meta = new
29+
{
30+
meta_1 = "value_1",
31+
meta_2 = 2,
32+
meta_3 = new
33+
{
34+
meta_3 = "value_3"
35+
}
36+
}
37+
}
38+
};
39+
40+
protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a
41+
.Min("min_last_activity", m => m
42+
.Field(p => p.LastActivity)
43+
.Meta(d => d
44+
.Add("meta_1", "value_1")
45+
.Add("meta_2", 2)
46+
.Add("meta_3", new { meta_3 = "value_3" })
47+
)
48+
);
49+
50+
protected override AggregationDictionary InitializerAggs =>
51+
new MinAggregation("min_last_activity", Infer.Field<Project>(p => p.LastActivity))
52+
{
53+
Meta = new Dictionary<string,object>
54+
{
55+
{ "meta_1", "value_1" },
56+
{ "meta_2", 2 },
57+
{ "meta_3", new { meta_3 = "value_3" } }
58+
}
59+
};
60+
61+
protected override void ExpectResponse(ISearchResponse<Project> response)
62+
{
63+
response.ShouldBeValid();
64+
var min = response.Aggregations.Min("min_last_activity");
65+
min.Meta.Should().NotBeNull().And.ContainKeys("meta_1", "meta_2", "meta_3");
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)