Skip to content

Commit ce5196f

Browse files
committed
Merge remote-tracking branch 'origin/feature/aggregations-geo-bounds' into develop
Conflicts: src/Nest/DSL/Aggregations/AggregationDescriptor.cs src/Nest/Nest.csproj src/Tests/Nest.Tests.Integration/Aggregations/MetricAggregationTests.cs
2 parents df4c20e + 95e6fa0 commit ce5196f

File tree

8 files changed

+143
-3
lines changed

8 files changed

+143
-3
lines changed

Diff for: src/Nest/DSL/Aggregations/AggregationDescriptor.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public interface IAggregationContainer
3333
[JsonProperty("geohash_grid")]
3434
IGeoHashAggregator GeoHash { get; set; }
3535

36+
[JsonProperty("geo_bounds")]
37+
IGeoBoundsAggregator GeoBounds { get; set; }
38+
3639
[JsonProperty("histogram")]
3740
IHistogramAggregator Histogram { get; set; }
3841

@@ -92,6 +95,7 @@ public class AggregationContainer : IAggregationContainer
9295
private IFilterAggregator _filter;
9396
private IGeoDistanceAggregator _geoDistance;
9497
private IGeoHashAggregator _geoHash;
98+
private IGeoBoundsAggregator _geoBounds;
9599
private IHistogramAggregator _histogram;
96100
private IGlobalAggregator _global;
97101
private IIp4RangeAggregator _ipRange;
@@ -102,6 +106,7 @@ public class AggregationContainer : IAggregationContainer
102106
private ITermsAggregator _terms;
103107
private ISignificantTermsAggregator _significantTerms;
104108
private IPercentileRanksAggregaor _percentileRanks;
109+
105110
public IAverageAggregator Average { get; set; }
106111
public IValueCountAggregator ValueCount { get; set; }
107112
public IMaxAggregator Max { get; set; }
@@ -146,6 +151,12 @@ public IGeoHashAggregator GeoHash
146151
set { _geoHash = value; }
147152
}
148153

154+
public IGeoBoundsAggregator GeoBounds
155+
{
156+
get { return _geoBounds; }
157+
set { _geoBounds = value; }
158+
}
159+
149160
public IHistogramAggregator Histogram
150161
{
151162
get { return _histogram; }
@@ -235,7 +246,9 @@ public class AggregationDescriptor<T> : IAggregationContainer
235246
IGeoDistanceAggregator IAggregationContainer.GeoDistance { get; set; }
236247

237248
IGeoHashAggregator IAggregationContainer.GeoHash { get; set; }
238-
249+
250+
IGeoBoundsAggregator IAggregationContainer.GeoBounds { get; set; }
251+
239252
IHistogramAggregator IAggregationContainer.Histogram { get; set; }
240253

241254
IGlobalAggregator IAggregationContainer.Global { get; set; }
@@ -319,6 +332,12 @@ public AggregationDescriptor<T> GeoHash(string name,
319332
return _SetInnerAggregation(name, selector, (a, d) => a.GeoHash = d);
320333
}
321334

335+
public AggregationDescriptor<T> GeoBounds(string name,
336+
Func<GeoBoundsAggregationDescriptor<T>, GeoBoundsAggregationDescriptor<T>> selector)
337+
{
338+
return _SetInnerAggregation(name, selector, (a, d) => a.GeoBounds = d);
339+
}
340+
322341
public AggregationDescriptor<T> Histogram(string name,
323342
Func<HistogramAggregationDescriptor<T>, HistogramAggregationDescriptor<T>> selector)
324343
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Nest.Resolvers.Converters;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace Nest
9+
{
10+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
11+
[JsonConverter(typeof(ReadAsTypeConverter<GeoBoundsAggregator>))]
12+
public interface IGeoBoundsAggregator : IMetricAggregator
13+
{
14+
[JsonProperty("wrap_longitude")]
15+
bool? WrapLongitude { get; set; }
16+
}
17+
18+
public class GeoBoundsAggregator : MetricAggregator, IGeoBoundsAggregator
19+
{
20+
public bool? WrapLongitude { get; set; }
21+
}
22+
23+
public class GeoBoundsAggregationDescriptor<T>
24+
: MetricAggregationBaseDescriptor<GeoBoundsAggregationDescriptor<T>, T>, IGeoBoundsAggregator
25+
where T : class
26+
{
27+
IGeoBoundsAggregator Self { get { return this; } }
28+
bool? IGeoBoundsAggregator.WrapLongitude { get; set; }
29+
30+
public GeoBoundsAggregationDescriptor<T> WrapLongitude(bool wrapLongitude = true)
31+
{
32+
this.Self.WrapLongitude = wrapLongitude;
33+
return this;
34+
}
35+
}
36+
}

Diff for: src/Nest/Domain/Aggregations/AggregationsHelper.cs

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public ExtendedStatsMetric ExtendedStats(string key)
6565
{
6666
return this.TryGet<ExtendedStatsMetric>(key);
6767
}
68+
69+
public GeoBoundsMetric GeoBounds(string key)
70+
{
71+
return this.TryGet<GeoBoundsMetric>(key);
72+
}
73+
6874
public PercentilesMetric Percentiles(string key)
6975
{
7076
return this.TryGet<PercentilesMetric>(key);

Diff for: src/Nest/Domain/Aggregations/GeoBoundsMetric.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public class GeoBoundsMetric : IMetricAggregation
9+
{
10+
public GeoBoundsMetric()
11+
{
12+
Bounds = new GeoBounds();
13+
}
14+
15+
public GeoBounds Bounds { get; set; }
16+
}
17+
18+
public class GeoBounds
19+
{
20+
public LatLon TopLeft { get; set; }
21+
public LatLon BottomRight { get; set; }
22+
}
23+
}

Diff for: src/Nest/Domain/DSL/LatLon.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
public class LatLon
44
{
5-
public double Lat { get; set; }
6-
public double Lon { get; set; }
5+
public double? Lat { get; set; }
6+
public double? Lon { get; set; }
77
}
88
}

