Debian Codegraph @

search_aggs_pipeline_bucket_selector.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 // BucketSelectorAggregation is a parent pipeline aggregation which
8 // determines whether the current bucket will be retained in the parent
9 // multi-bucket aggregation. The specific metric must be numeric and
10 // the script must return a boolean value. If the script language is
11 // expression then a numeric return value is permitted. In this case 0.0
12 // will be evaluated as false and all other values will evaluate to true.
13 //
14 // For more details, see
15 // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-bucket-selector-aggregation.html
16 type BucketSelectorAggregation struct {
17 format string
18 gapPolicy string
19 script *Script
20
21 subAggregations map[string]Aggregation
22 meta map[string]interface{}
23 bucketsPathsMap map[string]string
24 }
25
26 // NewBucketSelectorAggregation creates and initializes a new BucketSelectorAggregation.
27 func NewBucketSelectorAggregation() *BucketSelectorAggregation {
28 return &BucketSelectorAggregation{
29 subAggregations: make(map[string]Aggregation),
30 bucketsPathsMap: make(map[string]string),
31 }
32 }
33
34 func (a *BucketSelectorAggregation) Format(format string) *BucketSelectorAggregation {
35 a.format = format
36 return a
37 }
38
39 // GapPolicy defines what should be done when a gap in the series is discovered.
40 // Valid values include "insert_zeros" or "skip". Default is "insert_zeros".
41 func (a *BucketSelectorAggregation) GapPolicy(gapPolicy string) *BucketSelectorAggregation {
42 a.gapPolicy = gapPolicy
43 return a
44 }
45
46 // GapInsertZeros inserts zeros for gaps in the series.
47 func (a *BucketSelectorAggregation) GapInsertZeros() *BucketSelectorAggregation {
48 a.gapPolicy = "insert_zeros"
49 return a
50 }
51
52 // GapSkip skips gaps in the series.
53 func (a *BucketSelectorAggregation) GapSkip() *BucketSelectorAggregation {
54 a.gapPolicy = "skip"
55 return a
56 }
57
58 // Script is the script to run.
59 func (a *BucketSelectorAggregation) Script(script *Script) *BucketSelectorAggregation {
60 a.script = script
61 return a
62 }
63
64 // SubAggregation adds a sub-aggregation to this aggregation.
65 func (a *BucketSelectorAggregation) SubAggregation(name string, subAggregation Aggregation) *BucketSelectorAggregation {
66 a.subAggregations[name] = subAggregation
67 return a
68 }
69
70 // Meta sets the meta data to be included in the aggregation response.
71 func (a *BucketSelectorAggregation) Meta(metaData map[string]interface{}) *BucketSelectorAggregation {
72 a.meta = metaData
73 return a
74 }
75
76 // BucketsPathsMap sets the paths to the buckets to use for this pipeline aggregator.
77 func (a *BucketSelectorAggregation) BucketsPathsMap(bucketsPathsMap map[string]string) *BucketSelectorAggregation {
78 a.bucketsPathsMap = bucketsPathsMap
79 return a
80 }
81
82 // AddBucketsPath adds a bucket path to use for this pipeline aggregator.
83 func (a *BucketSelectorAggregation) AddBucketsPath(name, path string) *BucketSelectorAggregation {
84 if a.bucketsPathsMap == nil {
85 a.bucketsPathsMap = make(map[string]string)
86 }
87 a.bucketsPathsMap[name] = path
88 return a
89 }
90
91 func (a *BucketSelectorAggregation) Source() (interface{}, error) {
92 source := make(map[string]interface{})
93 params := make(map[string]interface{})
94 source["bucket_selector"] = params
95
96 if a.format != "" {
97 params["format"] = a.format
98 }
99 if a.gapPolicy != "" {
100 params["gap_policy"] = a.gapPolicy
101 }
102 if a.script != nil {
103 src, err := a.script.Source()
104 if err != nil {
105 return nil, err
106 }
107 params["script"] = src
108 }
109
110 // Add buckets paths
111 if len(a.bucketsPathsMap) > 0 {
112 params["buckets_path"] = a.bucketsPathsMap
113 }
114
115 // AggregationBuilder (SubAggregations)
116 if len(a.subAggregations) > 0 {
117 aggsMap := make(map[string]interface{})
118 source["aggregations"] = aggsMap
119 for name, aggregate := range a.subAggregations {
120 src, err := aggregate.Source()
121 if err != nil {
122 return nil, err
123 }
124 aggsMap[name] = src
125 }
126 }
127
128 // Add Meta data if available
129 if len(a.meta) > 0 {
130 source["meta"] = a.meta
131 }
132
133 return source, nil
134 }