-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaggs_pipeline_bucket_script-helpers.go
108 lines (91 loc) · 3.11 KB
/
aggs_pipeline_bucket_script-helpers.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
package aggretastic
import (
"fmt"
"github.com/olivere/elastic/v7"
)
// declare an elastic script for every math operation
var (
addScript = elastic.NewScript("params.a + params.b")
subtractScript = elastic.NewScript("params.a - params.b")
multiplyScript = elastic.NewScript("params.a * params.b")
divideScript = elastic.NewScript("params.a / params.b")
percentScript = elastic.NewScript("(params.a / params.b) * 100")
valScript = elastic.NewScript("params.a")
)
// BucketsPath consists bucket's paths
type BucketsPath map[string]string
// BucketScriptAddAggregation performs math plus operation
func BucketScriptAddAggregation(a, b string) *BucketScriptAggregation {
return newBucketScriptAggregation(BucketsPath{
"a": a,
"b": b,
}, addScript)
}
// BucketScriptSubtractAggregation performs math minus operation
func BucketScriptSubtractAggregation(a, b string) *BucketScriptAggregation {
return newBucketScriptAggregation(BucketsPath{
"a": a,
"b": b,
}, subtractScript)
}
// BucketScriptMultiplyAggregation performs math multiply operation
func BucketScriptMultiplyAggregation(a, b string) *BucketScriptAggregation {
return newBucketScriptAggregation(BucketsPath{
"a": a,
"b": b,
}, multiplyScript)
}
// BucketScriptDivideAggregation performs math division operation
func BucketScriptDivideAggregation(a, b string) *BucketScriptAggregation {
return newBucketScriptAggregation(BucketsPath{
"a": a,
"b": b,
}, divideScript)
}
// BucketScriptPercentAggregation performs math percent operation
func BucketScriptPercentAggregation(a, b string) *BucketScriptAggregation {
return newBucketScriptAggregation(BucketsPath{
"a": a,
"b": b,
}, percentScript)
}
// BucketScriptValAggregation performs simple equal operation (just return the value)
func BucketScriptValAggregation(a string) *BucketScriptAggregation {
return newBucketScriptAggregation(BucketsPath{
"a": a,
}, valScript)
}
// BucketScriptNumDivideAggregation performs math divide (between bucket value and number) operation
func BucketScriptNumDivideAggregation(a string, num float64) *BucketScriptAggregation {
return newBucketScriptAggregation(BucketsPath{
"a": a,
}, elastic.NewScript("params.a / "+fmt.Sprintf("%f", num)))
}
// newBucketScriptAggregation is a private function, constructor of elastic.BucketScriptAggregation
func newBucketScriptAggregation(bucketPaths BucketsPath, script *elastic.Script) *BucketScriptAggregation {
bsa := NewBucketScriptAggregation()
for k, v := range bucketPaths {
bsa = bsa.AddBucketsPath(k, v)
}
bsa = bsa.Script(script)
return bsa
}
// RewriteBucketScriptPath allows to rewrite the bucket script path map
func RewriteBucketScriptPath(agg *BucketScriptAggregation, rewriter func(string) (string, bool)) {
for k, v := range agg.bucketsPathsMap {
rewrited, deleted := rewriter(v)
if deleted {
delete(agg.bucketsPathsMap, k)
} else {
agg.bucketsPathsMap[k] = rewrited
}
}
}
func IsBucketScriptAggregation(agg Aggregation) (ok bool) {
_, ok = agg.(*BucketScriptAggregation)
return
}
func IsBucketSortAggregation(agg Aggregation) (ok bool) {
_, ok = agg.(*BucketSortAggregation)
return
}