Debian Codegraph @

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