Skip to content

Commit 78220c6

Browse files
committed
Merge branch 'jws305-master'
1 parent 2f8aaf3 commit 78220c6

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/Nest/Aggregations/AggregateDictionary.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ public AutoDateHistogramAggregate AutoDateHistogram(string key)
217217
};
218218
}
219219

220-
221220
public CompositeBucketAggregate Composite(string key)
222221
{
223222
var bucket = TryGet<BucketAggregate>(key);
@@ -263,15 +262,14 @@ private MultiBucketAggregate<KeyedBucket<TKey>> GetMultiKeyedBucketAggregate<TKe
263262
};
264263
}
265264

266-
267265
private IEnumerable<KeyedBucket<TKey>> GetKeyedBuckets<TKey>(IEnumerable<IBucket> items)
268266
{
269267
var buckets = items.Cast<KeyedBucket<object>>();
270268

271269
foreach (var bucket in buckets)
272270
yield return new KeyedBucket<TKey>(bucket.BackingDictionary)
273271
{
274-
Key = (TKey)Convert.ChangeType(bucket.Key, typeof(TKey)),
272+
Key = GetKeyFromBucketKey<TKey>(bucket.Key),
275273
KeyAsString = bucket.KeyAsString,
276274
DocCount = bucket.DocCount,
277275
DocCountErrorUpperBound = bucket.DocCountErrorUpperBound
@@ -285,7 +283,7 @@ private IEnumerable<SignificantTermsBucket<TKey>> GetSignificantTermsBuckets<TKe
285283
foreach (var bucket in buckets)
286284
yield return new SignificantTermsBucket<TKey>(bucket.BackingDictionary)
287285
{
288-
Key = (TKey)Convert.ChangeType(bucket.Key, typeof(TKey)),
286+
Key = GetKeyFromBucketKey<TKey>(bucket.Key),
289287
BgCount = bucket.BgCount,
290288
DocCount = bucket.DocCount,
291289
Score = bucket.Score
@@ -299,9 +297,14 @@ private IEnumerable<RareTermsBucket<TKey>> GetRareTermsBuckets<TKey>(IEnumerable
299297
foreach (var bucket in buckets)
300298
yield return new RareTermsBucket<TKey>(bucket.BackingDictionary)
301299
{
302-
Key = (TKey)Convert.ChangeType(bucket.Key, typeof(TKey)),
300+
Key = GetKeyFromBucketKey<TKey>(bucket.Key),
303301
DocCount = bucket.DocCount.GetValueOrDefault(0)
304302
};
305303
}
304+
305+
private static TKey GetKeyFromBucketKey<TKey>(object key) =>
306+
typeof(TKey).IsEnum
307+
? (TKey)Enum.Parse(typeof(TKey), key.ToString(), true)
308+
: (TKey)Convert.ChangeType(key, typeof(TKey));
306309
}
307310
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,15 @@ public TermsAggregationIncludePatternUsageTests(ReadOnlyCluster i, EndpointUsage
188188
protected override void ExpectResponse(ISearchResponse<Project> response)
189189
{
190190
response.ShouldBeValid();
191-
var states = response.Aggregations.Terms("states");
191+
var states = response.Aggregations.Terms<StateOfBeing>("states");
192192
states.Should().NotBeNull();
193193
states.DocCountErrorUpperBound.Should().HaveValue();
194194
states.SumOtherDocCount.Should().HaveValue();
195195
states.Buckets.Should().NotBeNull();
196196
states.Buckets.Count.Should().BeGreaterThan(0);
197197
foreach (var item in states.Buckets)
198198
{
199-
item.Key.Should().NotBeNullOrEmpty();
199+
item.Key.Should().BeOfType<StateOfBeing>();
200200
item.DocCount.Should().BeGreaterOrEqualTo(1);
201201
}
202202
states.Meta.Should().NotBeNull().And.HaveCount(1);

0 commit comments

Comments
 (0)