forked from mattbaird/elastigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchaggregate.go
226 lines (187 loc) · 4.66 KB
/
searchaggregate.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package elastigo
import "encoding/json"
func Aggregate(name string) *AggregateDsl {
return &AggregateDsl{Name: name}
}
type AggregateDsl struct {
Name string
TypeName string
Type interface{}
Filters *FilterWrap `json:"filters,omitempty"`
AggregatesVal map[string]*AggregateDsl `json:"aggregations,omitempty"`
}
type FieldAggregate struct {
Field string `json:"field"`
Size *int `json:"size,omitempty"`
}
/**
* Aggregates accepts n "sub-aggregates" to be applied to this aggregate
*
* agg := Aggregate("user").Term("user_id")
* agg.Aggregates(
* Aggregate("total_spent").Sum("price"),
* Aggregate("total_saved").Sum("discount"),
* )
*/
func (d *AggregateDsl) Aggregates(aggs ...*AggregateDsl) *AggregateDsl {
if len(aggs) < 1 {
return d
}
if len(d.AggregatesVal) == 0 {
d.AggregatesVal = make(map[string]*AggregateDsl)
}
for _, agg := range aggs {
d.AggregatesVal[agg.Name] = agg
}
return d
}
func (d *AggregateDsl) Min(field string) *AggregateDsl {
d.Type = FieldAggregate{Field: field}
d.TypeName = "min"
return d
}
func (d *AggregateDsl) Max(field string) *AggregateDsl {
d.Type = FieldAggregate{Field: field}
d.TypeName = "max"
return d
}
func (d *AggregateDsl) Sum(field string) *AggregateDsl {
d.Type = FieldAggregate{Field: field}
d.TypeName = "sum"
return d
}
func (d *AggregateDsl) Avg(field string) *AggregateDsl {
d.Type = FieldAggregate{Field: field}
d.TypeName = "avg"
return d
}
func (d *AggregateDsl) Stats(field string) *AggregateDsl {
d.Type = FieldAggregate{Field: field}
d.TypeName = "stats"
return d
}
func (d *AggregateDsl) ExtendedStats(field string) *AggregateDsl {
d.Type = FieldAggregate{Field: field}
d.TypeName = "extended_stats"
return d
}
func (d *AggregateDsl) ValueCount(field string) *AggregateDsl {
d.Type = FieldAggregate{Field: field}
d.TypeName = "value_count"
return d
}
func (d *AggregateDsl) Percentiles(field string) *AggregateDsl {
d.Type = FieldAggregate{Field: field}
d.TypeName = "percentiles"
return d
}
type Cardinality struct {
Field string `json:"field"`
PrecisionThreshold float64 `json:"precision_threshold,omitempty"`
Rehash bool `json:"rehash,omitempty"`
}
/**
* Cardinality(
* "field_name",
* true,
* 0,
* )
*/
func (d *AggregateDsl) Cardinality(field string, rehash bool, threshold int) *AggregateDsl {
c := Cardinality{Field: field}
// Only set if it's false, since the default is true
if !rehash {
c.Rehash = false
}
if threshold > 0 {
c.PrecisionThreshold = float64(threshold)
}
d.Type = c
d.TypeName = "cardinality"
return d
}
func (d *AggregateDsl) Global() *AggregateDsl {
d.Type = struct{}{}
d.TypeName = "global"
return d
}
func (d *AggregateDsl) Filter(filters ...interface{}) *AggregateDsl {
if len(filters) == 0 {
return d
}
if d.Filters == nil {
d.Filters = NewFilterWrap()
}
d.Filters.addFilters(filters)
return d
}
func (d *AggregateDsl) Missing(field string) *AggregateDsl {
d.Type = FieldAggregate{Field: field}
d.TypeName = "missing"
return d
}
func (d *AggregateDsl) Terms(field string) *AggregateDsl {
d.Type = FieldAggregate{Field: field}
d.TypeName = "terms"
return d
}
func (d *AggregateDsl) TermsWithSize(field string, size int) *AggregateDsl {
d.Type = FieldAggregate{Field: field, Size: &size}
d.TypeName = "terms"
return d
}
func (d *AggregateDsl) SignificantTerms(field string) *AggregateDsl {
d.Type = FieldAggregate{Field: field}
d.TypeName = "significant_terms"
return d
}
type Histogram struct {
Field string `json:"field"`
Interval float64 `json:"interval"`
}
func (d *AggregateDsl) Histogram(field string, interval int) *AggregateDsl {
d.Type = Histogram{
Field: field,
Interval: float64(interval),
}
d.TypeName = "histogram"
return d
}
type DateHistogram struct {
Field string `json:"field"`
Interval string `json:"interval"`
}
func (d *AggregateDsl) DateHistogram(field, interval string) *AggregateDsl {
d.Type = DateHistogram{
Field: field,
Interval: interval,
}
d.TypeName = "date_histogram"
return d
}
func (d *AggregateDsl) MarshalJSON() ([]byte, error) {
return json.Marshal(d.toMap())
}
func (d *AggregateDsl) toMap() map[string]interface{} {
root := map[string]interface{}{}
if d.Type != nil {
root[d.TypeName] = d.Type
}
aggregates := d.aggregatesMap()
if d.Filters != nil {
root["filter"] = d.Filters
}
if len(aggregates) > 0 {
root["aggregations"] = aggregates
}
return root
}
func (d *AggregateDsl) aggregatesMap() map[string]interface{} {
root := map[string]interface{}{}
if len(d.AggregatesVal) > 0 {
for _, agg := range d.AggregatesVal {
root[agg.Name] = agg.toMap()
}
}
return root
}