| 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 |
import ( |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
// RangeAggregation is a multi-bucket value source based aggregation that |
| 12 |
// enables the user to define a set of ranges - each representing a bucket. |
| 13 |
// During the aggregation process, the values extracted from each document |
| 14 |
// will be checked against each bucket range and "bucket" the |
| 15 |
// relevant/matching document. Note that this aggregration includes the |
| 16 |
// from value and excludes the to value for each range. |
| 17 |
// See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-range-aggregation.html |
| 18 |
type RangeAggregation struct { |
| 19 |
field string |
| 20 |
script *Script |
| 21 |
missing interface{} |
| 22 |
subAggregations map[string]Aggregation |
| 23 |
meta map[string]interface{} |
| 24 |
keyed *bool |
| 25 |
unmapped *bool |
| 26 |
entries []rangeAggregationEntry |
| 27 |
} |
| 28 |
|
| 29 |
type rangeAggregationEntry struct { |
| 30 |
Key string |
| 31 |
From interface{} |
| 32 |
To interface{} |
| 33 |
} |
| 34 |
|
| 35 |
func NewRangeAggregation() *RangeAggregation { |
| 36 |
return &RangeAggregation{ |
| 37 |
subAggregations: make(map[string]Aggregation), |
| 38 |
entries: make([]rangeAggregationEntry, 0), |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func (a *RangeAggregation) Field(field string) *RangeAggregation { |
| 43 |
a.field = field |
| 44 |
return a |
| 45 |
} |
| 46 |
|
| 47 |
func (a *RangeAggregation) Script(script *Script) *RangeAggregation { |
| 48 |
a.script = script |
| 49 |
return a |
| 50 |
} |
| 51 |
|
| 52 |
// Missing configures the value to use when documents miss a value. |
| 53 |
func (a *RangeAggregation) Missing(missing interface{}) *RangeAggregation { |
| 54 |
a.missing = missing |
| 55 |
return a |
| 56 |
} |
| 57 |
|
| 58 |
func (a *RangeAggregation) SubAggregation(name string, subAggregation Aggregation) *RangeAggregation { |
| 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 *RangeAggregation) Meta(metaData map[string]interface{}) *RangeAggregation { |
| 65 |
a.meta = metaData |
| 66 |
return a |
| 67 |
} |
| 68 |
|
| 69 |
func (a *RangeAggregation) Keyed(keyed bool) *RangeAggregation { |
| 70 |
a.keyed = &keyed |
| 71 |
return a |
| 72 |
} |
| 73 |
|
| 74 |
func (a *RangeAggregation) Unmapped(unmapped bool) *RangeAggregation { |
| 75 |
a.unmapped = &unmapped |
| 76 |
return a |
| 77 |
} |
| 78 |
|
| 79 |
func (a *RangeAggregation) AddRange(from, to interface{}) *RangeAggregation { |
| 80 |
a.entries = append(a.entries, rangeAggregationEntry{From: from, To: to}) |
| 81 |
return a |
| 82 |
} |
| 83 |
|
| 84 |
func (a *RangeAggregation) AddRangeWithKey(key string, from, to interface{}) *RangeAggregation { |
| 85 |
a.entries = append(a.entries, rangeAggregationEntry{Key: key, From: from, To: to}) |
| 86 |
return a |
| 87 |
} |
| 88 |
|
| 89 |
func (a *RangeAggregation) AddUnboundedTo(from interface{}) *RangeAggregation { |
| 90 |
a.entries = append(a.entries, rangeAggregationEntry{From: from, To: nil}) |
| 91 |
return a |
| 92 |
} |
| 93 |
|
| 94 |
func (a *RangeAggregation) AddUnboundedToWithKey(key string, from interface{}) *RangeAggregation { |
| 95 |
a.entries = append(a.entries, rangeAggregationEntry{Key: key, From: from, To: nil}) |
| 96 |
return a |
| 97 |
} |
| 98 |
|
| 99 |
func (a *RangeAggregation) AddUnboundedFrom(to interface{}) *RangeAggregation { |
| 100 |
a.entries = append(a.entries, rangeAggregationEntry{From: nil, To: to}) |
| 101 |
return a |
| 102 |
} |
| 103 |
|
| 104 |
func (a *RangeAggregation) AddUnboundedFromWithKey(key string, to interface{}) *RangeAggregation { |
| 105 |
a.entries = append(a.entries, rangeAggregationEntry{Key: key, From: nil, To: to}) |
| 106 |
return a |
| 107 |
} |
| 108 |
|
| 109 |
func (a *RangeAggregation) Lt(to interface{}) *RangeAggregation { |
| 110 |
a.entries = append(a.entries, rangeAggregationEntry{From: nil, To: to}) |
| 111 |
return a |
| 112 |
} |
| 113 |
|
| 114 |
func (a *RangeAggregation) LtWithKey(key string, to interface{}) *RangeAggregation { |
| 115 |
a.entries = append(a.entries, rangeAggregationEntry{Key: key, From: nil, To: to}) |
| 116 |
return a |
| 117 |
} |
| 118 |
|
| 119 |
func (a *RangeAggregation) Between(from, to interface{}) *RangeAggregation { |
| 120 |
a.entries = append(a.entries, rangeAggregationEntry{From: from, To: to}) |
| 121 |
return a |
| 122 |
} |
| 123 |
|
| 124 |
func (a *RangeAggregation) BetweenWithKey(key string, from, to interface{}) *RangeAggregation { |
| 125 |
a.entries = append(a.entries, rangeAggregationEntry{Key: key, From: from, To: to}) |
| 126 |
return a |
| 127 |
} |
| 128 |
|
| 129 |
func (a *RangeAggregation) Gt(from interface{}) *RangeAggregation { |
| 130 |
a.entries = append(a.entries, rangeAggregationEntry{From: from, To: nil}) |
| 131 |
return a |
| 132 |
} |
| 133 |
|
| 134 |
func (a *RangeAggregation) GtWithKey(key string, from interface{}) *RangeAggregation { |
| 135 |
a.entries = append(a.entries, rangeAggregationEntry{Key: key, From: from, To: nil}) |
| 136 |
return a |
| 137 |
} |
| 138 |
|
| 139 |
func (a *RangeAggregation) Source() (interface{}, error) { |
| 140 |
// Example: |
| 141 |
// { |
| 142 |
// "aggs" : { |
| 143 |
// "price_ranges" : { |
| 144 |
// "range" : { |
| 145 |
// "field" : "price", |
| 146 |
// "ranges" : [ |
| 147 |
// { "to" : 50 }, |
| 148 |
// { "from" : 50, "to" : 100 }, |
| 149 |
// { "from" : 100 } |
| 150 |
// ] |
| 151 |
// } |
| 152 |
// } |
| 153 |
// } |
| 154 |
// } |
| 155 |
// |
| 156 |
// This method returns only the { "range" : { ... } } part. |
| 157 |
|
| 158 |
source := make(map[string]interface{}) |
| 159 |
opts := make(map[string]interface{}) |
| 160 |
source["range"] = opts |
| 161 |
|
| 162 |
// ValuesSourceAggregationBuilder |
| 163 |
if a.field != "" { |
| 164 |
opts["field"] = a.field |
| 165 |
} |
| 166 |
if a.script != nil { |
| 167 |
src, err := a.script.Source() |
| 168 |
if err != nil { |
| 169 |
return nil, err |
| 170 |
} |
| 171 |
opts["script"] = src |
| 172 |
} |
| 173 |
if a.missing != nil { |
| 174 |
opts["missing"] = a.missing |
| 175 |
} |
| 176 |
|
| 177 |
if a.keyed != nil { |
| 178 |
opts["keyed"] = *a.keyed |
| 179 |
} |
| 180 |
if a.unmapped != nil { |
| 181 |
opts["unmapped"] = *a.unmapped |
| 182 |
} |
| 183 |
|
| 184 |
var ranges []interface{} |
| 185 |
for _, ent := range a.entries { |
| 186 |
r := make(map[string]interface{}) |
| 187 |
if ent.Key != "" { |
| 188 |
r["key"] = ent.Key |
| 189 |
} |
| 190 |
if ent.From != nil { |
| 191 |
switch from := ent.From.(type) { |
| 192 |
case int, int16, int32, int64, float32, float64: |
| 193 |
r["from"] = from |
| 194 |
case *int, *int16, *int32, *int64, *float32, *float64: |
| 195 |
r["from"] = from |
| 196 |
case time.Time: |
| 197 |
r["from"] = from.Format(time.RFC3339) |
| 198 |
case *time.Time: |
| 199 |
r["from"] = from.Format(time.RFC3339) |
| 200 |
case string: |
| 201 |
r["from"] = from |
| 202 |
case *string: |
| 203 |
r["from"] = from |
| 204 |
} |
| 205 |
} |
| 206 |
if ent.To != nil { |
| 207 |
switch to := ent.To.(type) { |
| 208 |
case int, int16, int32, int64, float32, float64: |
| 209 |
r["to"] = to |
| 210 |
case *int, *int16, *int32, *int64, *float32, *float64: |
| 211 |
r["to"] = to |
| 212 |
case time.Time: |
| 213 |
r["to"] = to.Format(time.RFC3339) |
| 214 |
case *time.Time: |
| 215 |
r["to"] = to.Format(time.RFC3339) |
| 216 |
case string: |
| 217 |
r["to"] = to |
| 218 |
case *string: |
| 219 |
r["to"] = to |
| 220 |
} |
| 221 |
} |
| 222 |
ranges = append(ranges, r) |
| 223 |
} |
| 224 |
opts["ranges"] = ranges |
| 225 |
|
| 226 |
// AggregationBuilder (SubAggregations) |
| 227 |
if len(a.subAggregations) > 0 { |
| 228 |
aggsMap := make(map[string]interface{}) |
| 229 |
source["aggregations"] = aggsMap |
| 230 |
for name, aggregate := range a.subAggregations { |
| 231 |
src, err := aggregate.Source() |
| 232 |
if err != nil { |
| 233 |
return nil, err |
| 234 |
} |
| 235 |
aggsMap[name] = src |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
// Add Meta data if available |
| 240 |
if len(a.meta) > 0 { |
| 241 |
source["meta"] = a.meta |
| 242 |
} |
| 243 |
return source, nil |
| 244 |
} |