-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaggs_metrics_scripted.go
116 lines (98 loc) · 2.95 KB
/
aggs_metrics_scripted.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package aggretastic
import "github.com/olivere/elastic/v7"
// ScriptedMetricAggregation
// wip (larry) careful to be used
//
// See: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
type ScriptedMetricAggregation struct {
*notInjectable
initScript *elastic.Script
mapScript *elastic.Script
combineScript *elastic.Script
reduceScript *elastic.Script
params map[string]interface{}
meta map[string]interface{}
}
func NewScriptedMetricAggregation() *ScriptedMetricAggregation {
a := &ScriptedMetricAggregation{}
a.notInjectable = newNotInjectable(a)
return a
}
func (a *ScriptedMetricAggregation) InitScript(script *elastic.Script) *ScriptedMetricAggregation {
a.initScript = script
return a
}
func (a *ScriptedMetricAggregation) MapScript(script *elastic.Script) *ScriptedMetricAggregation {
a.mapScript = script
return a
}
func (a *ScriptedMetricAggregation) CombineScript(script *elastic.Script) *ScriptedMetricAggregation {
a.combineScript = script
return a
}
func (a *ScriptedMetricAggregation) ReduceScript(script *elastic.Script) *ScriptedMetricAggregation {
a.reduceScript = script
return a
}
func (a *ScriptedMetricAggregation) Params(params map[string]interface{}) *ScriptedMetricAggregation {
a.params = params
return a
}
// Meta sets the meta data to be included in the aggregation response.
func (a *ScriptedMetricAggregation) Meta(metaData map[string]interface{}) *ScriptedMetricAggregation {
a.meta = metaData
return a
}
func (a *ScriptedMetricAggregation) Source() (interface{}, error) {
// Example:
// {
// "aggs" : {
// "magic_script" : { "scripted_metric" : {
// "init_script" : "state.transactions = []",
// "map_script" : "state.transactions.add(doc.type.value == 'sale' ? doc.amount.value : -1 * doc.amount.value)",
// "combine_script" : "double profit = 0; for (t in state.transactions) { profit += t } return profit",
// "reduce_script" : "double profit = 0; for (a in states) { profit += a } return profit"
// } }
// }
// }
// This method returns only the { "scripted_metric" : { ... } } part.
source := make(map[string]interface{})
opts := make(map[string]interface{})
source["scripted_metric"] = opts
if a.initScript != nil {
src, err := a.initScript.Source()
if err != nil {
return nil, err
}
opts["init_script"] = src
}
if a.mapScript != nil {
src, err := a.mapScript.Source()
if err != nil {
return nil, err
}
opts["map_script"] = src
}
if a.combineScript != nil {
src, err := a.combineScript.Source()
if err != nil {
return nil, err
}
opts["combine_script"] = src
}
if a.reduceScript != nil {
src, err := a.reduceScript.Source()
if err != nil {
return nil, err
}
opts["reduce_script"] = src
}
if a.params != nil && len(a.params) > 0 {
opts["params"] = a.params
}
// Add Meta data if available
if len(a.meta) > 0 {
source["meta"] = a.meta
}
return source, nil
}