Skip to content

Commit 1036308

Browse files
make SignificantTermsBucket generic on key type (#3890)
Supersedes: #3795 Key of SignificantTermsBucket is generic to allow for returning both string and numeric key values. Co-authored-by: kevinphelps <[email protected]>
1 parent cb23c2b commit 1036308

File tree

6 files changed

+78
-12
lines changed

6 files changed

+78
-12
lines changed

src/Nest/Aggregations/AggregateDictionary.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,34 +122,38 @@ public FiltersAggregate Filters(string key)
122122

123123
public GeoCentroidAggregate GeoCentroid(string key) => TryGet<GeoCentroidAggregate>(key);
124124

125-
public SignificantTermsAggregate SignificantTerms(string key)
125+
public SignificantTermsAggregate<TKey> SignificantTerms<TKey>(string key)
126126
{
127127
var bucket = TryGet<BucketAggregate>(key);
128128
return bucket == null
129129
? null
130-
: new SignificantTermsAggregate
130+
: new SignificantTermsAggregate<TKey>
131131
{
132132
BgCount = bucket.BgCount,
133133
DocCount = bucket.DocCount,
134-
Buckets = bucket.Items.OfType<SignificantTermsBucket>().ToList(),
134+
Buckets = GetSignificantTermsBuckets<TKey>(bucket.Items).ToList(),
135135
Meta = bucket.Meta
136136
};
137137
}
138138

139-
public SignificantTermsAggregate SignificantText(string key)
139+
public SignificantTermsAggregate<string> SignificantTerms(string key) => SignificantTerms<string>(key);
140+
141+
public SignificantTermsAggregate<TKey> SignificantText<TKey>(string key)
140142
{
141143
var bucket = TryGet<BucketAggregate>(key);
142144
return bucket == null
143145
? null
144-
: new SignificantTermsAggregate
146+
: new SignificantTermsAggregate<TKey>
145147
{
146148
BgCount = bucket.BgCount,
147149
DocCount = bucket.DocCount,
148-
Buckets = bucket.Items.OfType<SignificantTermsBucket>().ToList(),
150+
Buckets = GetSignificantTermsBuckets<TKey>(bucket.Items).ToList(),
149151
Meta = bucket.Meta
150152
};
151153
}
152154

155+
public SignificantTermsAggregate<string> SignificantText(string key) => SignificantText<string>(key);
156+
153157
public TermsAggregate<TKey> Terms<TKey>(string key)
154158
{
155159
var bucket = TryGet<BucketAggregate>(key);
@@ -255,5 +259,19 @@ private IEnumerable<KeyedBucket<TKey>> GetKeyedBuckets<TKey>(IEnumerable<IBucket
255259
DocCountErrorUpperBound = bucket.DocCountErrorUpperBound
256260
};
257261
}
262+
263+
private IEnumerable<SignificantTermsBucket<TKey>> GetSignificantTermsBuckets<TKey>(IEnumerable<IBucket> items)
264+
{
265+
var buckets = items.Cast<SignificantTermsBucket<object>>();
266+
267+
foreach (var bucket in buckets)
268+
yield return new SignificantTermsBucket<TKey>(bucket.BackingDictionary)
269+
{
270+
Key = (TKey)Convert.ChangeType(bucket.Key, typeof(TKey)),
271+
BgCount = bucket.BgCount,
272+
DocCount = bucket.DocCount,
273+
Score = bucket.Score
274+
};
275+
}
258276
}
259277
}

src/Nest/Aggregations/AggregateFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,9 +864,9 @@ private IBucket GetSignificantTermsBucket(ref JsonReader reader, IJsonFormatterR
864864
subAggregates = GetSubAggregates(ref reader, propertyName, formatterResolver);
865865
}
866866

