|
| 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 | + |
0 commit comments