| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
// GeoCentroidAggregation is a metric aggregation that computes the weighted centroid |
| 8 |
// from all coordinate values for a Geo-point datatype field. |
| 9 |
// See: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-metrics-geocentroid-aggregation.html |
| 10 |
type GeoCentroidAggregation struct { |
| 11 |
field string |
| 12 |
script *Script |
| 13 |
subAggregations map[string]Aggregation |
| 14 |
meta map[string]interface{} |
| 15 |
} |
| 16 |
|
| 17 |
func NewGeoCentroidAggregation() *GeoCentroidAggregation { |
| 18 |
return &GeoCentroidAggregation{ |
| 19 |
subAggregations: make(map[string]Aggregation), |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
func (a *GeoCentroidAggregation) Field(field string) *GeoCentroidAggregation { |
| 24 |
a.field = field |
| 25 |
return a |
| 26 |
} |
| 27 |
|
| 28 |
func (a *GeoCentroidAggregation) Script(script *Script) *GeoCentroidAggregation { |
| 29 |
a.script = script |
| 30 |
return a |
| 31 |
} |
| 32 |
|
| 33 |
func (a *GeoCentroidAggregation) SubAggregation(name string, subAggregation Aggregation) *GeoCentroidAggregation { |
| 34 |
a.subAggregations[name] = subAggregation |
| 35 |
return a |
| 36 |
} |
| 37 |
|
| 38 |
// Meta sets the meta data to be included in the aggregation response. |
| 39 |
func (a *GeoCentroidAggregation) Meta(metaData map[string]interface{}) *GeoCentroidAggregation { |
| 40 |
a.meta = metaData |
| 41 |
return a |
| 42 |
} |
| 43 |
|
| 44 |
func (a *GeoCentroidAggregation) Source() (interface{}, error) { |
| 45 |
// Example: |
| 46 |
// { |
| 47 |
// "query" : { |
| 48 |
// "match" : { "business_type" : "shop" } |
| 49 |
// }, |
| 50 |
// "aggs" : { |
| 51 |
// "centroid" : { |
| 52 |
// "geo_centroid" : { |
| 53 |
// "field" : "location" |
| 54 |
// } |
| 55 |
// } |
| 56 |
// } |
| 57 |
// } |
| 58 |
// |
| 59 |
// This method returns only the { "geo_centroid" : { ... } } part. |
| 60 |
|
| 61 |
source := make(map[string]interface{}) |
| 62 |
opts := make(map[string]interface{}) |
| 63 |
source["geo_centroid"] = opts |
| 64 |
|
| 65 |
if a.field != "" { |
| 66 |
opts["field"] = a.field |
| 67 |
} |
| 68 |
if a.script != nil { |
| 69 |
src, err := a.script.Source() |
| 70 |
if err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
opts["script"] = src |
| 74 |
} |
| 75 |
|
| 76 |
// AggregationBuilder (SubAggregations) |
| 77 |
if len(a.subAggregations) > 0 { |
| 78 |
aggsMap := make(map[string]interface{}) |
| 79 |
source["aggregations"] = aggsMap |
| 80 |
for name, aggregate := range a.subAggregations { |
| 81 |
src, err := aggregate.Source() |
| 82 |
if err != nil { |
| 83 |
return nil, err |
| 84 |
} |
| 85 |
aggsMap[name] = src |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// Add Meta data if available |
| 90 |
if len(a.meta) > 0 { |
| 91 |
source["meta"] = a.meta |
| 92 |
} |
| 93 |
|
| 94 |
return source, nil |
| 95 |
} |