Skip to content

Commit 05616b3

Browse files
committed
Add geo_centroid aggregation
1 parent 0fc5c45 commit 05616b3

File tree

17 files changed

+365
-50
lines changed

17 files changed

+365
-50
lines changed

docs/aggregations-usage.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ include::aggregations/metric/extended-stats/extended-stats-aggregation-usage.asc
4444

4545
include::aggregations/metric/geo-bounds/geo-bounds-aggregation-usage.asciidoc[]
4646

47+
include::aggregations/metric/geo-centroid/geo-centroid-aggregation-usage.asciidoc[]
48+
4749
include::aggregations/metric/max/max-aggregation-usage.asciidoc[]
4850

4951
include::aggregations/metric/min/min-aggregation-usage.asciidoc[]

docs/aggregations.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ exposes all of the available Aggregation types
5050

5151
* <<geo-bounds-aggregation-usage,Geo Bounds Aggregation Usage>>
5252

53+
* <<geo-centroid-aggregation-usage,Geo Centroid Aggregation Usage>>
54+
5355
* <<max-aggregation-usage,Max Aggregation Usage>>
5456

5557
* <<min-aggregation-usage,Min Aggregation Usage>>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/2.3
2+
3+
:github: https://github.com/elastic/elasticsearch-net
4+
5+
:nuget: https://www.nuget.org/packages
6+
7+
[[geo-centroid-aggregation-usage]]
8+
== Geo Centroid Aggregation Usage
9+
10+
A metric aggregation that computes the weighted centroid from all coordinate values
11+
for a Geo-point datatype field.
12+
13+
Be sure to read the Elasticsearch documentation on {ref_current}/search-aggregations-metrics-geocentroid-aggregation.html[Geo Centroid Aggregation]
14+
15+
=== Fluent DSL Example
16+
17+
[source,csharp]
18+
----
19+
s => s
20+
.Aggregations(a => a
21+
.GeoCentroid("centroid", gb => gb
22+
.Field(p => p.Location)
23+
)
24+
)
25+
----
26+
27+
=== Object Initializer Syntax Example
28+
29+
[source,csharp]
30+
----
31+
new SearchRequest<Project>
32+
{
33+
Aggregations = new GeoCentroidAggregation("centroid", Infer.Field<Project>(p => p.Location))
34+
}
35+
----
36+
37+
[source,javascript]
38+
.Example json output
39+
----
40+
{
41+
"aggs": {
42+
"centroid": {
43+
"geo_centroid": {
44+
"field": "location"
45+
}
46+
}
47+
}
48+
}
49+
----
50+
51+
=== Handling Responses
52+
53+
[source,csharp]
54+
----
55+
response.IsValid.Should().BeTrue();
56+
var centroid = response.Aggs.GeoCentroid("centroid");
57+
centroid.Should().NotBeNull();
58+
centroid.Location.Should().NotBeNull();
59+
centroid.Location.Latitude.Should().NotBe(0);
60+
centroid.Location.Longitude.Should().NotBe(0);
61+
----
62+
63+
[[geo-centroid-sub-aggregation]]
64+
65+
[[geo-centroid-sub-aggregation]]
66+
[float]
67+
== Geo Centroid Sub Aggregation
68+
69+
The `geo_centroid` aggregation is more interesting when combined as a sub-aggregation to other bucket aggregations
70+
71+
=== Fluent DSL Example
72+
73+
[source,csharp]
74+
----
75+
s => s
76+
.Aggregations(a => a
77+
.Terms("projects", t => t
78+
.Field(p => p.Name)
79+
.Aggregations(sa => sa
80+
.GeoCentroid("centroid", gb => gb
81+
.Field(p => p.Location)
82+
)
83+
)
84+
)
85+
)
86+
----
87+
88+
=== Object Initializer Syntax Example
89+
90+
[source,csharp]
91+
----
92+
new SearchRequest<Project>
93+
{
94+
Aggregations = new TermsAggregation("projects")
95+
{
96+
Field = Infer.Field<Project>(p => p.Name),
97+
Aggregations = new GeoCentroidAggregation("centroid", Infer.Field<Project>(p => p.Location))
98+
}
99+
}
100+
----
101+
102+
[source,javascript]
103+
.Example json output
104+
----
105+
{
106+
"aggs": {
107+
"projects": {
108+
"terms": {
109+
"field": "name"
110+
},
111+
"aggs": {
112+
"centroid": {
113+
"geo_centroid": {
114+
"field": "location"
115+
}
116+
}
117+
}
118+
}
119+
}
120+
}
121+
----
122+
123+
=== Handling Responses
124+
125+
[source,csharp]
126+
----
127+
response.IsValid.Should().BeTrue();
128+
var projects = response.Aggs.Terms("projects");
129+
130+
foreach (var bucket in projects.Buckets)
131+
{
132+
var centroid = bucket.GeoCentroid("centroid");
133+
centroid.Should().NotBeNull();
134+
centroid.Location.Should().NotBeNull();
135+
136+
centroid.Location.Latitude.Should().NotBe(0);
137+
centroid.Location.Longitude.Should().NotBe(0);
138+
}
139+
----
140+

