| 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 |
// PercentilesAggregation |
| 8 |
// See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-percentile-aggregation.html |
| 9 |
type PercentilesAggregation struct { |
| 10 |
field string |
| 11 |
script *Script |
| 12 |
format string |
| 13 |
subAggregations map[string]Aggregation |
| 14 |
meta map[string]interface{} |
| 15 |
percentiles []float64 |
| 16 |
method string |
| 17 |
compression *float64 |
| 18 |
numberOfSignificantValueDigits *int |
| 19 |
estimator string |
| 20 |
} |
| 21 |
|
| 22 |
func NewPercentilesAggregation() *PercentilesAggregation { |
| 23 |
return &PercentilesAggregation{ |
| 24 |
subAggregations: make(map[string]Aggregation), |
| 25 |
percentiles: make([]float64, 0), |
| 26 |
method: "tdigest", |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func (a *PercentilesAggregation) Field(field string) *PercentilesAggregation { |
| 31 |
a.field = field |
| 32 |
return a |
| 33 |
} |
| 34 |
|
| 35 |
func (a *PercentilesAggregation) Script(script *Script) *PercentilesAggregation { |
| 36 |
a.script = script |
| 37 |
return a |
| 38 |
} |
| 39 |
|
| 40 |
func (a *PercentilesAggregation) Format(format string) *PercentilesAggregation { |
| 41 |
a.format = format |
| 42 |
return a |
| 43 |
} |
| 44 |
|
| 45 |
func (a *PercentilesAggregation) SubAggregation(name string, subAggregation Aggregation) *PercentilesAggregation { |
| 46 |
a.subAggregations[name] = subAggregation |
| 47 |
return a |
| 48 |
} |
| 49 |
|
| 50 |
// Meta sets the meta data to be included in the aggregation response. |
| 51 |
func (a *PercentilesAggregation) Meta(metaData map[string]interface{}) *PercentilesAggregation { |
| 52 |
a.meta = metaData |
| 53 |
return a |
| 54 |
} |
| 55 |
|
| 56 |
func (a *PercentilesAggregation) Percentiles(percentiles ...float64) *PercentilesAggregation { |
| 57 |
a.percentiles = append(a.percentiles, percentiles...) |
| 58 |
return a |
| 59 |
} |
| 60 |
|
| 61 |
// Method is the percentiles method, which can be "tdigest" (default) or "hdr". |
| 62 |
func (a *PercentilesAggregation) Method(method string) *PercentilesAggregation { |
| 63 |
a.method = method |
| 64 |
return a |
| 65 |
} |
| 66 |
|
| 67 |
func (a *PercentilesAggregation) Compression(compression float64) *PercentilesAggregation { |
| 68 |
a.compression = &compression |
| 69 |
return a |
| 70 |
} |
| 71 |
|
| 72 |
func (a *PercentilesAggregation) NumberOfSignificantValueDigits(digits int) *PercentilesAggregation { |
| 73 |
a.numberOfSignificantValueDigits = &digits |
| 74 |
return a |
| 75 |
} |
| 76 |
|
| 77 |
func (a *PercentilesAggregation) Estimator(estimator string) *PercentilesAggregation { |
| 78 |
a.estimator = estimator |
| 79 |
return a |
| 80 |
} |
| 81 |
|
| 82 |
func (a *PercentilesAggregation) Source() (interface{}, error) { |
| 83 |
// Example: |
| 84 |
// { |
| 85 |
// "aggs" : { |
| 86 |
// "load_time_outlier" : { |
| 87 |
// "percentiles" : { |
| 88 |
// "field" : "load_time" |
| 89 |
// } |
| 90 |
// } |
| 91 |
// } |
| 92 |
// } |
| 93 |
// This method returns only the |
| 94 |
// { "percentiles" : { "field" : "load_time" } } |
| 95 |
// part. |
| 96 |
|
| 97 |
source := make(map[string]interface{}) |
| 98 |
opts := make(map[string]interface{}) |
| 99 |
source["percentiles"] = opts |
| 100 |
|
| 101 |
// ValuesSourceAggregationBuilder |
| 102 |
if a.field != "" { |
| 103 |
opts["field"] = a.field |
| 104 |
} |
| 105 |
if a.script != nil { |
| 106 |
src, err := a.script.Source() |
| 107 |
if err != nil { |
| 108 |
return nil, err |
| 109 |
} |
| 110 |
opts["script"] = src |
| 111 |
} |
| 112 |
if a.format != "" { |
| 113 |
opts["format"] = a.format |
| 114 |
} |
| 115 |
if len(a.percentiles) > 0 { |
| 116 |
opts["percents"] = a.percentiles |
| 117 |
} |
| 118 |
switch a.method { |
| 119 |
case "tdigest": |
| 120 |
if c := a.compression; c != nil { |
| 121 |
opts[a.method] = map[string]interface{}{ |
| 122 |
"compression": *c, |
| 123 |
} |
| 124 |
} |
| 125 |
case "hdr": |
| 126 |
if n := a.numberOfSignificantValueDigits; n != nil { |
| 127 |
opts[a.method] = map[string]interface{}{ |
| 128 |
"number_of_significant_value_digits": *n, |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
if a.estimator != "" { |
| 133 |
opts["estimator"] = a.estimator |
| 134 |
} |
| 135 |
|
| 136 |
// AggregationBuilder (SubAggregations) |
| 137 |
if len(a.subAggregations) > 0 { |
| 138 |
aggsMap := make(map[string]interface{}) |
| 139 |
source["aggregations"] = aggsMap |
| 140 |
for name, aggregate := range a.subAggregations { |
| 141 |
src, err := aggregate.Source() |
| 142 |
if err != nil { |
| 143 |
return nil, err |
| 144 |
} |
| 145 |
aggsMap[name] = src |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// Add Meta data if available |
| 150 |
if len(a.meta) > 0 { |
| 151 |
source["meta"] = a.meta |
| 152 |
} |
| 153 |
|
| 154 |
return source, nil |
| 155 |
} |