867-
return new SignificantTermsBucket(subAggregates)
867+
return new SignificantTermsBucket<object>(subAggregates)
868868
{
869-
Key = (string)key,
869+
Key = key,
870870
DocCount = docCount.GetValueOrDefault(0),
871871
BgCount = bgCount,
872872
Score = score

src/Nest/Aggregations/Bucket/SignificantTerms/SignificantTermsAggregate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Nest
22
{
3-
public class SignificantTermsAggregate : MultiBucketAggregate<SignificantTermsBucket>
3+
public class SignificantTermsAggregate<TKey> : MultiBucketAggregate<SignificantTermsBucket<TKey>>
44
{
55
/// <summary>
66
/// The background count

src/Nest/Aggregations/Bucket/SignificantTerms/SignificantTermsBucket.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace Nest
44
{
5-
public class SignificantTermsBucket : BucketBase, IBucket
5+
public class SignificantTermsBucket<TKey> : BucketBase, IBucket
66
{
77
public SignificantTermsBucket(IReadOnlyDictionary<string, IAggregate> dict) : base(dict) { }
88

99
public long BgCount { get; set; }
1010
public long DocCount { get; set; }
1111

12-
public string Key { get; set; }
12+
public TKey Key { get; set; }
1313
public double Score { get; set; }
1414
}
1515
}

src/Tests/Tests.Domain/Project.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class Project
6363
.RuleFor(p => p.LocationPoint, f => SimpleGeoPoint.Generator.Generate())
6464
.RuleFor(p => p.LocationShape, f => new PointGeoShape(new GeoCoordinate(f.Address.Latitude(), f.Address.Latitude())))
6565
.RuleFor(p => p.NumberOfCommits, f => Gimme.Random.Number(1, 1000))
66-
.RuleFor(p => p.NumberOfContributors, f => Gimme.Random.Number(1, 200))
66+
.RuleFor(p => p.NumberOfContributors, f => Gimme.Random.Number(1, 50))
6767
.RuleFor(p => p.Ranges, f => Ranges.Generator.Generate())
6868
.RuleFor(p => p.Branches, f => Gimme.Random.ListItems(new List<string> { "master", "dev", "release", "qa", "test" }))
6969
.RuleFor(p => p.SourceOnly, f =>

src/Tests/Tests/Aggregations/Bucket/SignificantTerms/SignificantTermsAggregationUsageTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,52 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
196196
sigNames.DocCount.Should().BeGreaterThan(0);
197197
}
198198
}
199+
200+
/**
201+
* [float]
202+
* [[significant-terms-numeric-field]]
203+
* == Numeric fields
204+
*
205+
* A significant terms aggregation on a numeric field
206+
*/
207+
public class NumericSignificantTermsAggregationUsageTests : AggregationUsageTestBase
208+
{
209+
public NumericSignificantTermsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
210+
211+
protected override object AggregationJson => new
212+
{
213+
commits = new
214+
{
215+
significant_terms = new
216+
{
217+
field = "numberOfContributors"
218+
}
219+
}
220+
};
221+
222+
protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a
223+
.SignificantTerms("commits", st => st
224+
.Field(p => p.NumberOfContributors)
225+
);
226+
227+
protected override AggregationDictionary InitializerAggs =>
228+
new SignificantTermsAggregation("commits")
229+
{
230+
Field = Field<Project, int>(p => p.NumberOfContributors)
231+
};
232+
233+
protected override void ExpectResponse(ISearchResponse<Project> response)
234+
{
235+
response.ShouldBeValid();
236+
var commits = response.Aggregations.SignificantTerms<int>("commits");
237+
commits.Should().NotBeNull();
238+
commits.Buckets.Should().NotBeNull();
239+
commits.Buckets.Count.Should().BeGreaterThan(0);
240+
foreach (var item in commits.Buckets)
241+
{
242+
item.Key.Should().BeGreaterThan(0);
243+
item.DocCount.Should().BeGreaterOrEqualTo(1);
244+
}
245+
}
246+
}
199247
}

0 commit comments

Comments
 (0)