-
Notifications
You must be signed in to change notification settings - Fork 5
/
elasticsearch-aggregator.go
191 lines (159 loc) · 5.06 KB
/
elasticsearch-aggregator.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package backends
// this file satifies the Aggregator interface for ElasticsearchIndexer
import (
"encoding/json"
"fmt"
"github.com/ghetzel/go-stockutil/httputil"
"github.com/ghetzel/go-stockutil/maputil"
"github.com/ghetzel/go-stockutil/typeutil"
"github.com/ghetzel/pivot/v3/dal"
"github.com/ghetzel/pivot/v3/filter"
"github.com/ghetzel/pivot/v3/filter/generators"
)
type esAggregationQuery struct {
Aggregations map[string]esAggregation `json:"aggs"`
Query map[string]interface{} `json:"query,omitempty"`
Size int `json:"size"`
From int `json:"from"`
Sort []string `json:"sort,omitempty"`
}
type esAggregation map[string]interface{}
func (self *ElasticsearchIndexer) Sum(collection *dal.Collection, field string, f ...*filter.Filter) (float64, error) {
return self.aggregateFloat(collection, filter.Sum, field, f)
}
func (self *ElasticsearchIndexer) Count(collection *dal.Collection, flt ...*filter.Filter) (uint64, error) {
var f *filter.Filter
if len(flt) > 0 {
f = flt[0]
}
if query, err := filter.Render(
generators.NewElasticsearchGenerator(),
collection.GetAggregatorName(),
f,
); err == nil {
var q = maputil.M(query)
q.Delete(`size`)
q.Delete(`from`)
q.Delete(`sort`)
if res, err := self.client.GetWithBody(
fmt.Sprintf("/%s/_doc/_count", collection.GetAggregatorName()),
httputil.Literal(q.JSON()),
nil,
nil,
); err == nil {
var rv map[string]interface{}
if err := self.client.Decode(res.Body, &rv); err == nil {
return uint64(typeutil.Int(rv[`count`])), nil
} else {
return 0, err
}
} else {
return 0, err
}
} else {
return 0, err
}
}
func (self *ElasticsearchIndexer) Minimum(collection *dal.Collection, field string, f ...*filter.Filter) (float64, error) {
return self.aggregateFloat(collection, filter.Minimum, field, f)
}
func (self *ElasticsearchIndexer) Maximum(collection *dal.Collection, field string, f ...*filter.Filter) (float64, error) {
return self.aggregateFloat(collection, filter.Maximum, field, f)
}
func (self *ElasticsearchIndexer) Average(collection *dal.Collection, field string, f ...*filter.Filter) (float64, error) {
return self.aggregateFloat(collection, filter.Average, field, f)
}
func (self *ElasticsearchIndexer) GroupBy(collection *dal.Collection, groupBy []string, aggregates []filter.Aggregate, flt ...*filter.Filter) (*dal.RecordSet, error) {
if result, err := self.aggregate(collection, groupBy, aggregates, flt, false); err == nil {
return result.(*dal.RecordSet), nil
} else {
return nil, err
}
}
func (self *ElasticsearchIndexer) aggregateFloat(collection *dal.Collection, aggregation filter.Aggregation, field string, flt []*filter.Filter) (float64, error) {
if result, err := self.aggregate(collection, nil, []filter.Aggregate{
{
Aggregation: aggregation,
Field: field,
},
}, flt, true); err == nil {
var aggkey string
switch aggregation {
case filter.Minimum:
aggkey = `min`
case filter.Maximum:
aggkey = `max`
case filter.Sum:
aggkey = `sum`
case filter.Average:
aggkey = `avg`
}
if aggkey != `` {
return maputil.M(result).Float(`aggregations.` + field + `.` + aggkey), nil
} else {
return 0, fmt.Errorf("unknown aggregation")
}
} else {
return 0, err
}
}
func (self *ElasticsearchIndexer) aggregate(collection *dal.Collection, groupBy []string, aggregates []filter.Aggregate, flt []*filter.Filter, single bool) (interface{}, error) {
var f *filter.Filter
if len(flt) > 0 {
f = flt[0]
}
if query, err := filter.Render(
generators.NewElasticsearchGenerator(),
collection.GetAggregatorName(),
f,
); err == nil {
var esFilter map[string]interface{}
if err := json.Unmarshal(query, &esFilter); err == nil {
var aggs = esAggregationQuery{
Aggregations: make(map[string]esAggregation),
}
for _, aggregate := range aggregates {
var statsKey = aggregate.Field
var statsField esAggregation
if s, ok := aggs.Aggregations[statsKey]; ok {
statsField = s
} else {
statsField = make(esAggregation)
}
statsField[`stats`] = map[string]interface{}{
`field`: aggregate.Field,
}
aggs.Aggregations[statsKey] = statsField
}
if len(esFilter) > 0 {
aggs.Query = maputil.M(esFilter).Get(`query`).MapNative()
aggs.Size = 0
}
if response, err := self.client.GetWithBody(
fmt.Sprintf("/%s/_search", collection.GetAggregatorName()),
&aggs,
nil,
nil,
); err == nil {
var output = make(map[string]interface{})
if err := self.client.Decode(response.Body, &output); err == nil {
return output, nil
} else {
return nil, fmt.Errorf("response decode error: %v", err)
}
} else {
return nil, err
}
} else {
return nil, fmt.Errorf("filter encode error: %v", err)
}
} else {
return nil, fmt.Errorf("filter error: %v", err)
}
}
func (self *ElasticsearchIndexer) AggregatorConnectionString() *dal.ConnectionString {
return self.conn
}
func (self *ElasticsearchIndexer) AggregatorInitialize(parent Backend) error {
return nil
}