Skip to content

Commit 6a425ad

Browse files
committed
Finish deserialization of aggs meta, simplify AggregationHelper, fix meta on OIS
1 parent aa0c9fc commit 6a425ad

File tree

5 files changed

+49
-66
lines changed

5 files changed

+49
-66
lines changed

src/Nest/Aggregations/AggregationContainer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ public static implicit operator AggregationContainer(AggregationBase aggregator)
265265
aggregator.WrapInContainer(container);
266266
var bucket = aggregator as BucketAggregationBase;
267267
container.Aggregations = bucket?.Aggregations;
268+
container.Meta = aggregator.Meta;
268269
return container;
269270
}
270271

src/Nest/Aggregations/AggregationResultJsonConverter.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
namespace Nest
1010
{
1111
internal class AggregationResultJsonConverter<TReadAs> : ReadAsTypeJsonConverter<TReadAs>
12-
where TReadAs : class
13-
{
14-
15-
}
12+
where TReadAs : class { }
1613

1714
internal class AggregationResultJsonConverter : JsonConverter
1815
{
@@ -44,10 +41,10 @@ private IAggregationResult ReadAggregation(JsonReader reader, JsonSerializer ser
4441
if (_numeric.IsMatch(property))
4542
result = GetPercentilesMetricAggregation(reader, serializer, oldFormat: true);
4643

47-
Dictionary<string, object> meta = null;
48-
if (property == "meta")
49-
meta = GetMetadata(reader);
44+
var meta = (property == "meta") ? GetMetadata(reader) : null;
45+
5046
property = reader.Value as string;
47+
5148
switch (property)
5249
{
5350
case "values":

src/Nest/Aggregations/AggregationsHelper.cs

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ public AggregationsHelper(IDictionary<string, IAggregationResult> aggregations)
1414
this.Aggregations = aggregations;
1515
}
1616

17-
private TAggregation TryGet<TAggregation>(string key)
18-
where TAggregation : class, IAggregationResult
19-
{
20-
IAggregationResult agg;
21-
return this.Aggregations.TryGetValue(key, out agg)
22-
? agg as TAggregation
23-
: null;
24-
}
25-
2617
public ValueMetric Min(string key) => this.TryGet<ValueMetric>(key);
2718

2819
public ValueMetric Max(string key) => this.TryGet<ValueMetric>(key);
@@ -56,7 +47,7 @@ public ScriptedValueMetric ScriptedMetric(string key)
5647
var valueMetric = this.TryGet<ValueMetric>(key);
5748

5849
return valueMetric != null
59-
? new ScriptedValueMetric { _Value = valueMetric.Value }
50+
? new ScriptedValueMetric { _Value = valueMetric.Value, Meta = valueMetric.Meta }
6051
: this.TryGet<ScriptedValueMetric>(key);
6152
}
6253

@@ -79,7 +70,7 @@ public FiltersBucket Filters(string key)
7970
return named;
8071

8172
var anonymous = this.TryGet<Bucket>(key);
82-
return anonymous != null ? new FiltersBucket(anonymous.Items) : null;
73+
return anonymous != null ? new FiltersBucket(anonymous.Items) { Meta = anonymous.Meta } : null;
8374
}
8475

8576
public SingleBucket Global(string key) => this.TryGet<SingleBucket>(key);
@@ -104,17 +95,12 @@ public DocCountBucket<SignificantTermItem> SignificantTerms(string key)
10495
: new DocCountBucket<SignificantTermItem>
10596
{
10697
DocCount = bucket.DocCount,
107-
Items = bucket.Items.OfType<SignificantTermItem>().ToList()
98+
Items = bucket.Items.OfType<SignificantTermItem>().ToList(),
99+
Meta = bucket.Meta
108100
};
109101
}
110102

111-
public Bucket<KeyedBucket> Terms(string key)
112-
{
113-
var bucket = this.TryGet<Bucket>(key);
114-
return bucket == null
115-
? null
116-
: new Bucket<KeyedBucket> {Items = bucket.Items.OfType<KeyedBucket>().ToList()};
117-
}
103+
public Bucket<KeyedBucket> Terms(string key) => GetBucket<KeyedBucket>(key);
118104

