| 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 |
// CumulativeSumAggregation is a parent pipeline aggregation which calculates |
| 8 |
// the cumulative sum of a specified metric in a parent histogram (or date_histogram) |
| 9 |
// aggregation. The specified metric must be numeric and the enclosing |
| 10 |
// histogram must have min_doc_count set to 0 (default for histogram aggregations). |
| 11 |
// |
| 12 |
// For more details, see |
| 13 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-cumulative-sum-aggregation.html |
| 14 |
type CumulativeSumAggregation struct { |
| 15 |
format string |
| 16 |
|
| 17 |
subAggregations map[string]Aggregation |
| 18 |
meta map[string]interface{} |
| 19 |
bucketsPaths []string |
| 20 |
} |
| 21 |
|
| 22 |
// NewCumulativeSumAggregation creates and initializes a new CumulativeSumAggregation. |
| 23 |
func NewCumulativeSumAggregation() *CumulativeSumAggregation { |
| 24 |
return &CumulativeSumAggregation{ |
| 25 |
subAggregations: make(map[string]Aggregation), |
| 26 |
bucketsPaths: make([]string, 0), |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func (a *CumulativeSumAggregation) Format(format string) *CumulativeSumAggregation { |
| 31 |
a.format = format |
| 32 |
return a |
| 33 |
} |
| 34 |
|
| 35 |
// SubAggregation adds a sub-aggregation to this aggregation. |
| 36 |
func (a *CumulativeSumAggregation) SubAggregation(name string, subAggregation Aggregation) *CumulativeSumAggregation { |
| 37 |
a.subAggregations[name] = subAggregation |
| 38 |
return a |
| 39 |
} |
| 40 |
|
| 41 |
// Meta sets the meta data to be included in the aggregation response. |
| 42 |
func (a *CumulativeSumAggregation) Meta(metaData map[string]interface{}) *CumulativeSumAggregation { |
| 43 |
a.meta = metaData |
| 44 |
return a |
| 45 |
} |
| 46 |
|
| 47 |
// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. |
| 48 |
func (a *CumulativeSumAggregation) BucketsPath(bucketsPaths ...string) *CumulativeSumAggregation { |
| 49 |
a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...) |
| 50 |
return a |
| 51 |
} |
| 52 |
|
| 53 |
func (a *CumulativeSumAggregation) Source() (interface{}, error) { |
| 54 |
source := make(map[string]interface{}) |
| 55 |
params := make(map[string]interface{}) |
| 56 |
source["cumulative_sum"] = params |
| 57 |
|
| 58 |
if a.format != "" { |
| 59 |
params["format"] = a.format |
| 60 |
} |
| 61 |
|
| 62 |
// Add buckets paths |
| 63 |
switch len(a.bucketsPaths) { |
| 64 |
case 0: |
| 65 |
case 1: |
| 66 |
params["buckets_path"] = a.bucketsPaths[0] |
| 67 |
default: |
| 68 |
params["buckets_path"] = a.bucketsPaths |
| 69 |
} |
| 70 |
|
| 71 |
// AggregationBuilder (SubAggregations) |
| 72 |
if len(a.subAggregations) > 0 { |
| 73 |
aggsMap := make(map[string]interface{}) |
| 74 |
source["aggregations"] = aggsMap |
| 75 |
for name, aggregate := range a.subAggregations { |
| 76 |
src, err := aggregate.Source() |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
aggsMap[name] = src |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// Add Meta data if available |
| 85 |
if len(a.meta) > 0 { |
| 86 |
source["meta"] = a.meta |
| 87 |
} |
| 88 |
|
| 89 |
return source, nil |
| 90 |
} |