Skip to content

Commit f0d1ec2

Browse files
committed
Merge branch 'fix/2336' into 5.x
2 parents 2d60071 + 478ba1e commit f0d1ec2

File tree

10 files changed

+115
-68
lines changed

10 files changed

+115
-68
lines changed

src/Nest/Aggregations/AggregateJsonConverter.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -547,38 +547,35 @@ private IBucket GetDateHistogramBucket(JsonReader reader, JsonSerializer seriali
547547

548548
private IBucket GetKeyedBucket(JsonReader reader, JsonSerializer serializer)
549549
{
550-
var key = reader.ReadAsString();
550+
reader.Read();
551+
var key = reader.Value;
551552
reader.Read();
552553
var property = reader.Value as string;
553554
if (property == "from" || property == "to")
554-
return GetRangeBucket(reader, serializer, key);
555+
return GetRangeBucket(reader, serializer, key as string);
555556

556-
var keyItem = new KeyedBucket { Key = key };
557+
var bucket = new KeyedBucket<object> { Key = key };
557558

558559
if (property == "key_as_string")
559560
{
560-
keyItem.KeyAsString = reader.ReadAsString();
561+
bucket.KeyAsString = reader.ReadAsString();
561562
reader.Read();
562563
}
563564

564565
reader.Read(); //doc_count;
565566
var docCount = reader.Value as long?;
566-
keyItem.DocCount = docCount.GetValueOrDefault(0);
567+
bucket.DocCount = docCount.GetValueOrDefault(0);
567568
reader.Read();
568569

569570
var nextProperty = reader.Value as string;
570571
if (nextProperty == "score")
571-
{
572-
return GetSignificantTermsBucket(reader, serializer, keyItem);
573-
}
574-
575-
576-
keyItem.Aggregations = this.GetSubAggregates(reader, serializer);
577-
return keyItem;
572+
return GetSignificantTermsBucket(reader, serializer, bucket);
578573

574+
bucket.Aggregations = this.GetSubAggregates(reader, serializer);
575+
return bucket;
579576
}
580577

581-
private IBucket GetSignificantTermsBucket(JsonReader reader, JsonSerializer serializer, KeyedBucket keyItem)
578+
private IBucket GetSignificantTermsBucket(JsonReader reader, JsonSerializer serializer, KeyedBucket<object> keyItem)
582579
{
583580
reader.Read();
584581
var score = reader.Value as double?;
@@ -587,7 +584,7 @@ private IBucket GetSignificantTermsBucket(JsonReader reader, JsonSerializer seri
587584
var bgCount = reader.Value as long?;
588585
var significantTermItem = new SignificantTermsBucket()
589586
{
590-
Key = keyItem.Key,
587+
Key = keyItem.Key as string,
591588
DocCount = keyItem.DocCount.GetValueOrDefault(0),
592589
BgCount = bgCount.GetValueOrDefault(0),
593590
Score = score.GetValueOrDefault(0)

src/Nest/Aggregations/AggregationsHelper.cs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Globalization;
34
using System.Linq;
@@ -117,55 +118,35 @@ public SignificantTermsAggregate SignificantTerms(string key)
117118
};
118119
}
119120

120-
public TermsAggregate Terms(string key)
121+
public TermsAggregate<TKey> Terms<TKey>(string key)
121122
{
122123
var bucket = this.TryGet<BucketAggregate>(key);
123124
return bucket == null
124125
? null
125-
: new TermsAggregate
126+
: new TermsAggregate<TKey>
126127
{
127128
DocCountErrorUpperBound = bucket.DocCountErrorUpperBound,
128129
SumOtherDocCount = bucket.SumOtherDocCount,
129-
Buckets = bucket.Items.OfType<KeyedBucket>().ToList(),
130+
Buckets = GetKeyedBuckets<TKey>(bucket.Items).ToList(),
130131
Meta = bucket.Meta
131132
};
132133
}
133134

134-
public MultiBucketAggregate<HistogramBucket> Histogram(string key)
135-
{
136-
var bucket = this.TryGet<BucketAggregate>(key);
137-
return bucket == null
138-
? null
139-
: new MultiBucketAggregate<HistogramBucket>
140-
{
141-
Buckets = bucket.Items.OfType<HistogramBucket>()
142-
.Concat(bucket.Items.OfType<KeyedBucket>()
143-
.Select(x =>
144-
new HistogramBucket
145-
{
146-
Key = double.Parse(x.Key, CultureInfo.InvariantCulture),
147-
KeyAsString = x.Key,
148-
DocCount = x.DocCount.GetValueOrDefault(0),
149-
Aggregations = x.Aggregations
150-
}
151-
)
152-
)
153-
.ToList(),
154-
Meta = bucket.Meta
155-
};
156-
}
135+
public TermsAggregate<string> Terms(string key) => Terms<string>(key);
136+
137+
public MultiBucketAggregate<KeyedBucket<double>> Histogram(string key) => GetMultiKeyedBucketAggregate<double>(key);
157138

158-
public MultiBucketAggregate<KeyedBucket> GeoHash(string key) => GetBucket<KeyedBucket>(key);
139+
public MultiBucketAggregate<KeyedBucket<string>> GeoHash(string key) => GetMultiKeyedBucketAggregate<string>(key);
159140

160-
public MultiBucketAggregate<RangeBucket> Range(string key) => GetBucket<RangeBucket>(key);
141+
public MultiBucketAggregate<RangeBucket> Range(string key) => GetMultiBucketAggregate<RangeBucket>(key);
161142

162-
public MultiBucketAggregate<RangeBucket> DateRange(string key) => GetBucket<RangeBucket>(key);
143+
public MultiBucketAggregate<RangeBucket> DateRange(string key) => GetMultiBucketAggregate<RangeBucket>(key);
163144

164-
public MultiBucketAggregate<RangeBucket> IpRange(string key) => GetBucket<RangeBucket>(key);
145+
public MultiBucketAggregate<RangeBucket> IpRange(string key) => GetMultiBucketAggregate<RangeBucket>(key);
165146

166-
public MultiBucketAggregate<RangeBucket> GeoDistance(string key) => GetBucket<RangeBucket>(key);
147+
public MultiBucketAggregate<RangeBucket> GeoDistance(string key) => GetMultiBucketAggregate<RangeBucket>(key);
167148

168-
public MultiBucketAggregate<DateHistogramBucket> DateHistogram(string key) => GetBucket<DateHistogramBucket>(key);
149+
public MultiBucketAggregate<DateHistogramBucket> DateHistogram(string key) => GetMultiBucketAggregate<DateHistogramBucket>(key);
169150

170151
public MatrixStatsAggregate MatrixStats(string key) => this.TryGet<MatrixStatsAggregate>(key);
171152

@@ -177,16 +158,43 @@ private TAggregate TryGet<TAggregate>(string key)
177158
return this.Aggregations.TryGetValue(key, out agg) ? agg as TAggregate : null;
178159
}
179160

180-
private MultiBucketAggregate<TBucket> GetBucket<TBucket>(string key)
161+
private MultiBucketAggregate<TBucket> GetMultiBucketAggregate<TBucket>(string key)
181162
where TBucket : IBucket
182163
{
183164
var bucket = this.TryGet<BucketAggregate>(key);
184165
if (bucket == null) return null;
185166
return new MultiBucketAggregate<TBucket>
186167
{
187168
Buckets = bucket.Items.OfType<TBucket>().ToList(),
188-
Meta = bucket.Meta
169+
Meta = bucket.Meta,
189170
};
171+
}
172+
private MultiBucketAggregate<KeyedBucket<TKey>> GetMultiKeyedBucketAggregate<TKey>(string key)
173+
{
174+
var bucket = this.TryGet<BucketAggregate>(key);
175+
if (bucket == null) return null;
176+
return new MultiBucketAggregate<KeyedBucket<TKey>>
177+
{
178+
Buckets = GetKeyedBuckets<TKey>(bucket.Items).ToList(),
179+
Meta = bucket.Meta,
180+
};
181+
}
182+
183+
184+
private IEnumerable<KeyedBucket<TKey>> GetKeyedBuckets<TKey>(IEnumerable<IBucket> items)
185+
{
186+
var buckets = items.Cast<KeyedBucket<object>>();
187+
188+
foreach (var bucket in buckets)
189+
{
190+
yield return new KeyedBucket<TKey>
191+
{
192+
Key = (TKey)Convert.ChangeType(bucket.Key, typeof(TKey)),
193+
KeyAsString = bucket.KeyAsString,
194+
Aggregations = bucket.Aggregations,
195+
DocCount = bucket.DocCount
196+
};
197+
}
190198
}
191199
}
192200
}

