Debian Codegraph @

search_aggs_pipeline_min_bucket.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 // MinBucketAggregation is a sibling pipeline aggregation which identifies
8 // the bucket(s) with the maximum value of a specified metric in a sibling
9 // aggregation and outputs both the value and the key(s) of the bucket(s).
10 // The specified metric must be numeric and the sibling aggregation must
11 // be a multi-bucket aggregation.
12 //
13 // For more details, see
14 // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-min-bucket-aggregation.html
15 type MinBucketAggregation struct {
16 format string
17 gapPolicy string
18
19 subAggregations map[string]Aggregation
20 meta map[string]interface{}
21 bucketsPaths []string
22 }
23
24 // NewMinBucketAggregation creates and initializes a new MinBucketAggregation.
25 func NewMinBucketAggregation() *MinBucketAggregation {
26 return &MinBucketAggregation{
27 subAggregations: make(map[string]Aggregation),
28 bucketsPaths: make([]string, 0),
29 }
30 }
31
32 func (a *MinBucketAggregation) Format(format string) *MinBucketAggregation {
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 *MinBucketAggregation) GapPolicy(gapPolicy string) *MinBucketAggregation {
40 a.gapPolicy = gapPolicy
41 return a
42 }
43
44 // GapInsertZeros inserts zeros for gaps in the series.
45 func (a *MinBucketAggregation) GapInsertZeros() *MinBucketAggregation {
46 a.gapPolicy = "insert_zeros"
47 return a
48 }
49
50 // GapSkip skips gaps in the series.
51 func (a *MinBucketAggregation) GapSkip() *MinBucketAggregation {
52 a.gapPolicy = "skip"
53 return a
54 }
55
56 // SubAggregation adds a sub-aggregation to this aggregation.
57 func (a *MinBucketAggregation) SubAggregation(name string, subAggregation Aggregation) *MinBucketAggregation {
58 a.subAggregations[name] = subAggregation
59 return a
60 }
61
62 // Meta sets the meta data to be included in the aggregation response.
63 func (a *MinBucketAggregation) Meta(metaData map[string]interface{}) *MinBucketAggregation {
64 a.meta = metaData
65 return a
66 }
67
68 // BucketsPath sets the paths to the buckets to use for this pipeline aggregator.
69 func (a *MinBucketAggregation) BucketsPath(bucketsPaths ...string) *MinBucketAggregation {
70 a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...)
71 return a
72 }
73
74 func (a *MinBucketAggregation) Source() (interface{}, error) {
75 source := make(map[string]interface{})
76 params := make(map[string]interface{})
77 source["min_bucket"] = params
78
79 if a.format != "" {
80 params["format"] = a.format
81 }
82 if a.gapPolicy != "" {
83 params["gap_policy"] = a.gapPolicy
84 }
85
86 // Add buckets paths
87 switch len(a.bucketsPaths) {
88 case 0:
89 case 1:
90 params["buckets_path"] = a.bucketsPaths[0]
91 default:
92 params["buckets_path"] = a.bucketsPaths
93 }
94
95 // AggregationBuilder (SubAggregations)
96 if len(a.subAggregations) > 0 {
97 aggsMap := make(map[string]interface{})
98 source["aggregations"] = aggsMap
99 for name, aggregate := range a.subAggregations {
100 src, err := aggregate.Source()
101 if err != nil {
102 return nil, err
103 }
104 aggsMap[name] = src
105 }
106 }
107
108 // Add Meta data if available
109 if len(a.meta) > 0 {
110 source["meta"] = a.meta
111 }
112
113 return source, nil
114 }