| 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 |
// HistogramAggregation is a multi-bucket values source based aggregation |
| 8 |
// that can be applied on numeric values extracted from the documents. |
| 9 |
// It dynamically builds fixed size (a.k.a. interval) buckets over the |
| 10 |
// values. |
| 11 |
// See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-bucket-histogram-aggregation.html |
| 12 |
type HistogramAggregation struct { |
| 13 |
field string |
| 14 |
script *Script |
| 15 |
missing interface{} |
| 16 |
subAggregations map[string]Aggregation |
| 17 |
meta map[string]interface{} |
| 18 |
|
| 19 |
interval float64 |
| 20 |
order string |
| 21 |
orderAsc bool |
| 22 |
minDocCount *int64 |
| 23 |
minBounds *float64 |
| 24 |
maxBounds *float64 |
| 25 |
offset *float64 |
| 26 |
} |
| 27 |
|
| 28 |
func NewHistogramAggregation() *HistogramAggregation { |
| 29 |
return &HistogramAggregation{ |
| 30 |
subAggregations: make(map[string]Aggregation), |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func (a *HistogramAggregation) Field(field string) *HistogramAggregation { |
| 35 |
a.field = field |
| 36 |
return a |
| 37 |
} |
| 38 |
|
| 39 |
func (a *HistogramAggregation) Script(script *Script) *HistogramAggregation { |
| 40 |
a.script = script |
| 41 |
return a |
| 42 |
} |
| 43 |
|
| 44 |
// Missing configures the value to use when documents miss a value. |
| 45 |
func (a *HistogramAggregation) Missing(missing interface{}) *HistogramAggregation { |
| 46 |
a.missing = missing |
| 47 |
return a |
| 48 |
} |
| 49 |
|
| 50 |
func (a *HistogramAggregation) SubAggregation(name string, subAggregation Aggregation) *HistogramAggregation { |
| 51 |
a.subAggregations[name] = subAggregation |
| 52 |
return a |
| 53 |
} |
| 54 |
|
| 55 |
// Meta sets the meta data to be included in the aggregation response. |
| 56 |
func (a *HistogramAggregation) Meta(metaData map[string]interface{}) *HistogramAggregation { |
| 57 |
a.meta = metaData |
| 58 |
return a |
| 59 |
} |
| 60 |
|
| 61 |
// Interval for this builder, must be greater than 0. |
| 62 |
func (a *HistogramAggregation) Interval(interval float64) *HistogramAggregation { |
| 63 |
a.interval = interval |
| 64 |
return a |
| 65 |
} |
| 66 |
|
| 67 |
// Order specifies the sort order. Valid values for order are: |
| 68 |
// "_key", "_count", a sub-aggregation name, or a sub-aggregation name |
| 69 |
// with a metric. |
| 70 |
func (a *HistogramAggregation) Order(order string, asc bool) *HistogramAggregation { |
| 71 |
a.order = order |
| 72 |
a.orderAsc = asc |
| 73 |
return a |
| 74 |
} |
| 75 |
|
| 76 |
func (a *HistogramAggregation) OrderByCount(asc bool) *HistogramAggregation { |
| 77 |
// "order" : { "_count" : "asc" } |
| 78 |
a.order = "_count" |
| 79 |
a.orderAsc = asc |
| 80 |
return a |
| 81 |
} |
| 82 |
|
| 83 |
func (a *HistogramAggregation) OrderByCountAsc() *HistogramAggregation { |
| 84 |
return a.OrderByCount(true) |
| 85 |
} |
| 86 |
|
| 87 |
func (a *HistogramAggregation) OrderByCountDesc() *HistogramAggregation { |
| 88 |
return a.OrderByCount(false) |
| 89 |
} |
| 90 |
|
| 91 |
func (a *HistogramAggregation) OrderByKey(asc bool) *HistogramAggregation { |
| 92 |
// "order" : { "_key" : "asc" } |
| 93 |
a.order = "_key" |
| 94 |
a.orderAsc = asc |
| 95 |
return a |
| 96 |
} |
| 97 |
|
| 98 |
func (a *HistogramAggregation) OrderByKeyAsc() *HistogramAggregation { |
| 99 |
return a.OrderByKey(true) |
| 100 |
} |
| 101 |
|
| 102 |
func (a *HistogramAggregation) OrderByKeyDesc() *HistogramAggregation { |
| 103 |
return a.OrderByKey(false) |
| 104 |
} |
| 105 |
|
| 106 |
// OrderByAggregation creates a bucket ordering strategy which sorts buckets |
| 107 |
// based on a single-valued calc get. |
| 108 |
func (a *HistogramAggregation) OrderByAggregation(aggName string, asc bool) *HistogramAggregation { |
| 109 |
// { |
| 110 |
// "aggs" : { |
| 111 |
// "genders" : { |
| 112 |
// "terms" : { |
| 113 |
// "field" : "gender", |
| 114 |
// "order" : { "avg_height" : "desc" } |
| 115 |
// }, |
| 116 |
// "aggs" : { |
| 117 |
// "avg_height" : { "avg" : { "field" : "height" } } |
| 118 |
// } |
| 119 |
// } |
| 120 |
// } |
| 121 |
// } |
| 122 |
a.order = aggName |
| 123 |
a.orderAsc = asc |
| 124 |
return a |
| 125 |
} |
| 126 |
|
| 127 |
// OrderByAggregationAndMetric creates a bucket ordering strategy which |
| 128 |
// sorts buckets based on a multi-valued calc get. |
| 129 |
func (a *HistogramAggregation) OrderByAggregationAndMetric(aggName, metric string, asc bool) *HistogramAggregation { |
| 130 |
// { |
| 131 |
// "aggs" : { |
| 132 |
// "genders" : { |
| 133 |
// "terms" : { |
| 134 |
// "field" : "gender", |
| 135 |
// "order" : { "height_stats.avg" : "desc" } |
| 136 |
// }, |
| 137 |
// "aggs" : { |
| 138 |
// "height_stats" : { "stats" : { "field" : "height" } } |
| 139 |
// } |
| 140 |
// } |
| 141 |
// } |
| 142 |
// } |
| 143 |
a.order = aggName + "." + metric |
| 144 |
a.orderAsc = asc |
| 145 |
return a |
| 146 |
} |
| 147 |
|
| 148 |
func (a *HistogramAggregation) MinDocCount(minDocCount int64) *HistogramAggregation { |
| 149 |
a.minDocCount = &minDocCount |
| 150 |
return a |
| 151 |
} |
| 152 |
|
| 153 |
func (a *HistogramAggregation) ExtendedBounds(min, max float64) *HistogramAggregation { |
| 154 |
a.minBounds = &min |
| 155 |
a.maxBounds = &max |
| 156 |
return a |
| 157 |
} |
| 158 |
|
| 159 |
func (a *HistogramAggregation) ExtendedBoundsMin(min float64) *HistogramAggregation { |
| 160 |
a.minBounds = &min |
| 161 |
return a |
| 162 |
} |
| 163 |
|
| 164 |
func (a *HistogramAggregation) MinBounds(min float64) *HistogramAggregation { |
| 165 |
a.minBounds = &min |
| 166 |
return a |
| 167 |
} |
| 168 |
|
| 169 |
func (a *HistogramAggregation) ExtendedBoundsMax(max float64) *HistogramAggregation { |
| 170 |
a.maxBounds = &max |
| 171 |
return a |
| 172 |
} |
| 173 |
|
| 174 |
func (a *HistogramAggregation) MaxBounds(max float64) *HistogramAggregation { |
| 175 |
a.maxBounds = &max |
| 176 |
return a |
| 177 |
} |
| 178 |
|
| 179 |
// Offset into the histogram |
| 180 |
func (a *HistogramAggregation) Offset(offset float64) *HistogramAggregation { |
| 181 |
a.offset = &offset |
| 182 |
return a |
| 183 |
} |
| 184 |
|
| 185 |
func (a *HistogramAggregation) Source() (interface{}, error) { |
| 186 |
// Example: |
| 187 |
// { |
| 188 |
// "aggs" : { |
| 189 |
// "prices" : { |
| 190 |
// "histogram" : { |
| 191 |
// "field" : "price", |
| 192 |
// "interval" : 50 |
| 193 |
// } |
| 194 |
// } |
| 195 |
// } |
| 196 |
// } |
| 197 |
// |
| 198 |
// This method returns only the { "histogram" : { ... } } part. |
| 199 |
|
| 200 |
source := make(map[string]interface{}) |
| 201 |
opts := make(map[string]interface{}) |
| 202 |
source["histogram"] = opts |
| 203 |
|
| 204 |
// ValuesSourceAggregationBuilder |
| 205 |
if a.field != "" { |
| 206 |
opts["field"] = a.field |
| 207 |
} |
| 208 |
if a.script != nil { |
| 209 |
src, err := a.script.Source() |
| 210 |
if err != nil { |
| 211 |
return nil, err |
| 212 |
} |
| 213 |
opts["script"] = src |
| 214 |
} |
| 215 |
if a.missing != nil { |
| 216 |
opts["missing"] = a.missing |
| 217 |
} |
| 218 |
|
| 219 |
opts["interval"] = a.interval |
| 220 |
if a.order != "" { |
| 221 |
o := make(map[string]interface{}) |
| 222 |
if a.orderAsc { |
| 223 |
o[a.order] = "asc" |
| 224 |
} else { |
| 225 |
o[a.order] = "desc" |
| 226 |
} |
| 227 |
opts["order"] = o |
| 228 |
} |
| 229 |
if a.offset != nil { |
| 230 |
opts["offset"] = *a.offset |
| 231 |
} |
| 232 |
if a.minDocCount != nil { |
| 233 |
opts["min_doc_count"] = *a.minDocCount |
| 234 |
} |
| 235 |
if a.minBounds != nil || a.maxBounds != nil { |
| 236 |
bounds := make(map[string]interface{}) |
| 237 |
if a.minBounds != nil { |
| 238 |
bounds["min"] = a.minBounds |
| 239 |
} |
| 240 |
if a.maxBounds != nil { |
| 241 |
bounds["max"] = a.maxBounds |
| 242 |
} |
| 243 |
opts["extended_bounds"] = bounds |
| 244 |
} |
| 245 |
|
| 246 |
// AggregationBuilder (SubAggregations) |
| 247 |
if len(a.subAggregations) > 0 { |
| 248 |
aggsMap := make(map[string]interface{}) |
| 249 |
source["aggregations"] = aggsMap |
| 250 |
for name, aggregate := range a.subAggregations { |
| 251 |
src, err := aggregate.Source() |
| 252 |
if err != nil { |
| 253 |
return nil, err |
| 254 |
} |
| 255 |
aggsMap[name] = src |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
// Add Meta data if available |
| 260 |
if len(a.meta) > 0 { |
| 261 |
source["meta"] = a.meta |
| 262 |
} |
| 263 |
|
| 264 |
return source, nil |
| 265 |
} |