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
5package 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/6.7/search-aggregations-pipeline-bucket-script-aggregation.html
14type BucketScriptAggregation struct {
15	format    string
16	gapPolicy string
17	script    *Script
18
19	meta            map[string]interface{}
20	bucketsPathsMap map[string]string
21}
22
23// NewBucketScriptAggregation creates and initializes a new BucketScriptAggregation.
24func NewBucketScriptAggregation() *BucketScriptAggregation {
25	return &BucketScriptAggregation{
26		bucketsPathsMap: make(map[string]string),
27	}
28}
29
30// Format to use on the output of this aggregation.
31func (a *BucketScriptAggregation) Format(format string) *BucketScriptAggregation {
32	a.format = format
33	return a
34}
35
36// GapPolicy defines what should be done when a gap in the series is discovered.
37// Valid values include "insert_zeros" or "skip". Default is "insert_zeros".
38func (a *BucketScriptAggregation) GapPolicy(gapPolicy string) *BucketScriptAggregation {
39	a.gapPolicy = gapPolicy
40	return a
41}
42
43// GapInsertZeros inserts zeros for gaps in the series.
44func (a *BucketScriptAggregation) GapInsertZeros() *BucketScriptAggregation {
45	a.gapPolicy = "insert_zeros"
46	return a
47}
48
49// GapSkip skips gaps in the series.
50func (a *BucketScriptAggregation) GapSkip() *BucketScriptAggregation {
51	a.gapPolicy = "skip"
52	return a
53}
54
55// Script is the script to run.
56func (a *BucketScriptAggregation) Script(script *Script) *BucketScriptAggregation {
57	a.script = script
58	return a
59}
60
61// Meta sets the meta data to be included in the aggregation response.
62func (a *BucketScriptAggregation) Meta(metaData map[string]interface{}) *BucketScriptAggregation {
63	a.meta = metaData
64	return a
65}
66
67// BucketsPathsMap sets the paths to the buckets to use for this pipeline aggregator.
68func (a *BucketScriptAggregation) BucketsPathsMap(bucketsPathsMap map[string]string) *BucketScriptAggregation {
69	a.bucketsPathsMap = bucketsPathsMap
70	return a
71}
72
73// AddBucketsPath adds a bucket path to use for this pipeline aggregator.
74func (a *BucketScriptAggregation) AddBucketsPath(name, path string) *BucketScriptAggregation {
75	if a.bucketsPathsMap == nil {
76		a.bucketsPathsMap = make(map[string]string)
77	}
78	a.bucketsPathsMap[name] = path
79	return a
80}
81
82// Source returns the a JSON-serializable interface.
83func (a *BucketScriptAggregation) Source() (interface{}, error) {
84	source := make(map[string]interface{})
85	params := make(map[string]interface{})
86	source["bucket_script"] = params
87
88	if a.format != "" {
89		params["format"] = a.format
90	}
91	if a.gapPolicy != "" {
92		params["gap_policy"] = a.gapPolicy
93	}
94	if a.script != nil {
95		src, err := a.script.Source()
96		if err != nil {
97			return nil, err
98		}
99		params["script"] = src
100	}
101
102	// Add buckets paths
103	if len(a.bucketsPathsMap) > 0 {
104		params["buckets_path"] = a.bucketsPathsMap
105	}
106
107	// Add Meta data if available
108	if len(a.meta) > 0 {
109		source["meta"] = a.meta
110	}
111
112	return source, nil
113}
114