Debian Codegraph @

search_aggs_metrics_percentile_ranks.go

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