Debian Codegraph @

search_aggs_pipeline_percentiles_bucket.go

raw
1 // Copyright 2012-2015 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 // PercentilesBucketAggregation is a sibling pipeline aggregation which calculates
8 // percentiles across all bucket of a specified metric in a sibling aggregation.
9 // The specified metric must be numeric and the sibling aggregation must
10 // be a multi-bucket aggregation.
11 //
12 // For more details, see
13 // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-percentiles-bucket-aggregation.html
14 type PercentilesBucketAggregation struct {
15 format string
16 gapPolicy string
17 percents []float64
18 bucketsPaths []string
19
20 subAggregations map[string]Aggregation
21 meta map[string]interface{}
22 }
23
24 // NewPercentilesBucketAggregation creates and initializes a new PercentilesBucketAggregation.
25 func NewPercentilesBucketAggregation() *PercentilesBucketAggregation {
26 return &PercentilesBucketAggregation{
27 subAggregations: make(map[string]Aggregation),
28 }
29 }
30
31 // Format to apply the output value of this aggregation.
32 func (p *PercentilesBucketAggregation) Format(format string) *PercentilesBucketAggregation {
33 p.format = format
34 return p
35 }
36
37 // Percents to calculate percentiles for in this aggregation.
38 func (p *PercentilesBucketAggregation) Percents(percents ...float64) *PercentilesBucketAggregation {
39 p.percents = percents
40 return p
41 }
42
43 // GapPolicy defines what should be done when a gap in the series is discovered.
44 // Valid values include "insert_zeros" or "skip". Default is "insert_zeros".
45 func (p *PercentilesBucketAggregation) GapPolicy(gapPolicy string) *PercentilesBucketAggregation {
46 p.gapPolicy = gapPolicy
47 return p
48 }
49
50 // GapInsertZeros inserts zeros for gaps in the series.
51 func (p *PercentilesBucketAggregation) GapInsertZeros() *PercentilesBucketAggregation {
52 p.gapPolicy = "insert_zeros"
53 return p
54 }
55
56 // GapSkip skips gaps in the series.
57 func (p *PercentilesBucketAggregation) GapSkip() *PercentilesBucketAggregation {
58 p.gapPolicy = "skip"
59 return p
60 }
61
62 // SubAggregation adds a sub-aggregation to this aggregation.
63 func (p *PercentilesBucketAggregation) SubAggregation(name string, subAggregation Aggregation) *PercentilesBucketAggregation {
64 p.subAggregations[name] = subAggregation
65 return p
66 }
67
68 // Meta sets the meta data to be included in the aggregation response.
69 func (p *PercentilesBucketAggregation) Meta(metaData map[string]interface{}) *PercentilesBucketAggregation {
70 p.meta = metaData
71 return p
72 }
73
74 // BucketsPath sets the paths to the buckets to use for this pipeline aggregator.
75 func (p *PercentilesBucketAggregation) BucketsPath(bucketsPaths ...string) *PercentilesBucketAggregation {
76 p.bucketsPaths = append(p.bucketsPaths, bucketsPaths...)
77 return p
78 }
79
80 func (p *PercentilesBucketAggregation) Source() (interface{}, error) {
81 source := make(map[string]interface{})
82 params := make(map[string]interface{})
83 source["percentiles_bucket"] = params
84
85 if p.format != "" {
86 params["format"] = p.format
87 }
88 if p.gapPolicy != "" {
89 params["gap_policy"] = p.gapPolicy
90 }
91
92 // Add buckets paths
93 switch len(p.bucketsPaths) {
94 case 0:
95 case 1:
96 params["buckets_path"] = p.bucketsPaths[0]
97 default:
98 params["buckets_path"] = p.bucketsPaths
99 }
100
101 // Add percents
102 if len(p.percents) > 0 {
103 params["percents"] = p.percents
104 }
105
106 // AggregationBuilder (SubAggregations)
107 if len(p.subAggregations) > 0 {
108 aggsMap := make(map[string]interface{})
109 source["aggregations"] = aggsMap
110 for name, aggregate := range p.subAggregations {
111 src, err := aggregate.Source()
112 if err != nil {
113 return nil, err
114 }
115 aggsMap[name] = src
116 }
117 }
118
119 // Add Meta data if available
120 if len(p.meta) > 0 {
121 source["meta"] = p.meta
122 }
123
124 return source, nil
125 }