Skip to content

Commit 13c91bd

Browse files
committed
added support for nested aggregation type
1 parent 1297d95 commit 13c91bd

File tree

13 files changed

+56
-19
lines changed

13 files changed

+56
-19
lines changed

src/Nest/DSL/Aggregations/NestedAggregationDescriptor.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using System;
22
using System.Linq.Expressions;
33
using Nest.Resolvers;
4+
using Nest.Resolvers.Converters;
45
using Newtonsoft.Json;
56

67
namespace Nest
78
{
9+
[JsonConverter(typeof(CustomJsonConverter))]
810
public class NestedAggregationDescriptor<T> : BucketAggregationBaseDescriptor<NestedAggregationDescriptor<T>, T>
11+
, ICustomJson
912
where T : class
1013
{
1114
internal class NestedAgg
@@ -14,7 +17,6 @@ internal class NestedAgg
1417
internal PropertyPathMarker _Path;
1518
}
1619

17-
[JsonProperty("nested")]
1820
internal NestedAgg _Nested;
1921

2022

@@ -31,5 +33,10 @@ public NestedAggregationDescriptor<T> Path(Expression<Func<T, object>> path)
3133
this._Nested._Path = path;
3234
return this;
3335
}
36+
37+
object ICustomJson.GetCustomJson()
38+
{
39+
return this._Nested;
40+
}
3441
}
3542
}

src/Nest/Domain/Aggregations/AggregationsHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ public SingleBucket Missing(string key)
7777
return this.TryGet<SingleBucket>(key);
7878
}
7979

80-
public NestedBucket Nested(string key)
80+
public SingleBucket Nested(string key)
8181
{
82-
return this.TryGet<NestedBucket>(key);
82+
return this.TryGet<SingleBucket>(key);
8383
}
8484

8585
public Bucket<KeyItem> Terms(string key)
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
namespace Nest
1+
using Newtonsoft.Json;
2+
3+
namespace Nest
24
{
3-
public class NestedBucket : BucketAggregationBase
5+
public class SingleBucket : BucketAggregationBase
46
{
7+
[JsonProperty("doc_count")]
8+
public long DocCount { get; set; }
59
}
610
}

src/Nest/Domain/Aggregations/SingleBucket.cs

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

src/Nest/Nest.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
<Compile Include="Domain\Aggregations\KeyItem.cs" />
106106
<Compile Include="Domain\Aggregations\NestedBucket.cs" />
107107
<Compile Include="Domain\Aggregations\RangeItem.cs" />
108-
<Compile Include="Domain\Aggregations\SingleBucket.cs" />
109108
<Compile Include="Domain\Aggregations\StatsMetric.cs" />
110109
<Compile Include="Domain\Aggregations\ValueMetric.cs" />
111110
<Compile Include="Domain\Alias\AliasAddDescriptor.cs" />

src/Tests/Nest.Tests.Integration/Aggregations/BucketAggregationTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void Histogram()
3333
.Field(p=>p.IntValues)
3434
.Interval(10)
3535
)
36+
3637
)
3738
);
3839
results.IsValid.Should().BeTrue();

src/Tests/Nest.Tests.Integration/Aggregations/BucketAggregationTests - Copy.cs renamed to src/Tests/Nest.Tests.Integration/Aggregations/NestedBucketAggregationTests.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,24 @@ public void Terms()
1515
var results = this._client.Search<ElasticsearchProject>(s=>s
1616
.Size(0)
1717
.Aggregations(a=>a
18-
.Nested("followers", n=>n
18+
.Nested("contributors", n=>n
19+
.Path(p=>p.Contributors)
1920
.Aggregations(t=>t
20-
.Terms("bucket_agg", m=>m.Field(p=>p.Country))
21+
.Average("avg_age", m=>m
22+
.Field(p=>p.Contributors.First().Age)
23+
)
2124
)
2225
)
2326
)
2427
);
2528
results.IsValid.Should().BeTrue();
26-
var bucket = results.Aggs.Terms("bucket_agg");
27-
bucket.Items.Should().NotBeEmpty();
29+
var bucket = results.Aggs.Nested("contributors");
30+
bucket.DocCount.Should().BeGreaterThan(1);
31+
32+
var averageAge = bucket.Average("avg_age");
33+
averageAge.Should().NotBeNull();
34+
averageAge.Value.Should().HaveValue()
35+
.And.BeGreaterOrEqualTo(18);
2836
}
2937

3038
}

src/Tests/Nest.Tests.Integration/Aggregations/ParseResponseItemsTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ public void GeoDistanceItem()
112112
.Aggregations(a=>a
113113
.GeoDistance("my_geod", dh=>dh
114114
.Field(p=>p.Origin)
115-
//.DistanceType(GeoDistanceType.sloppy_arc)
116115
.Origin(28.0, 28.0)
117116
.Unit(GeoUnit.km)
118117
.Ranges(

src/Tests/Nest.Tests.Integration/Nest.Tests.Integration.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@
6868
<Reference Include="System.Xml" />
6969
</ItemGroup>
7070
<ItemGroup>
71-
<Compile Include="Aggregations\SingleBucketAggregationTests - Copy.cs" />
71+
<Compile Include="Aggregations\NestedBucketAggregationTests.cs" />
7272
<Compile Include="Aggregations\BucketAggregationTests.cs" />
73+
<Compile Include="Aggregations\SingleBucketAggregationTests.cs" />
7374
<Compile Include="Aggregations\StatsAggregationTests.cs" />
7475
<Compile Include="Aggregations\MetricAggregationTests.cs" />
7576
<Compile Include="Aggregations\TermsAggregationTests.cs" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using AutoPoco.Engine;
3+
4+
namespace Nest.Tests.MockData.DataSources
5+
{
6+
public class AgeSource : DatasourceBase<int>
7+
{
8+
private Random mRandom = new Random(1337);
9+
public override int Next(IGenerationSession session)
10+
{
11+
int f = mRandom.Next(18, 99);
12+
return f;
13+
}
14+
15+
}
16+
}

0 commit comments

Comments
 (0)