Skip to content

Commit 3bc8754

Browse files
committed
Merge branch 'jws305-master'
1 parent e7b80df commit 3bc8754

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/Nest/Aggregations/AggregateDictionary.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,7 @@ public CompositeBucketAggregate Composite(string key)
209209

210210
public ValueAggregate MedianAbsoluteDeviation(string key) => TryGet<ValueAggregate>(key);
211211

212-
private TAggregate TryGet<TAggregate>(string key)
213-
where TAggregate : class, IAggregate =>
212+
private TAggregate TryGet<TAggregate>(string key) where TAggregate : class, IAggregate =>
214213
BackingDictionary.TryGetValue(key, out var agg) ? agg as TAggregate : null;
215214

216215
private MultiBucketAggregate<TBucket> GetMultiBucketAggregate<TBucket>(string key)
@@ -238,19 +237,23 @@ private MultiBucketAggregate<KeyedBucket<TKey>> GetMultiKeyedBucketAggregate<TKe
238237
};
239238
}
240239

241-
242240
private IEnumerable<KeyedBucket<TKey>> GetKeyedBuckets<TKey>(IEnumerable<IBucket> items)
243241
{
244242
var buckets = items.Cast<KeyedBucket<object>>();
245243

246244
foreach (var bucket in buckets)
247245
yield return new KeyedBucket<TKey>(bucket.BackingDictionary)
248246
{
249-
Key = (TKey)Convert.ChangeType(bucket.Key, typeof(TKey)),
247+
Key = GetKeyFromBucketKey<TKey>(bucket.Key),
250248
KeyAsString = bucket.KeyAsString,
251249
DocCount = bucket.DocCount,
252250
DocCountErrorUpperBound = bucket.DocCountErrorUpperBound
253251
};
254252
}
253+
254+
private static TKey GetKeyFromBucketKey<TKey>(object key) =>
255+
typeof(TKey).IsEnum
256+
? (TKey)Enum.Parse(typeof(TKey), key.ToString(), true)
257+
: (TKey)Convert.ChangeType(key, typeof(TKey));
255258
}
256259
}

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)