src/CodeGeneration/CodeGeneration.LowLevelClient/CodeGeneration.LowLevelClient.project.lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"locked": false,
3-
"version": 1,
3+
"version": 2,
44
"targets": {
55
".NETFramework,Version=v4.5": {},
66
".NETFramework,Version=v4.5/win": {}

src/CodeGeneration/Nest.Litterateur/Nest.Litterateur.project.lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"locked": false,
3-
"version": 1,
3+
"version": 2,
44
"targets": {
55
".NETFramework,Version=v4.5": {},
66
".NETFramework,Version=v4.5/win": {}

src/Elasticsearch.Net/Elasticsearch.Net.project.lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"locked": false,
3-
"version": 1,
3+
"version": 2,
44
"targets": {
55
".NETFramework,Version=v4.5": {},
66
".NETFramework,Version=v4.5/win": {}

src/Nest/Aggregations/AggregateJsonConverter.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ private IAggregate ReadAggregate(JsonReader reader, JsonSerializer serializer)
3434

3535
IAggregate aggregate = null;
3636

37-
var property = reader.Value as string;
37+
var property = (string)reader.Value;
3838
if (_numeric.IsMatch(property))
3939
aggregate = GetPercentilesAggregate(reader, serializer, oldFormat: true);
4040

41-
var meta = (property == "meta") ? GetMetadata(reader) : null;
41+
var meta = property == "meta" ? GetMetadata(reader) : null;
4242

4343
if (aggregate != null)
4444
{
4545
aggregate.Meta = meta;
4646
return aggregate;
4747
}
4848

49-
property = reader.Value as string;
49+
property = (string)reader.Value;
5050

5151
switch (property)
5252
{
@@ -74,6 +74,9 @@ private IAggregate ReadAggregate(JsonReader reader, JsonSerializer serializer)
7474
case "hits":
7575
aggregate = GetTopHitsAggregate(reader, serializer);
7676
break;
77+
case "location":
78+
aggregate = GetGeoCentroidAggregate(reader, serializer);
79+
break;
7780
default:
7881
return null;
7982
}
@@ -146,6 +149,14 @@ private IAggregate GetTopHitsAggregate(JsonReader reader, JsonSerializer seriali
146149
return new TopHitsAggregate(hits, serializer) { Total = total, MaxScore = maxScore };
147150
}
148151

152+
private IAggregate GetGeoCentroidAggregate(JsonReader reader, JsonSerializer serializer)
153+
{
154+
reader.Read();
155+
var geoCentroid = new GeoCentroidAggregate { Location = serializer.Deserialize<GeoLocation>(reader) };
156+
reader.Read();
157+
return geoCentroid;
158+
}
159+
149160
private IAggregate GetGeoBoundsAggregate(JsonReader reader, JsonSerializer serializer)
150161
{
151162
reader.Read();
@@ -586,4 +597,4 @@ private IBucket GetFiltersBucket(JsonReader reader, JsonSerializer serializer)
586597
return filtersBucketItem;
587598
}
588599
}
589-
}
600+
}

src/Nest/Aggregations/AggregationContainer.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ public interface IAggregationContainer
183183
[JsonProperty("sampler")]
184184
ISamplerAggregation Sampler { get; set; }
185185

186+
[JsonProperty("geo_centroid")]
187+
IGeoCentroidAggregation GeoCentroid { get; set; }
188+
186189
[JsonProperty("aggs")]
187190
AggregationDictionary Aggregations { get; set; }
188191

@@ -271,6 +274,8 @@ public class AggregationContainer : IAggregationContainer
271274

272275
public ISamplerAggregation Sampler { get; set; }
273276

277+
public IGeoCentroidAggregation GeoCentroid { get; set; }
278+
274279
public AggregationDictionary Aggregations { get; set; }
275280

276281
public static implicit operator AggregationContainer(AggregationBase aggregator)
@@ -384,6 +389,8 @@ public class AggregationContainerDescriptor<T> : DescriptorBase<AggregationConta
384389

385390
ISamplerAggregation IAggregationContainer.Sampler { get; set; }
386391

392+
IGeoCentroidAggregation IAggregationContainer.GeoCentroid { get; set; }
393+
387394
public AggregationContainerDescriptor<T> Average(string name,
388395
Func<AverageAggregationDescriptor<T>, IAverageAggregation> selector) =>
389396
_SetInnerAggregation(name, selector, (a, d) => a.Average = d);
@@ -556,6 +563,10 @@ public AggregationContainerDescriptor<T> Sampler(string name,
556563
Func<SamplerAggregationDescriptor<T>, ISamplerAggregation> selector) =>
557564
_SetInnerAggregation(name, selector, (a, d) => a.Sampler = d);
558565

566+
public AggregationContainerDescriptor<T> GeoCentroid(string name,
567+
Func<GeoCentroidAggregationDescriptor<T>, IGeoCentroidAggregation> selector) =>
568+
_SetInnerAggregation(name, selector, (a, d) => a.GeoCentroid = d);
569+
559570
/// <summary>
560571
/// Fluent methods do not assign to properties on `this` directly but on IAggregationContainers inside `this.Aggregations[string, IContainer]
561572
/// </summary>

src/Nest/Aggregations/AggregationsHelper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public FiltersAggregate Filters(string key)
9595

9696
public SingleBucketAggregate Sampler(string key) => this.TryGet<SingleBucketAggregate>(key);
9797

98+
public GeoCentroidAggregate GeoCentroid(string key) => this.TryGet<GeoCentroidAggregate>(key);
99+
98100
public SignificantTermsAggregate SignificantTerms(string key)
99101
{
100102
var bucket = this.TryGet<BucketAggregate>(key);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Nest
2+
{
3+
public class GeoCentroidAggregate : MetricAggregateBase
4+
{
5+
public GeoLocation Location { get; set; }
6+
}
7+
}

0 commit comments

Comments
 (0)