-
Notifications
You must be signed in to change notification settings - Fork 1
/
aggs_metrics_weighted_avg.go
159 lines (136 loc) · 3.81 KB
/
aggs_metrics_weighted_avg.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package aggretastic
import "github.com/olivere/elastic/v7"
// WeightedAvgAggregation is a single-value metrics aggregation that
// computes the weighted average of numeric values that are extracted
// from the aggregated documents. These values can be extracted either
// from specific numeric fields in the documents.
//
// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-weight-avg-aggregation.html
type WeightedAvgAggregation struct {
*tree
fields map[string]*MultiValuesSourceFieldConfig
valueType string
format string
value *MultiValuesSourceFieldConfig
weight *MultiValuesSourceFieldConfig
subAggregations map[string]Aggregation
meta map[string]interface{}
}
func NewWeightedAvgAggregation() *WeightedAvgAggregation {
a := &WeightedAvgAggregation{
fields: make(map[string]*MultiValuesSourceFieldConfig),
subAggregations: make(map[string]Aggregation),
}
a.tree = nilAggregationTree(a)
return a
}
func (a *WeightedAvgAggregation) Field(field string, config *MultiValuesSourceFieldConfig) *WeightedAvgAggregation {
a.fields[field] = config
return a
}
func (a *WeightedAvgAggregation) ValueType(valueType string) *WeightedAvgAggregation {
a.valueType = valueType
return a
}
func (a *WeightedAvgAggregation) Format(format string) *WeightedAvgAggregation {
a.format = format
return a
}
func (a *WeightedAvgAggregation) Value(value *MultiValuesSourceFieldConfig) *WeightedAvgAggregation {
a.value = value
return a
}
func (a *WeightedAvgAggregation) Weight(weight *MultiValuesSourceFieldConfig) *WeightedAvgAggregation {
a.weight = weight
return a
}
func (a *WeightedAvgAggregation) SubAggregation(name string, subAggregation Aggregation) *WeightedAvgAggregation {
a.subAggregations[name] = subAggregation
return a
}
// Meta sets the meta data to be included in the aggregation response.
func (a *WeightedAvgAggregation) Meta(metaData map[string]interface{}) *WeightedAvgAggregation {
a.meta = metaData
return a
}
func (a *WeightedAvgAggregation) Source() (interface{}, error) {
source := make(map[string]interface{})
opts := make(map[string]interface{})
source["weighted_avg"] = opts
if len(a.fields) > 0 {
f := make(map[string]interface{})
for name, config := range a.fields {
cfg, err := config.Source()
if err != nil {
return nil, err
}
f[name] = cfg
}
opts["fields"] = f
}
if v := a.format; v != "" {
opts["format"] = v
}
if v := a.valueType; v != "" {
opts["value_type"] = v
}
if v := a.value; v != nil {
cfg, err := v.Source()
if err != nil {
return nil, err
}
opts["value"] = cfg
}
if v := a.weight; v != nil {
cfg, err := v.Source()
if err != nil {
return nil, err
}
opts["weight"] = cfg
}
// AggregationBuilder (SubAggregations)
if len(a.subAggregations) > 0 {
aggsMap := make(map[string]interface{})
source["aggregations"] = aggsMap
for name, aggregate := range a.subAggregations {
src, err := aggregate.Source()
if err != nil {
return nil, err
}
aggsMap[name] = src
}
}
// Add Meta data if available
if len(a.meta) > 0 {
source["meta"] = a.meta
}
return source, nil
}
// MultiValuesSourceFieldConfig represents a field configuration
// used e.g. in WeightedAvgAggregation.
type MultiValuesSourceFieldConfig struct {
FieldName string
Missing interface{}
Script *elastic.Script
TimeZone string
}
func (f *MultiValuesSourceFieldConfig) Source() (interface{}, error) {
source := make(map[string]interface{})
if v := f.Missing; v != nil {
source["missing"] = v
}
if v := f.Script; v != nil {
src, err := v.Source()
if err != nil {
return nil, err
}
source["script"] = src
}
if v := f.FieldName; v != "" {
source["field"] = v
}
if v := f.TimeZone; v != "" {
source["time_zone"] = v
}
return source, nil
}