| 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 |
// GeoDistanceAggregation is a multi-bucket aggregation that works on geo_point fields |
| 8 |
// and conceptually works very similar to the range aggregation. |
| 9 |
// The user can define a point of origin and a set of distance range buckets. |
| 10 |
// The aggregation evaluate the distance of each document value from |
| 11 |
// the origin point and determines the buckets it belongs to based on |
| 12 |
// the ranges (a document belongs to a bucket if the distance between the |
| 13 |
// document and the origin falls within the distance range of the bucket). |
| 14 |
// See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-geodistance-aggregation.html |
| 15 |
type GeoDistanceAggregation struct { |
| 16 |
field string |
| 17 |
unit string |
| 18 |
distanceType string |
| 19 |
point string |
| 20 |
ranges []geoDistAggRange |
| 21 |
subAggregations map[string]Aggregation |
| 22 |
meta map[string]interface{} |
| 23 |
} |
| 24 |
|
| 25 |
type geoDistAggRange struct { |
| 26 |
Key string |
| 27 |
From interface{} |
| 28 |
To interface{} |
| 29 |
} |
| 30 |
|
| 31 |
func NewGeoDistanceAggregation() *GeoDistanceAggregation { |
| 32 |
return &GeoDistanceAggregation{ |
| 33 |
subAggregations: make(map[string]Aggregation), |
| 34 |
ranges: make([]geoDistAggRange, 0), |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func (a *GeoDistanceAggregation) Field(field string) *GeoDistanceAggregation { |
| 39 |
a.field = field |
| 40 |
return a |
| 41 |
} |
| 42 |
|
| 43 |
func (a *GeoDistanceAggregation) Unit(unit string) *GeoDistanceAggregation { |
| 44 |
a.unit = unit |
| 45 |
return a |
| 46 |
} |
| 47 |
|
| 48 |
func (a *GeoDistanceAggregation) DistanceType(distanceType string) *GeoDistanceAggregation { |
| 49 |
a.distanceType = distanceType |
| 50 |
return a |
| 51 |
} |
| 52 |
|
| 53 |
func (a *GeoDistanceAggregation) Point(latLon string) *GeoDistanceAggregation { |
| 54 |
a.point = latLon |
| 55 |
return a |
| 56 |
} |
| 57 |
|
| 58 |
func (a *GeoDistanceAggregation) SubAggregation(name string, subAggregation Aggregation) *GeoDistanceAggregation { |
| 59 |
a.subAggregations[name] = subAggregation |
| 60 |
return a |
| 61 |
} |
| 62 |
|
| 63 |
// Meta sets the meta data to be included in the aggregation response. |
| 64 |
func (a *GeoDistanceAggregation) Meta(metaData map[string]interface{}) *GeoDistanceAggregation { |
| 65 |
a.meta = metaData |
| 66 |
return a |
| 67 |
} |
| 68 |
func (a *GeoDistanceAggregation) AddRange(from, to interface{}) *GeoDistanceAggregation { |
| 69 |
a.ranges = append(a.ranges, geoDistAggRange{From: from, To: to}) |
| 70 |
return a |
| 71 |
} |
| 72 |
|
| 73 |
func (a *GeoDistanceAggregation) AddRangeWithKey(key string, from, to interface{}) *GeoDistanceAggregation { |
| 74 |
a.ranges = append(a.ranges, geoDistAggRange{Key: key, From: from, To: to}) |
| 75 |
return a |
| 76 |
} |
| 77 |
|
| 78 |
func (a *GeoDistanceAggregation) AddUnboundedTo(from float64) *GeoDistanceAggregation { |
| 79 |
a.ranges = append(a.ranges, geoDistAggRange{From: from, To: nil}) |
| 80 |
return a |
| 81 |
} |
| 82 |
|
| 83 |
func (a *GeoDistanceAggregation) AddUnboundedToWithKey(key string, from float64) *GeoDistanceAggregation { |
| 84 |
a.ranges = append(a.ranges, geoDistAggRange{Key: key, From: from, To: nil}) |
| 85 |
return a |
| 86 |
} |
| 87 |
|
| 88 |
func (a *GeoDistanceAggregation) AddUnboundedFrom(to float64) *GeoDistanceAggregation { |
| 89 |
a.ranges = append(a.ranges, geoDistAggRange{From: nil, To: to}) |
| 90 |
return a |
| 91 |
} |
| 92 |
|
| 93 |
func (a *GeoDistanceAggregation) AddUnboundedFromWithKey(key string, to float64) *GeoDistanceAggregation { |
| 94 |
a.ranges = append(a.ranges, geoDistAggRange{Key: key, From: nil, To: to}) |
| 95 |
return a |
| 96 |
} |
| 97 |
|
| 98 |
func (a *GeoDistanceAggregation) Between(from, to interface{}) *GeoDistanceAggregation { |
| 99 |
a.ranges = append(a.ranges, geoDistAggRange{From: from, To: to}) |
| 100 |
return a |
| 101 |
} |
| 102 |
|
| 103 |
func (a *GeoDistanceAggregation) BetweenWithKey(key string, from, to interface{}) *GeoDistanceAggregation { |
| 104 |
a.ranges = append(a.ranges, geoDistAggRange{Key: key, From: from, To: to}) |
| 105 |
return a |
| 106 |
} |
| 107 |
|
| 108 |
func (a *GeoDistanceAggregation) Source() (interface{}, error) { |
| 109 |
// Example: |
| 110 |
// { |
| 111 |
// "aggs" : { |
| 112 |
// "rings_around_amsterdam" : { |
| 113 |
// "geo_distance" : { |
| 114 |
// "field" : "location", |
| 115 |
// "origin" : "52.3760, 4.894", |
| 116 |
// "ranges" : [ |
| 117 |
// { "to" : 100 }, |
| 118 |
// { "from" : 100, "to" : 300 }, |
| 119 |
// { "from" : 300 } |
| 120 |
// ] |
| 121 |
// } |
| 122 |
// } |
| 123 |
// } |
| 124 |
// } |
| 125 |
// |
| 126 |
// This method returns only the { "range" : { ... } } part. |
| 127 |
|
| 128 |
source := make(map[string]interface{}) |
| 129 |
opts := make(map[string]interface{}) |
| 130 |
source["geo_distance"] = opts |
| 131 |
|
| 132 |
if a.field != "" { |
| 133 |
opts["field"] = a.field |
| 134 |
} |
| 135 |
if a.unit != "" { |
| 136 |
opts["unit"] = a.unit |
| 137 |
} |
| 138 |
if a.distanceType != "" { |
| 139 |
opts["distance_type"] = a.distanceType |
| 140 |
} |
| 141 |
if a.point != "" { |
| 142 |
opts["origin"] = a.point |
| 143 |
} |
| 144 |
|
| 145 |
var ranges []interface{} |
| 146 |
for _, ent := range a.ranges { |
| 147 |
r := make(map[string]interface{}) |
| 148 |
if ent.Key != "" { |
| 149 |
r["key"] = ent.Key |
| 150 |
} |
| 151 |
if ent.From != nil { |
| 152 |
switch from := ent.From.(type) { |
| 153 |
case int, int16, int32, int64, float32, float64: |
| 154 |
r["from"] = from |
| 155 |
case *int, *int16, *int32, *int64, *float32, *float64: |
| 156 |
r["from"] = from |
| 157 |
case string: |
| 158 |
r["from"] = from |
| 159 |
case *string: |
| 160 |
r["from"] = from |
| 161 |
} |
| 162 |
} |
| 163 |
if ent.To != nil { |
| 164 |
switch to := ent.To.(type) { |
| 165 |
case int, int16, int32, int64, float32, float64: |
| 166 |
r["to"] = to |
| 167 |
case *int, *int16, *int32, *int64, *float32, *float64: |
| 168 |
r["to"] = to |
| 169 |
case string: |
| 170 |
r["to"] = to |
| 171 |
case *string: |
| 172 |
r["to"] = to |
| 173 |
} |
| 174 |
} |
| 175 |
ranges = append(ranges, r) |
| 176 |
} |
| 177 |
opts["ranges"] = ranges |
| 178 |
|
| 179 |
// AggregationBuilder (SubAggregations) |
| 180 |
if len(a.subAggregations) > 0 { |
| 181 |
aggsMap := make(map[string]interface{}) |
| 182 |
source["aggregations"] = aggsMap |
| 183 |
for name, aggregate := range a.subAggregations { |
| 184 |
src, err := aggregate.Source() |
| 185 |
if err != nil { |
| 186 |
return nil, err |
| 187 |
} |
| 188 |
aggsMap[name] = src |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
// Add Meta data if available |
| 193 |
if len(a.meta) > 0 { |
| 194 |
source["meta"] = a.meta |
| 195 |
} |
| 196 |
|
| 197 |
return source, nil |
| 198 |
} |