119105
public Bucket<HistogramItem> Histogram(string key)
120106
{
@@ -124,7 +110,7 @@ public Bucket<HistogramItem> Histogram(string key)
124110
: new Bucket<HistogramItem>
125111
{
126112
Items = bucket.Items.OfType<HistogramItem>()
127-
.Concat<HistogramItem>(bucket.Items.OfType<KeyedBucket>()
113+
.Concat(bucket.Items.OfType<KeyedBucket>()
128114
.Select(x =>
129115
new HistogramItem
130116
{
@@ -135,56 +121,40 @@ public Bucket<HistogramItem> Histogram(string key)
135121
}
136122
)
137123
)
138-
.ToList()
124+
.ToList(),
125+
Meta = bucket.Meta
139126
};
140127
}
141128

142-
public Bucket<KeyedBucket> GeoHash(string key)
143-
{
144-
var bucket = this.TryGet<Bucket>(key);
145-
return bucket == null
146-
? null
147-
: new Bucket<KeyedBucket> {Items = bucket.Items.OfType<KeyedBucket>().ToList()};
148-
}
129+
public Bucket<KeyedBucket> GeoHash(string key) => GetBucket<KeyedBucket>(key);
149130

150-
public Bucket<RangeItem> Range(string key)
151-
{
152-
var bucket = this.TryGet<Bucket>(key);
153-
return bucket == null
154-
? null
155-
: new Bucket<RangeItem> {Items = bucket.Items.OfType<RangeItem>().ToList()};
156-
}
131+
public Bucket<RangeItem> Range(string key) => GetBucket<RangeItem>(key);
157132

158-
public Bucket<RangeItem> DateRange(string key)
159-
{
160-
var bucket = this.TryGet<Bucket>(key);
161-
return bucket == null
162-
? null
163-
: new Bucket<RangeItem> {Items = bucket.Items.OfType<RangeItem>().ToList()};
164-
}
133+
public Bucket<RangeItem> DateRange(string key) => GetBucket<RangeItem>(key);
165134

166-
public Bucket<RangeItem> IpRange(string key)
167-
{
168-
var bucket = this.TryGet<Bucket>(key);
169-
return bucket == null
170-
? null
171-
: new Bucket<RangeItem> {Items = bucket.Items.OfType<RangeItem>().ToList()};
172-
}
135+
public Bucket<RangeItem> IpRange(string key) => GetBucket<RangeItem>(key);
173136

174-
public Bucket<RangeItem> GeoDistance(string key)
137+
public Bucket<RangeItem> GeoDistance(string key) => GetBucket<RangeItem>(key);
138+
139+
public Bucket<DateHistogramItem> DateHistogram(string key) => GetBucket<DateHistogramItem>(key);
140+
141+
private TAggregation TryGet<TAggregation>(string key)
142+
where TAggregation : class, IAggregationResult
175143
{
176-
var bucket = this.TryGet<Bucket>(key);
177-
return bucket == null
178-
? null
179-
: new Bucket<RangeItem> {Items = bucket.Items.OfType<RangeItem>().ToList()};
144+
IAggregationResult agg;
145+
return this.Aggregations.TryGetValue(key, out agg) ? agg as TAggregation : null;
180146
}
181147

182-
public Bucket<DateHistogramItem> DateHistogram(string key)
148+
private Bucket<TBucketItem> GetBucket<TBucketItem>(string key)
149+
where TBucketItem : IBucketItem
183150
{
184151
var bucket = this.TryGet<Bucket>(key);
185-
return bucket == null
186-
? null
187-
: new Bucket<DateHistogramItem> {Items = bucket.Items.OfType<DateHistogramItem>().ToList()};
152+
if (bucket == null) return null;
153+
return new Bucket<TBucketItem>
154+
{
155+
Items = bucket.Items.OfType<TBucketItem>().ToList(),
156+
Meta = bucket.Meta
157+
};
188158
}
189159
}
190160
}

src/Tests/Aggregations/Bucket/Terms/TermsAggregationUsageTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
9999
item.Key.Should().NotBeNullOrEmpty();
100100
item.DocCount.Should().BeGreaterOrEqualTo(1);
101101
}
102-
states.Meta.Should().NotBeNull();
102+
states.Meta.Should().NotBeNull().And.HaveCount(1);
103+
states.Meta["foo"].Should().Be("bar");
103104
}
104105
}
105106
}

src/Tests/Aggregations/Metric/Average/AverageAggregationUsageTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Tests.Framework.Integration;
55
using Tests.Framework.MockData;
66
using static Nest.Infer;
7+
using System.Collections.Generic;
78

89
namespace Tests.Aggregations.Metric.Average
910
{
@@ -17,6 +18,10 @@ public AverageAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
1718
{
1819
average_commits = new
1920
{
21+
meta = new
22+
{
23+
foo = "bar"
24+
},
2025
avg = new
2126
{
2227
field = Field<Project>(p => p.NumberOfCommits),
@@ -33,6 +38,9 @@ public AverageAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
3338
protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
3439
.Aggregations(a => a
3540
.Average("average_commits", avg => avg
41+
.Meta(m => m
42+
.Add("foo", "bar")
43+
)
3644
.Field(p => p.NumberOfCommits)
3745
.Missing(10)
3846
.Script("_value * 1.2")
@@ -44,6 +52,10 @@ public AverageAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
4452
{
4553
Aggregations = new AverageAggregation("average_commits", Field<Project>(p => p.NumberOfCommits))
4654
{
55+
Meta = new Dictionary<string, object>
56+
{
57+
{ "foo", "bar" }
58+
},
4759
Missing = 10,
4860
Script = new InlineScript("_value * 1.2")
4961
}
@@ -55,6 +67,8 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
5567
var commitsAvg = response.Aggs.Average("average_commits");
5668
commitsAvg.Should().NotBeNull();
5769
commitsAvg.Value.Should().BeGreaterThan(0);
70+
commitsAvg.Meta.Should().NotBeNull().And.HaveCount(1);
71+
commitsAvg.Meta["foo"].Should().Be("bar");
5872
}
5973
}
6074
}

0 commit comments

Comments
 (0)