forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_aggs_bucket_composite.go
498 lines (421 loc) · 14.3 KB
/
search_aggs_bucket_composite.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
// CompositeAggregation is a multi-bucket values source based aggregation
// that can be used to calculate unique composite values from source documents.
//
// See https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-aggregations-bucket-composite-aggregation.html
// for details.
type CompositeAggregation struct {
after map[string]interface{}
size *int
sources []CompositeAggregationValuesSource
subAggregations map[string]Aggregation
meta map[string]interface{}
}
// NewCompositeAggregation creates a new CompositeAggregation.
func NewCompositeAggregation() *CompositeAggregation {
return &CompositeAggregation{
sources: make([]CompositeAggregationValuesSource, 0),
subAggregations: make(map[string]Aggregation),
}
}
// Size represents the number of composite buckets to return.
// Defaults to 10 as of Elasticsearch 6.1.
func (a *CompositeAggregation) Size(size int) *CompositeAggregation {
a.size = &size
return a
}
// AggregateAfter sets the values that indicate which composite bucket this
// request should "aggregate after".
func (a *CompositeAggregation) AggregateAfter(after map[string]interface{}) *CompositeAggregation {
a.after = after
return a
}
// Sources specifies the list of CompositeAggregationValuesSource instances to
// use in the aggregation.
func (a *CompositeAggregation) Sources(sources ...CompositeAggregationValuesSource) *CompositeAggregation {
a.sources = append(a.sources, sources...)
return a
}
// SubAggregations of this aggregation.
func (a *CompositeAggregation) SubAggregation(name string, subAggregation Aggregation) *CompositeAggregation {
a.subAggregations[name] = subAggregation
return a
}
// Meta sets the meta data to be included in the aggregation response.
func (a *CompositeAggregation) Meta(metaData map[string]interface{}) *CompositeAggregation {
a.meta = metaData
return a
}
// Source returns the serializable JSON for this aggregation.
func (a *CompositeAggregation) Source() (interface{}, error) {
// Example:
// {
// "aggs" : {
// "my_composite_agg" : {
// "composite" : {
// "sources": [
// {"my_term": { "terms": { "field": "product" }}},
// {"my_histo": { "histogram": { "field": "price", "interval": 5 }}},
// {"my_date": { "date_histogram": { "field": "timestamp", "interval": "1d" }}},
// ],
// "size" : 10,
// "after" : ["a", 2, "c"]
// }
// }
// }
// }
//
// This method returns only the { "histogram" : { ... } } part.
source := make(map[string]interface{})
opts := make(map[string]interface{})
source["composite"] = opts
sources := make([]interface{}, len(a.sources))
for i, s := range a.sources {
src, err := s.Source()
if err != nil {
return nil, err
}
sources[i] = src
}
opts["sources"] = sources
if a.size != nil {
opts["size"] = *a.size
}
if a.after != nil {
opts["after"] = a.after
}
// 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
}
// -- Generic interface for CompositeAggregationValues --
// CompositeAggregationValuesSource specifies the interface that
// all implementations for CompositeAggregation's Sources method
// need to implement.
//
// The different implementations are described in
// https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-aggregations-bucket-composite-aggregation.html#_values_source_2.
type CompositeAggregationValuesSource interface {
Source() (interface{}, error)
}
// -- CompositeAggregationTermsValuesSource --
// CompositeAggregationTermsValuesSource is a source for the CompositeAggregation that handles terms
// it works very similar to a terms aggregation with slightly different syntax
//
// See https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-aggregations-bucket-composite-aggregation.html#_terms
// for details.
type CompositeAggregationTermsValuesSource struct {
name string
field string
script *Script
valueType string
missing interface{}
order string
}
// NewCompositeAggregationTermsValuesSource creates and initializes
// a new CompositeAggregationTermsValuesSource.
func NewCompositeAggregationTermsValuesSource(name string) *CompositeAggregationTermsValuesSource {
return &CompositeAggregationTermsValuesSource{
name: name,
}
}
// Field to use for this source.
func (a *CompositeAggregationTermsValuesSource) Field(field string) *CompositeAggregationTermsValuesSource {
a.field = field
return a
}
// Script to use for this source.
func (a *CompositeAggregationTermsValuesSource) Script(script *Script) *CompositeAggregationTermsValuesSource {
a.script = script
return a
}
// ValueType specifies the type of values produced by this source,
// e.g. "string" or "date".
func (a *CompositeAggregationTermsValuesSource) ValueType(valueType string) *CompositeAggregationTermsValuesSource {
a.valueType = valueType
return a
}
// Order specifies the order in the values produced by this source.
// It can be either "asc" or "desc".
func (a *CompositeAggregationTermsValuesSource) Order(order string) *CompositeAggregationTermsValuesSource {
a.order = order
return a
}
// Asc ensures the order of the values produced is ascending.
func (a *CompositeAggregationTermsValuesSource) Asc() *CompositeAggregationTermsValuesSource {
a.order = "asc"
return a
}
// Desc ensures the order of the values produced is descending.
func (a *CompositeAggregationTermsValuesSource) Desc() *CompositeAggregationTermsValuesSource {
a.order = "desc"
return a
}
// Missing specifies the value to use when the source finds a missing
// value in a document.
func (a *CompositeAggregationTermsValuesSource) Missing(missing interface{}) *CompositeAggregationTermsValuesSource {
a.missing = missing
return a
}
// Source returns the serializable JSON for this values source.
func (a *CompositeAggregationTermsValuesSource) Source() (interface{}, error) {
source := make(map[string]interface{})
name := make(map[string]interface{})
source[a.name] = name
values := make(map[string]interface{})
name["terms"] = values
// field
if a.field != "" {
values["field"] = a.field
}
// script
if a.script != nil {
src, err := a.script.Source()
if err != nil {
return nil, err
}
values["script"] = src
}
// missing
if a.missing != nil {
values["missing"] = a.missing
}
// value_type
if a.valueType != "" {
values["value_type"] = a.valueType
}
// order
if a.order != "" {
values["order"] = a.order
}
return source, nil
}
// -- CompositeAggregationHistogramValuesSource --
// CompositeAggregationHistogramValuesSource is a source for the CompositeAggregation that handles histograms
// it works very similar to a terms histogram with slightly different syntax
//
// See https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-aggregations-bucket-composite-aggregation.html#_histogram
// for details.
type CompositeAggregationHistogramValuesSource struct {
name string
field string
script *Script
valueType string
missing interface{}
order string
interval float64
}
// NewCompositeAggregationHistogramValuesSource creates and initializes
// a new CompositeAggregationHistogramValuesSource.
func NewCompositeAggregationHistogramValuesSource(name string, interval float64) *CompositeAggregationHistogramValuesSource {
return &CompositeAggregationHistogramValuesSource{
name: name,
interval: interval,
}
}
// Field to use for this source.
func (a *CompositeAggregationHistogramValuesSource) Field(field string) *CompositeAggregationHistogramValuesSource {
a.field = field
return a
}
// Script to use for this source.
func (a *CompositeAggregationHistogramValuesSource) Script(script *Script) *CompositeAggregationHistogramValuesSource {
a.script = script
return a
}
// ValueType specifies the type of values produced by this source,
// e.g. "string" or "date".
func (a *CompositeAggregationHistogramValuesSource) ValueType(valueType string) *CompositeAggregationHistogramValuesSource {
a.valueType = valueType
return a
}
// Missing specifies the value to use when the source finds a missing
// value in a document.
func (a *CompositeAggregationHistogramValuesSource) Missing(missing interface{}) *CompositeAggregationHistogramValuesSource {
a.missing = missing
return a
}
// Order specifies the order in the values produced by this source.
// It can be either "asc" or "desc".
func (a *CompositeAggregationHistogramValuesSource) Order(order string) *CompositeAggregationHistogramValuesSource {
a.order = order
return a
}
// Asc ensures the order of the values produced is ascending.
func (a *CompositeAggregationHistogramValuesSource) Asc() *CompositeAggregationHistogramValuesSource {
a.order = "asc"
return a
}
// Desc ensures the order of the values produced is descending.
func (a *CompositeAggregationHistogramValuesSource) Desc() *CompositeAggregationHistogramValuesSource {
a.order = "desc"
return a
}
// Interval specifies the interval to use.
func (a *CompositeAggregationHistogramValuesSource) Interval(interval float64) *CompositeAggregationHistogramValuesSource {
a.interval = interval
return a
}
// Source returns the serializable JSON for this values source.
func (a *CompositeAggregationHistogramValuesSource) Source() (interface{}, error) {
source := make(map[string]interface{})
name := make(map[string]interface{})
source[a.name] = name
values := make(map[string]interface{})
name["histogram"] = values
// field
if a.field != "" {
values["field"] = a.field
}
// script
if a.script != nil {
src, err := a.script.Source()
if err != nil {
return nil, err
}
values["script"] = src
}
// missing
if a.missing != nil {
values["missing"] = a.missing
}
// value_type
if a.valueType != "" {
values["value_type"] = a.valueType
}
// order
if a.order != "" {
values["order"] = a.order
}
// Histogram-related properties
values["interval"] = a.interval
return source, nil
}
// -- CompositeAggregationDateHistogramValuesSource --
// CompositeAggregationDateHistogramValuesSource is a source for the CompositeAggregation that handles date histograms
// it works very similar to a date histogram aggregation with slightly different syntax
//
// See https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-aggregations-bucket-composite-aggregation.html#_date_histogram
// for details.
type CompositeAggregationDateHistogramValuesSource struct {
name string
field string
script *Script
valueType string
missing interface{}
order string
interval interface{}
timeZone string
}
// NewCompositeAggregationDateHistogramValuesSource creates and initializes
// a new CompositeAggregationDateHistogramValuesSource.
func NewCompositeAggregationDateHistogramValuesSource(name string, interval interface{}) *CompositeAggregationDateHistogramValuesSource {
return &CompositeAggregationDateHistogramValuesSource{
name: name,
interval: interval,
}
}
// Field to use for this source.
func (a *CompositeAggregationDateHistogramValuesSource) Field(field string) *CompositeAggregationDateHistogramValuesSource {
a.field = field
return a
}
// Script to use for this source.
func (a *CompositeAggregationDateHistogramValuesSource) Script(script *Script) *CompositeAggregationDateHistogramValuesSource {
a.script = script
return a
}
// ValueType specifies the type of values produced by this source,
// e.g. "string" or "date".
func (a *CompositeAggregationDateHistogramValuesSource) ValueType(valueType string) *CompositeAggregationDateHistogramValuesSource {
a.valueType = valueType
return a
}
// Missing specifies the value to use when the source finds a missing
// value in a document.
func (a *CompositeAggregationDateHistogramValuesSource) Missing(missing interface{}) *CompositeAggregationDateHistogramValuesSource {
a.missing = missing
return a
}
// Order specifies the order in the values produced by this source.
// It can be either "asc" or "desc".
func (a *CompositeAggregationDateHistogramValuesSource) Order(order string) *CompositeAggregationDateHistogramValuesSource {
a.order = order
return a
}
// Asc ensures the order of the values produced is ascending.
func (a *CompositeAggregationDateHistogramValuesSource) Asc() *CompositeAggregationDateHistogramValuesSource {
a.order = "asc"
return a
}
// Desc ensures the order of the values produced is descending.
func (a *CompositeAggregationDateHistogramValuesSource) Desc() *CompositeAggregationDateHistogramValuesSource {
a.order = "desc"
return a
}
// Interval to use for the date histogram, e.g. "1d" or a numeric value like "60".
func (a *CompositeAggregationDateHistogramValuesSource) Interval(interval interface{}) *CompositeAggregationDateHistogramValuesSource {
a.interval = interval
return a
}
// TimeZone to use for the dates.
func (a *CompositeAggregationDateHistogramValuesSource) TimeZone(timeZone string) *CompositeAggregationDateHistogramValuesSource {
a.timeZone = timeZone
return a
}
// Source returns the serializable JSON for this values source.
func (a *CompositeAggregationDateHistogramValuesSource) Source() (interface{}, error) {
source := make(map[string]interface{})
name := make(map[string]interface{})
source[a.name] = name
values := make(map[string]interface{})
name["date_histogram"] = values
// field
if a.field != "" {
values["field"] = a.field
}
// script
if a.script != nil {
src, err := a.script.Source()
if err != nil {
return nil, err
}
values["script"] = src
}
// missing
if a.missing != nil {
values["missing"] = a.missing
}
// value_type
if a.valueType != "" {
values["value_type"] = a.valueType
}
// order
if a.order != "" {
values["order"] = a.order
}
// DateHistogram-related properties
values["interval"] = a.interval
// timeZone
if a.timeZone != "" {
values["time_zone"] = a.timeZone
}
return source, nil
}