Diff for: src/Nest/Nest.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<Compile Include="Domain\Aggregations\AggregationsHelper.cs" />
107107
<Compile Include="Domain\Aggregations\Bucket.cs" />
108108
<Compile Include="Domain\Aggregations\BucketAggregationBase.cs" />
109+
<Compile Include="Domain\Aggregations\GeoBoundsMetric.cs" />
109110
<Compile Include="Domain\Aggregations\HistogramItem.cs" />
110111
<Compile Include="Domain\Aggregations\ExtendedStatsMetric.cs" />
111112
<Compile Include="Domain\Aggregations\IAggration.cs" />
@@ -208,6 +209,7 @@
208209
<Compile Include="Domain\Stats\PluginStats.cs" />
209210
<Compile Include="Domain\Stats\SegmentsStats.cs" />
210211
<Compile Include="DSL\Aggregations\PercentileRanksAggregationDescriptor.cs" />
212+
<Compile Include="DSL\Aggregations\GeoBoundsAggregationDescriptor.cs" />
211213
<Compile Include="DSL\CatIndicesDescriptor.cs" />
212214
<Compile Include="DSL\CatShardsDescriptor.cs" />
213215
<Compile Include="DSL\CatThreadPoolDescriptor.cs" />

Diff for: src/Nest/Resolvers/Converters/Aggregations/AggregationConverter.cs

+29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Text.RegularExpressions;
66
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
78

89
namespace Nest.Resolvers.Converters.Aggregations
910
{
@@ -56,12 +57,40 @@ private IAggregation ReadAggregation(JsonReader reader, JsonSerializer serialize
5657
return GetStatsAggregation(reader, serializer);
5758
case "doc_count":
5859
return GetSingleBucketAggregation(reader, serializer);
60+
case "bounds":
61+
return GetGeoBoundsMetricAggregation(reader, serializer);
5962
default:
6063
return null;
6164

6265
}
6366
}
6467

68+
private IAggregation GetGeoBoundsMetricAggregation(JsonReader reader, JsonSerializer serializer)
69+
{
70+
reader.Read();
71+
var o = JObject.Load(reader);
72+
if (o == null)
73+
return null;
74+
var geoBoundsMetric = new GeoBoundsMetric();
75+
JToken topLeftToken;
76+
o.TryGetValue("top_left", out topLeftToken);
77+
if (topLeftToken != null)
78+
{
79+
var topLeft = topLeftToken.ToObject<LatLon>();
80+
if (topLeft != null)
81+
geoBoundsMetric.Bounds.TopLeft = topLeft;
82+
}
83+
JToken bottomRightToken;
84+
o.TryGetValue("bottom_right", out bottomRightToken);
85+
if (bottomRightToken != null)
86+
{
87+
var bottomRight = bottomRightToken.ToObject<LatLon>();
88+
if (bottomRight != null)
89+
geoBoundsMetric.Bounds.BottomRight = bottomRight;
90+
}
91+
return geoBoundsMetric;
92+
}
93+
6594
private IAggregation GetPercentilesMetricAggregation(JsonReader reader, JsonSerializer serializer)
6695
{
6796
var metric = new PercentilesMetric();

Diff for: src/Tests/Nest.Tests.Integration/Aggregations/MetricAggregationTests.cs

+25
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,30 @@ public void PercentilesRank()
137137
percentiles.Should().NotBeNull();
138138
percentiles.Items.Count.Should().BeGreaterThan(0);
139139
}
140+
141+
[Test]
142+
public void GeoBounds()
143+
{
144+
var results = this.Client.Search<ElasticsearchProject>(s => s
145+
.Size(0)
146+
.Aggregations(a => a
147+
.GeoBounds("viewport", g => g
148+
.Field(p => p.Origin)
149+
.WrapLongitude()
150+
)
151+
)
152+
);
153+
154+
results.IsValid.Should().BeTrue();
155+
var geoBoundsMetric = results.Aggs.GeoBounds("viewport");
156+
geoBoundsMetric.Should().NotBeNull();
157+
geoBoundsMetric.Bounds.Should().NotBeNull();
158+
geoBoundsMetric.Bounds.TopLeft.Should().NotBeNull();
159+
geoBoundsMetric.Bounds.TopLeft.Lat.Should().NotBe(0);
160+
geoBoundsMetric.Bounds.TopLeft.Lon.Should().NotBe(0);
161+
geoBoundsMetric.Bounds.BottomRight.Should().NotBeNull();
162+
geoBoundsMetric.Bounds.BottomRight.Lat.Should().NotBe(0);
163+
geoBoundsMetric.Bounds.BottomRight.Lon.Should().NotBe(0);
164+
}
140165
}
141166
}

0 commit comments

Comments
 (0)