src/Nest/Aggregations/Bucket/DateHistogram/DateHistogramBucket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Nest
88
{
9-
public class DateHistogramBucket : HistogramBucket
9+
public class DateHistogramBucket : KeyedBucket<double>
1010
{
1111
// Get a DateTime form of the returned key
1212
public DateTime Date => DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddMilliseconds(0 + this.Key), DateTimeKind.Utc);

src/Nest/Aggregations/Bucket/Histogram/HistogramBucket.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Nest/Aggregations/Bucket/KeyedBucket.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
namespace Nest
77
{
8-
public class KeyedBucket : BucketBase
8+
public class KeyedBucket<TKey> : BucketBase
99
{
1010
public KeyedBucket() { }
1111
public KeyedBucket(IDictionary<string, IAggregate> aggregations) : base(aggregations) { }
1212

13-
public string Key { get; set; }
13+
public TKey Key { get; set; }
1414
public string KeyAsString { get; set; }
1515
public long? DocCount { get; set; }
1616
}

src/Nest/Aggregations/Bucket/Terms/TermsAggregate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Nest
77
{
8-
public class TermsAggregate : MultiBucketAggregate<KeyedBucket>
8+
public class TermsAggregate<TKey> : MultiBucketAggregate<KeyedBucket<TKey>>
99
{
1010
public long? DocCountErrorUpperBound { get; set; }
1111
public long? SumOtherDocCount { get; set; }

src/Nest/Nest.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
<Compile Include="Aggregations\Bucket\Global\GlobalAggregation.cs" />
8383
<Compile Include="Aggregations\Bucket\Histogram\ExtendedBounds.cs" />
8484
<Compile Include="Aggregations\Bucket\Histogram\HistogramAggregation.cs" />
85-
<Compile Include="Aggregations\Bucket\Histogram\HistogramBucket.cs" />
8685
<Compile Include="Aggregations\Bucket\Histogram\HistogramOrder.cs" />
8786
<Compile Include="Aggregations\Bucket\IpRange\IpRange.cs" />
8887
<Compile Include="Aggregations\Bucket\IpRange\IpRangeAggregation.cs" />

src/Tests/Aggregations/Bucket/DateHistogram/DateHistogramAggregationUsageTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
128128
dateHistogram.Should().NotBeNull();
129129
dateHistogram.Buckets.Should().NotBeNull();
130130
dateHistogram.Buckets.Count.Should().BeGreaterThan(10);
131+
dateHistogram.Buckets.Should().NotBeNull();
132+
dateHistogram.Buckets.Count.Should().BeGreaterThan(0);
131133
foreach (var item in dateHistogram.Buckets)
132134
{
133135
item.Date.Should().NotBe(default(DateTime));

src/Tests/Aggregations/Bucket/Histogram/HistogramAggregationUsageTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
5959
response.ShouldBeValid();
6060
var commits = response.Aggs.Histogram("commits");
6161
commits.Should().NotBeNull();
62+
commits.Buckets.Should().NotBeNull();
63+
commits.Buckets.Count.Should().BeGreaterThan(0);
6264
foreach (var item in commits.Buckets)
6365
item.DocCount.Should().BeGreaterThan(0);
6466
}

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
9494
states.Should().NotBeNull();
9595
states.DocCountErrorUpperBound.Should().HaveValue();
9696
states.SumOtherDocCount.Should().HaveValue();
97+
states.Buckets.Should().NotBeNull();
98+
states.Buckets.Count.Should().BeGreaterThan(0);
9799
foreach (var item in states.Buckets)
98100
{
99101
item.Key.Should().NotBeNullOrEmpty();
@@ -103,4 +105,56 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
103105
states.Meta["foo"].Should().Be("bar");
104106
}
105107
}
108+
109+
public class NumericTermsAggregationUsageTests : AggregationUsageTestBase
110+
{
111+
public NumericTermsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
112+
113+
protected override object ExpectJson => new
114+
{
115+
aggs = new
116+
{
117+
commits = new
118+
{
119+
120+
terms = new
121+
{
122+
field = "numberOfCommits"
123+
}
124+
}
125+
}
126+
};
127+
128+
protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
129+
.Aggregations(a => a
130+
.Terms("commits", st => st
131+
.Field(p => p.NumberOfCommits)
132+
)
133+
);
134+
135+
protected override SearchRequest<Project> Initializer =>
136+
new SearchRequest<Project>
137+
{
138+
Aggregations = new TermsAggregation("commits")
139+
{
140+
Field = Field<Project>(p => p.NumberOfCommits),
141+
}
142+
};
143+
144+
protected override void ExpectResponse(ISearchResponse<Project> response)
145+
{
146+
response.IsValid.Should().BeTrue();
147+
var commits = response.Aggs.Terms<int>("commits");
148+
commits.Should().NotBeNull();
149+
commits.DocCountErrorUpperBound.Should().HaveValue();
150+
commits.SumOtherDocCount.Should().HaveValue();
151+
commits.Buckets.Should().NotBeNull();
152+
commits.Buckets.Count.Should().BeGreaterThan(0);
153+
foreach (var item in commits.Buckets)
154+
{
155+
item.Key.Should().BeGreaterThan(0);
156+
item.DocCount.Should().BeGreaterOrEqualTo(1);
157+
}
158+
}
159+
}
106160
}

0 commit comments

Comments
 (0)