Skip to content

Commit cfbbe90

Browse files
committed
Geo bounds aggregation support
Closes #822
1 parent 5a23e8c commit cfbbe90

File tree

8 files changed

+146
-3
lines changed

8 files changed

+146
-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

@@ -89,6 +92,7 @@ public class AggregationContainer : IAggregationContainer
8992
private IFilterAggregator _filter;
9093
private IGeoDistanceAggregator _geoDistance;
9194
private IGeoHashAggregator _geoHash;
95+
private IGeoBoundsAggregator _geoBounds;
9296
private IHistogramAggregator _histogram;
9397
private IGlobalAggregator _global;
9498
private IIp4RangeAggregator _ipRange;
@@ -98,6 +102,7 @@ public class AggregationContainer : IAggregationContainer
98102
private IRangeAggregator _range;
99103
private ITermsAggregator _terms;
100104
private ISignificantTermsAggregator _significantTerms;
105+
101106
public IAverageAggregator Average { get; set; }
102107
public IValueCountAggregator ValueCount { get; set; }
103108
public IMaxAggregator Max { get; set; }
@@ -142,6 +147,12 @@ public IGeoHashAggregator GeoHash
142147
set { _geoHash = value; }
143148
}
144149

150+
public IGeoBoundsAggregator GeoBounds
151+
{
152+
get { return _geoBounds; }
153+
set { _geoBounds = value; }
154+
}
155+
145156
public IHistogramAggregator Histogram
146157
{
147158
get { return _histogram; }
@@ -225,7 +236,9 @@ public class AggregationDescriptor<T> : IAggregationContainer
225236
IGeoDistanceAggregator IAggregationContainer.GeoDistance { get; set; }
226237

227238
IGeoHashAggregator IAggregationContainer.GeoHash { get; set; }
228-
239+
240+
IGeoBoundsAggregator IAggregationContainer.GeoBounds { get; set; }
241+
229242
IHistogramAggregator IAggregationContainer.Histogram { get; set; }
230243

231244
IGlobalAggregator IAggregationContainer.Global { get; set; }
@@ -301,6 +314,12 @@ public AggregationDescriptor<T> GeoHash(string name,
301314
return _SetInnerAggregation(name, selector, (a, d) => a.GeoHash = d);
302315
}
303316

317+
public AggregationDescriptor<T> GeoBounds(string name,
318+
Func<GeoBoundsAggregationDescriptor<T>, GeoBoundsAggregationDescriptor<T>> selector)
319+
{
320+
return _SetInnerAggregation(name, selector, (a, d) => a.GeoBounds = d);
321+
}
322+
304323
public AggregationDescriptor<T> Histogram(string name,
305324
Func<HistogramAggregationDescriptor<T>, HistogramAggregationDescriptor<T>> selector)
306325
{
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

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

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" />
@@ -207,6 +208,7 @@
207208
<Compile Include="Domain\Stats\PercolateStats.cs" />
208209
<Compile Include="Domain\Stats\PluginStats.cs" />
209210
<Compile Include="Domain\Stats\SegmentsStats.cs" />
211+
<Compile Include="DSL\Aggregations\GeoBoundsAggregationDescriptor.cs" />
210212
<Compile Include="DSL\CatIndicesDescriptor.cs" />
211213
<Compile Include="DSL\CatShardsDescriptor.cs" />
212214
<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
@@ -119,5 +119,30 @@ public void ValueCount()
119119
termBucket.Should().NotBeNull();
120120
termBucket.Value.Should().HaveValue();
121121
}
122+
123+
[Test]
124+
public void GeoBounds()
125+
{
126+
var results = this.Client.Search<ElasticsearchProject>(s => s
127+
.Size(0)
128+
.Aggregations(a => a
129+
.GeoBounds("viewport", g => g
130+
.Field(p => p.Origin)
131+
.WrapLongitude()
132+
)
133+
)
134+
);
135+
136+
results.IsValid.Should().BeTrue();
137+
var geoBoundsMetric = results.Aggs.GeoBounds("viewport");
138+
geoBoundsMetric.Should().NotBeNull();
139+
geoBoundsMetric.Bounds.Should().NotBeNull();
140+
geoBoundsMetric.Bounds.TopLeft.Should().NotBeNull();
141+
geoBoundsMetric.Bounds.TopLeft.Lat.Should().NotBe(0);
142+
geoBoundsMetric.Bounds.TopLeft.Lon.Should().NotBe(0);
143+
geoBoundsMetric.Bounds.BottomRight.Should().NotBeNull();
144+
geoBoundsMetric.Bounds.BottomRight.Lat.Should().NotBe(0);
145+
geoBoundsMetric.Bounds.BottomRight.Lon.Should().NotBe(0);
146+
}
122147
}
123148
}

0 commit comments

Comments
 (0)