stretchy-model 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,758 @@
1
+ module Stretchy
2
+ module Relations
3
+ module AggregationMethods
4
+
5
+ AGGREGATION_METHODS = [
6
+ :aggregation,
7
+ :avg,
8
+ :bucket_script,
9
+ :bucket_selector,
10
+ :bucket_sort,
11
+ :cardinality,
12
+ :children,
13
+ :composite,
14
+ :date_histogram,
15
+ :date_range,
16
+ :extended_stats,
17
+ :filter, # filter is a query method
18
+ :filters,
19
+ :geo_bounds,
20
+ :geo_centroid,
21
+ :global,
22
+ :histogram,
23
+ :ip_range,
24
+ :max,
25
+ :min,
26
+ :missing,
27
+ :nested,
28
+ :percentile_ranks,
29
+ :percentiles,
30
+ :range,
31
+ :reverse_nested,
32
+ :sampler,
33
+ :scripted_metric,
34
+ :significant_terms,
35
+ :stats,
36
+ :sum,
37
+ :terms,
38
+ :top_hits,
39
+ :top_metrics,
40
+ :value_count,
41
+ :weighted_avg
42
+ ].freeze
43
+
44
+ # Adds an aggregation to the query.
45
+ #
46
+ # @param name [Symbol, String] the name of the aggregation
47
+ # @param options [Hash] a hash of options for the aggregation
48
+ # @param block [Proc] an optional block to further configure the aggregation
49
+ #
50
+ # @example
51
+ # Model.aggregation(:avg_price, field: :price)
52
+ # Model.aggregation(:price_ranges) do
53
+ # range field: :price, ranges: [{to: 100}, {from: 100, to: 200}, {from: 200}]
54
+ # end
55
+ #
56
+ # Aggregation results are available in the `aggregations` method of the results under name provided in the aggregation.
57
+ #
58
+ # @example
59
+ # results = Model.where(color: :blue).aggregation(:avg_price, field: :price)
60
+ # results.aggregations.avg_price
61
+ #
62
+ # @return [Stretchy::Relation] a new relation
63
+ def aggregation(name, options = {}, &block)
64
+ spawn.aggregation!(name, options, &block)
65
+ end
66
+
67
+ def aggregation!(name, options = {}, &block) # :nodoc:
68
+ self.aggregation_values += [{name: name, args: assume_keyword_field(options)}]
69
+ self
70
+ end
71
+
72
+
73
+
74
+ # Public: Perform an avg aggregation.
75
+ #
76
+ # name - The Symbol or String name of the aggregation.
77
+ # options - The Hash options used to refine the aggregation (default: {}):
78
+ # :field - The field to calculate the average on.
79
+ # aggs - The Hash of nested aggregations.
80
+ #
81
+ # Examples
82
+ #
83
+ # Model.aggregation(:average_price, field: :price)
84
+ #
85
+ # Returns a new Stretchy::Relation.
86
+ def avg(name, options = {}, *aggs)
87
+ options = {avg: options}.merge(*aggs)
88
+ aggregation(name, options)
89
+ end
90
+
91
+ # Public: Perform a bucket_script aggregation.
92
+ #
93
+ # name - The Symbol or String name of the aggregation.
94
+ # options - The Hash options used to refine the aggregation (default: {}):
95
+ # :buckets_path - The paths to the buckets.
96
+ # :script - The script to execute.
97
+ # aggs - The Hash of nested aggregations.
98
+ #
99
+ # Examples
100
+ #
101
+ # Model.aggregation(:total_sales, script: "params.tShirtsSold * params.price", buckets_path: {tShirtsSold: "tShirtsSold", price: "price"})
102
+ #
103
+ # Returns a new Stretchy::Relation.
104
+ def bucket_script(name, options = {}, *aggs)
105
+ options = {bucket_script: options}.merge(*aggs)
106
+ aggregation(name, options)
107
+ end
108
+
109
+ # Public: Perform a bucket_selector aggregation.
110
+ #
111
+ # name - The Symbol or String name of the aggregation.
112
+ # options - The Hash options used to refine the aggregation (default: {}):
113
+ # :script - The script to determine whether the current bucket will be retained.
114
+ # aggs - The Hash of nested aggregations.
115
+ #
116
+ # Examples
117
+ #
118
+ # Model.aggregation(:sales_bucket_filter, script: "params.totalSales > 200", buckets_path: {totalSales: "totalSales"})
119
+ #
120
+ # Returns a new Stretchy::Relation.
121
+ def bucket_selector(name, options = {}, *aggs)
122
+ options = {bucket_selector: options}.merge(*aggs)
123
+ aggregation(name, options)
124
+ end
125
+
126
+ # Public: Perform a bucket_sort aggregation.
127
+ #
128
+ # name - The Symbol or String name of the aggregation.
129
+ # options - The Hash options used to refine the aggregation (default: {}):
130
+ # :field - The field to sort on.
131
+ # aggs - The Hash of nested aggregations.
132
+ #
133
+ # Examples
134
+ #
135
+ # Model.bucket_sort(:my_agg, {field: 'my_field'})
136
+ # Model.bucket_sort(:my_agg, {field: 'my_field'}, aggs: {...})
137
+ #
138
+ # Returns a new Stretchy::Relation.
139
+ def bucket_sort(name, options = {}, *aggs)
140
+ options = {bucket_sort: options}.merge(*aggs)
141
+ aggregation(name, options)
142
+ end
143
+
144
+ # Public: Perform a cardinality aggregation.
145
+ #
146
+ # name - The Symbol or String name of the aggregation.
147
+ # options - The Hash options used to refine the aggregation (default: {}):
148
+ # :field - The field to perform the aggregation on.
149
+ # aggs - The Hash of nested aggregations.
150
+ #
151
+ # Examples
152
+ #
153
+ # Model.cardinality(:unique_names, {field: 'names'})
154
+ # Model.cardinality(:unique_names, {field: 'names'}, aggs: {...})
155
+ #
156
+ # Returns a new Stretchy::Relation.
157
+ def cardinality(name, options = {}, *aggs)
158
+ options = {cardinality: options}.merge(*aggs)
159
+ aggregation(name, options)
160
+ end
161
+
162
+ # Public: Perform a children aggregation.
163
+ #
164
+ # name - The Symbol or String name of the aggregation.
165
+ # options - The Hash options used to refine the aggregation (default: {}):
166
+ # :type - The type of children to aggregate.
167
+ # aggs - The Hash of nested aggregations.
168
+ #
169
+ # Examples
170
+ #
171
+ # Model.children(:my_agg, {type: 'my_type'})
172
+ # Model.children(:my_agg, {type: 'my_type'}, aggs: {...})
173
+ #
174
+ # Returns a new Stretchy::Relation.
175
+ def children(name, options = {}, *aggs)
176
+ options = {children: options}.merge(*aggs)
177
+ aggregation(name, options)
178
+ end
179
+
180
+ # Public: Perform a composite aggregation.
181
+ #
182
+ # name - The Symbol or String name of the aggregation.
183
+ # options - The Hash options used to refine the aggregation (default: {}):
184
+ # :sources - The sources to use for the composite aggregation.
185
+ # :size - The size of the composite aggregation.
186
+ # aggs - The Hash of nested aggregations.
187
+ #
188
+ # Examples
189
+ #
190
+ # Model.composite(:my_agg, {sources: [...], size: 100})
191
+ # Model.composite(:my_agg, {sources: [...], size: 100}, aggs: {...})
192
+ #
193
+ # Returns a new Stretchy::Relation.
194
+ def composite(name, options = {}, *aggs)
195
+ options = {composite: options}.merge(*aggs)
196
+ aggregation(name, options)
197
+ end
198
+
199
+ # Public: Perform a date_histogram aggregation.
200
+ #
201
+ # name - The Symbol or String name of the aggregation.
202
+ # options - The Hash options used to refine the aggregation (default: {}):
203
+ # :field - The field to use for the date_histogram aggregation.
204
+ # :interval - The interval for the date_histogram aggregation.
205
+ # :calendar_interval - The calendar interval for the date_histogram aggregation.
206
+ # :format - The format for the date_histogram aggregation.
207
+ # :time_zone - The time zone for the date_histogram aggregation.
208
+ # :min_doc_count - The minimum document count for the date_histogram aggregation.
209
+ # :extended_bounds - The extended bounds for the date_histogram aggregation.
210
+ # aggs - The Hash of nested aggregations.
211
+ #
212
+ # Examples
213
+ #
214
+ # Model.date_histogram(:my_agg, {field: 'date', interval: 'month', format: 'MM-yyyy', time_zone: 'UTC'})
215
+ # Model.date_histogram(:my_agg, {field: 'date', calendar_interval: :month, format: 'MM-yyyy', time_zone: 'UTC'}, aggs: {...})
216
+ #
217
+ # Returns a new Stretchy::Relation.
218
+ def date_histogram(name, options = {}, *aggs)
219
+ options = {date_histogram: options}.merge(*aggs)
220
+ aggregation(name, options)
221
+ end
222
+
223
+ # Public: Perform a date_range aggregation.
224
+ #
225
+ # name - The Symbol or String name of the aggregation.
226
+ # options - The Hash options used to refine the aggregation (default: {}):
227
+ # :field - The field to use for the date_range aggregation.
228
+ # :format - The format for the date_range aggregation.
229
+ # :time_zone - The time zone for the date_range aggregation.
230
+ # :ranges - The ranges for the date_range aggregation.
231
+ # :keyed - The keyed option for the date_range aggregation.
232
+ # aggs - The Hash of nested aggregations.
233
+ #
234
+ # Examples
235
+ #
236
+ # Model.date_range(:my_agg, {field: 'date', format: 'MM-yyyy', time_zone: 'UTC', ranges: [{to: 'now', from: 'now-1M'}]})
237
+ # Model.date_range(:my_agg, {field: 'date', format: 'MM-yyyy', time_zone: 'UTC', ranges: [{to: 'now', from: 'now-1M'}]}, aggs: {...})
238
+ #
239
+ # Returns a new Stretchy::Relation.
240
+ def date_range(name, options = {}, *aggs)
241
+ options = {date_range: options}.merge(*aggs)
242
+ aggregation(name, options)
243
+ end
244
+ # Public: Perform an extended_stats aggregation.
245
+ #
246
+ # name - The Symbol or String name of the aggregation.
247
+ # options - The Hash options used to refine the aggregation (default: {}):
248
+ # :field - The field to use for the extended_stats aggregation.
249
+ # :sigma - The sigma for the extended_stats aggregation.
250
+ # aggs - The Array of additional nested aggregations (optional).
251
+ #
252
+ # Examples
253
+ #
254
+ # Model.extended_stats(:my_agg, {field: 'field_name', sigma: 1.0})
255
+ # Model.extended_stats(:my_agg, {field: 'field_name', sigma: 1.0}, aggs: {...})
256
+ #
257
+ # Returns a new Stretchy::Relation.
258
+ def extended_stats(name, options = {}, *aggs)
259
+ options = {extended_stats: options}.merge(*aggs)
260
+ aggregation(name, options)
261
+ end
262
+
263
+ # Public: Perform a filter_agg aggregation.
264
+ #
265
+ # name - The Symbol or String name of the aggregation.
266
+ # options - The Hash options used to refine the aggregation (default: {}):
267
+ # :filter - The filter to use for the filter_agg aggregation.
268
+ # aggs - The Array of additional nested aggregations (optional).
269
+ #
270
+ # Examples
271
+ #
272
+ # Model.filter_agg(:my_agg, {filter: {...}})
273
+ # Model.filter_agg(:my_agg, {filter: {...}}, aggs: {...})
274
+ #
275
+ # Returns a new Stretchy::Relation.
276
+ def filter(name, options = {}, *aggs)
277
+ options = {filter: options}.merge(*aggs)
278
+ aggregation(name, options)
279
+ end
280
+
281
+ # Public: Perform a filters aggregation.
282
+ #
283
+ # name - The Symbol or String name of the aggregation.
284
+ # options - The Hash options used to refine the aggregation (default: {}):
285
+ # :filters - The filters to use for the filters aggregation.
286
+ # aggs - The Array of additional nested aggregations (optional).
287
+ #
288
+ # Examples
289
+ #
290
+ # Model.filters(:my_agg, {filters: {...}})
291
+ # Model.filters(:my_agg, {filters: {...}}, aggs: {...})
292
+ #
293
+ # Returns a new Stretchy::Relation.
294
+ def filters(name, options = {}, *aggs)
295
+ options = {filters: options}.merge(*aggs)
296
+ aggregation(name, options)
297
+ end
298
+
299
+ # Public: Perform a geo_bounds aggregation.
300
+ #
301
+ # name - The Symbol or String name of the aggregation.
302
+ # options - The Hash options used to refine the aggregation (default: {}):
303
+ # :field - The field to use for the geo_bounds aggregation.
304
+ # aggs - The Array of additional nested aggregations (optional).
305
+ #
306
+ # Examples
307
+ #
308
+ # Model.geo_bounds(:my_agg, {field: 'location_field'})
309
+ # Model.geo_bounds(:my_agg, {field: 'location_field'}, aggs: {...})
310
+ #
311
+ # Returns a new Stretchy::Relation.
312
+ def geo_bounds(name, options = {}, *aggs)
313
+ options = {geo_bounds: options}.merge(*aggs)
314
+ aggregation(name, options)
315
+ end
316
+
317
+ # Public: Perform a geo_centroid aggregation.
318
+ #
319
+ # name - The Symbol or String name of the aggregation.
320
+ # options - The Hash options used to refine the aggregation (default: {}):
321
+ # :field - The field to use for the geo_centroid aggregation.
322
+ # aggs - The Array of additional nested aggregations (optional).
323
+ #
324
+ # Examples
325
+ #
326
+ # Model.geo_centroid(:my_agg, {field: 'location_field'})
327
+ # Model.geo_centroid(:my_agg, {field: 'location_field'}, aggs: {...})
328
+ #
329
+ # Returns a new Stretchy::Relation.
330
+ def geo_centroid(name, options = {}, *aggs)
331
+ options = {geo_centroid: options}.merge(*aggs)
332
+ aggregation(name, options)
333
+ end
334
+
335
+ # Public: Perform a global aggregation.
336
+ #
337
+ # name - The Symbol or String name of the aggregation.
338
+ # options - The Hash options used to refine the aggregation (default: {}).
339
+ # aggs - The Array of additional nested aggregations (optional).
340
+ #
341
+ # Examples
342
+ #
343
+ # Model.global(:my_agg)
344
+ # Model.global(:my_agg, {}, aggs: {...})
345
+ #
346
+ # Returns a new Stretchy::Relation.
347
+ def global(name, options = {}, *aggs)
348
+ options = {global: options}.merge(*aggs)
349
+ aggregation(name, options)
350
+ end
351
+ # Public: Perform a histogram aggregation.
352
+ #
353
+ # name - The Symbol or String name of the aggregation.
354
+ # options - The Hash options used to refine the aggregation (default: {}):
355
+ # :field - The field to use for the histogram aggregation.
356
+ # :interval - The interval for the histogram aggregation.
357
+ # :min_doc_count - The minimum document count for the histogram aggregation.
358
+ # :extended_bounds - The extended bounds for the histogram aggregation.
359
+ # aggs - The Array of additional nested aggregations (optional).
360
+ #
361
+ # Examples
362
+ #
363
+ # Model.histogram(:my_agg, {field: 'field_name', interval: 5})
364
+ # Model.histogram(:my_agg, {field: 'field_name', interval: 5}, aggs: {...})
365
+ #
366
+ # Returns a new Stretchy::Relation.
367
+ def histogram(name, options = {}, *aggs)
368
+ options = {histogram: options}.merge(*aggs)
369
+ aggregation(name, options)
370
+ end
371
+
372
+ # Public: Perform an ip_range aggregation.
373
+ #
374
+ # name - The Symbol or String name of the aggregation.
375
+ # options - The Hash options used to refine the aggregation (default: {}):
376
+ # :field - The field to use for the ip_range aggregation.
377
+ # :ranges - The ranges to use for the ip_range aggregation. ranges: [{to: '10.0.0.5'}, {from: '10.0.0.5'}]
378
+ # aggs - The Array of additional nested aggregations (optional).
379
+ #
380
+ # Examples
381
+ #
382
+ # Model.ip_range(:my_agg, {field: 'ip_field'})
383
+ # Model.ip_range(:my_agg, {field: 'ip_field'}, aggs: {...})
384
+ #
385
+ # Returns a new Stretchy::Relation.
386
+ def ip_range(name, options = {}, *aggs)
387
+ options = {ip_range: options}.merge(*aggs)
388
+ aggregation(name, options)
389
+ end
390
+
391
+ # Public: Perform a max aggregation.
392
+ #
393
+ # name - The Symbol or String name of the aggregation.
394
+ # options - The Hash options used to refine the aggregation (default: {}):
395
+ # :field - The field to use for the max aggregation.
396
+ # aggs - The Array of additional nested aggregations (optional).
397
+ #
398
+ # Examples
399
+ #
400
+ # Model.max(:my_agg, {field: 'field_name'})
401
+ # Model.max(:my_agg, {field: 'field_name'}, aggs: {...})
402
+ #
403
+ # Returns a new Stretchy::Relation.
404
+ def max(name, options = {}, *aggs)
405
+ options = {max: options}.merge(*aggs)
406
+ aggregation(name, options)
407
+ end
408
+
409
+ # Public: Perform a min aggregation.
410
+ #
411
+ # name - The Symbol or String name of the aggregation.
412
+ # options - The Hash options used to refine the aggregation (default: {}):
413
+ # :field - The field to use for the min aggregation.
414
+ # aggs - The Array of additional nested aggregations (optional).
415
+ #
416
+ # Examples
417
+ #
418
+ # Model.min(:my_agg, {field: 'field_name'})
419
+ # Model.min(:my_agg, {field: 'field_name'}, aggs: {...})
420
+ #
421
+ # Returns a new Stretchy::Relation.
422
+ def min(name, options = {}, *aggs)
423
+ options = {min: options}.merge(*aggs)
424
+ aggregation(name, options)
425
+ end
426
+
427
+ # Public: Perform a missing aggregation.
428
+ #
429
+ # name - The Symbol or String name of the aggregation.
430
+ # options - The Hash options used to refine the aggregation (default: {}):
431
+ # :field - The field to use for the missing aggregation.
432
+ # aggs - The Array of additional nested aggregations (optional).
433
+ #
434
+ # Examples
435
+ #
436
+ # Model.missing(:my_agg, {field: 'field_name'})
437
+ # Model.missing(:my_agg, {field: 'field_name'}, aggs: {...})
438
+ #
439
+ # Returns a new Stretchy::Relation.
440
+ def missing(name, options = {}, *aggs)
441
+ options = {missing: options}.merge(*aggs)
442
+ aggregation(name, options)
443
+ end
444
+
445
+ # Public: Perform a nested aggregation.
446
+ #
447
+ # name - The Symbol or String name of the aggregation.
448
+ # options - The Hash options used to refine the aggregation (default: {}):
449
+ # :path - The path to use for the nested aggregation.
450
+ # aggs - The Array of additional nested aggregations (optional).
451
+ #
452
+ # Examples
453
+ #
454
+ # Model.nested(:my_agg, {path: 'path_to_field'})
455
+ # Model.nested(:my_agg, {path: 'path_to_field'}, aggs: {...})
456
+ #
457
+ # Returns a new Stretchy::Relation.
458
+ def nested(name, options = {}, *aggs)
459
+ options = {nested: options}.merge(*aggs)
460
+ aggregation(name, options)
461
+ end
462
+
463
+ # Public: Perform a percentile_ranks aggregation.
464
+ #
465
+ # name - The Symbol or String name of the aggregation.
466
+ # options - The Hash options used to refine the aggregation (default: {}):
467
+ # :field - The field to use for the percentile_ranks aggregation.
468
+ # :values - The values to use for the percentile_ranks aggregation.
469
+ # :keyed - associates a unique string key with each bucket and returns the ranges as a hash rather than an array. default: true
470
+ # :script - The script to use for the percentile_ranks aggregation. (optional) script: {source: "doc['field_name'].value", lang: "painless"}
471
+ # :hdr - The hdr to use for the percentile_ranks aggregation. (optional) hdr: {number_of_significant_value_digits: 3}
472
+ # :missing - The missing to use for the percentile_ranks aggregation. (optional) missing: 10
473
+ # aggs - The Array of additional nested aggregations (optional).
474
+ #
475
+ # Examples
476
+ #
477
+ # Model.percentile_ranks(:my_agg, {field: 'field_name', values: [1, 2, 3]})
478
+ # Model.percentile_ranks(:my_agg, {field: 'field_name', values: [1, 2, 3]}, aggs: {...})
479
+ #
480
+ # Returns a new Stretchy::Relation.
481
+ def percentile_ranks(name, options = {}, *aggs)
482
+ options = {percentile_ranks: options}.merge(*aggs)
483
+ aggregation(name, options)
484
+ end
485
+
486
+ # Public: Perform a percentiles aggregation.
487
+ #
488
+ # name - The Symbol or String name of the aggregation.
489
+ # options - The Hash options used to refine the aggregation (default: {}):
490
+ # :field - The field to use for the percentiles aggregation.
491
+ # :percents - The percents to use for the percentiles aggregation. percents: [95, 99, 99.9]
492
+ # :keyed - associates a unique string key with each bucket and returns the ranges as a hash rather than an array. default: true
493
+ # :tdigest - The tdigest to use for the percentiles aggregation. (optional) tdigest: {compression: 100, execution_hint: "high_accuracy"}
494
+ # :compression - The compression factor to use for the t-digest algorithm. A higher compression factor will yield more accurate percentiles, but will require more memory. The default value is 100.
495
+ # :execution_hint - The execution_hint to use for the t-digest algorithm. (optional) execution_hint: "auto"
496
+ # aggs - The Array of additional nested aggregations (optional).
497
+ #
498
+ # Examples
499
+ #
500
+ # Model.percentiles(:my_agg, {field: 'field_name', percents: [1, 2, 3]})
501
+ # Model.percentiles(:my_agg, {field: 'field_name', percents: [1, 2, 3]}, aggs: {...})
502
+ #
503
+ # Returns a new Stretchy::Relation.
504
+ def percentiles(name, options = {}, *aggs)
505
+ options = {percentiles: options}.merge(*aggs)
506
+ aggregation(name, options)
507
+ end
508
+
509
+ # Public: Perform a range aggregation.
510
+ #
511
+ # name - The Symbol or String name of the aggregation.
512
+ # options - The Hash options used to refine the aggregation (default: {}):
513
+ # :field - The field to use for the range aggregation.
514
+ # :ranges - The ranges to use for the range aggregation.
515
+ # :keyed - associates a unique string key with each bucket and returns the ranges as a hash rather than an array. default: true
516
+ # aggs - The Array of additional nested aggregations (optional).
517
+ #
518
+ # Examples
519
+ #
520
+ # Model.range(:my_agg, {field: 'field_name', ranges: [{from: 1, to: 2}, {from: 2, to: 3}]})
521
+ # Model.range(:my_agg, {field: 'field_name', ranges: [{from: 1, to: 2}, {from: 2, to: 3}]}, aggs: {...})
522
+ #
523
+ # Returns a new Stretchy::Relation.
524
+ def range(name, options = {}, *aggs)
525
+ options = {range: options}.merge(*aggs)
526
+ aggregation(name, options)
527
+ end
528
+
529
+ # Public: Perform a reverse_nested aggregation.
530
+ #
531
+ # name - The Symbol or String name of the aggregation.
532
+ # options - The Hash options used to refine the aggregation (default: {}).
533
+ # aggs - The Array of additional nested aggregations (optional).
534
+ #
535
+ # Examples
536
+ #
537
+ # Model.reverse_nested(:my_agg)
538
+ # Model.reverse_nested(:my_agg, {}, aggs: {...})
539
+ #
540
+ # Returns a new Stretchy::Relation.
541
+ def reverse_nested(name, options = {}, *aggs)
542
+ options = {reverse_nested: options}.merge(*aggs)
543
+ aggregation(name, options)
544
+ end
545
+
546
+ # Public: Perform a sampler aggregation.
547
+ #
548
+ # name - The Symbol or String name of the aggregation.
549
+ # options - The Hash options used to refine the aggregation (default: {}):
550
+ # :shard_size - The shard size to use for the sampler aggregation.
551
+ # aggs - The Array of additional nested aggregations (optional).
552
+ #
553
+ # Examples
554
+ #
555
+ # Model.sampler(:my_agg, {shard_size: 100})
556
+ # Model.sampler(:my_agg, {shard_size: 100}, aggs: {...})
557
+ #
558
+ # Returns a new Stretchy::Relation.
559
+ def sampler(name, options = {}, *aggs)
560
+ options = {sampler: options}.merge(*aggs)
561
+ aggregation(name, options)
562
+ end
563
+
564
+ # Public: Perform a scripted_metric aggregation.
565
+ #
566
+ # name - The Symbol or String name of the aggregation.
567
+ # options - The Hash options used to refine the aggregation (default: {}):
568
+ # :init_script - The initialization script for the scripted_metric aggregation.
569
+ # :map_script - The map script for the scripted_metric aggregation.
570
+ # :combine_script - The combine script for the scripted_metric aggregation.
571
+ # :reduce_script - The reduce script for the scripted_metric aggregation.
572
+ # aggs - The Array of additional nested aggregations (optional).
573
+ #
574
+ # Examples
575
+ #
576
+ # Model.scripted_metric(:my_agg, {init_script: '...', map_script: '...', combine_script: '...', reduce_script: '...'})
577
+ # Model.scripted_metric(:my_agg, {init_script: '...', map_script: '...', combine_script: '...', reduce_script: '...'}, aggs: {...})
578
+ #
579
+ # Returns a new Stretchy::Relation.
580
+ def scripted_metric(name, options = {}, *aggs)
581
+ options = {scripted_metric: options}.merge(*aggs)
582
+ aggregation(name, options)
583
+ end
584
+
585
+ # Public: Perform a significant_terms aggregation.
586
+ #
587
+ # name - The Symbol or String name of the aggregation.
588
+ # options - The Hash options used to refine the aggregation (default: {}):
589
+ # :field - The field to use for the significant_terms aggregation.
590
+ # :background_filter - The background filter to use for the significant_terms aggregation.
591
+ # :mutual_information - The mutual information to use for the significant_terms aggregation.
592
+ # :chi_square - The chi square to use for the significant_terms aggregation.
593
+ # :gnd - The gnd to use for the significant_terms aggregation.
594
+ # :jlh - The jlh to use for the significant_terms aggregation.
595
+ # aggs - The Array of additional nested aggregations (optional).
596
+ #
597
+ # Examples
598
+ #
599
+ # Model.significant_terms(:my_agg, {field: 'field_name'})
600
+ # Model.significant_terms(:my_agg, {field: 'field_name'}, aggs: {...})
601
+ #
602
+ # Returns a new Stretchy::Relation.
603
+ def significant_terms(name, options = {}, *aggs)
604
+ options = {significant_terms: options}.merge(*aggs)
605
+ aggregation(name, options)
606
+ end
607
+
608
+ # Public: Perform a stats aggregation.
609
+ #
610
+ # name - The Symbol or String name of the aggregation.
611
+ # options - The Hash options used to refine the aggregation (default: {}):
612
+ # :field - The field to use for the stats aggregation.
613
+ # :missing - The missing to use for the stats aggregation.
614
+ # :script - The script to use for the stats aggregation.
615
+ # aggs - The Array of additional nested aggregations (optional).
616
+ #
617
+ # Examples
618
+ #
619
+ # Model.stats(:my_agg, {field: 'field_name'})
620
+ # Model.stats(:my_agg, {field: 'field_name'}, aggs: {...})
621
+ #
622
+ # Returns a new Stretchy::Relation.
623
+ def stats(name, options = {}, *aggs)
624
+ options = {stats: options}.merge(*aggs)
625
+ aggregation(name, options)
626
+ end
627
+
628
+ # Public: Perform a sum aggregation.
629
+ #
630
+ # name - The Symbol or String name of the aggregation.
631
+ # options - The Hash options used to refine the aggregation (default: {}):
632
+ # :field - The field to use for the sum aggregation.
633
+ # :missing - The missing to use for the sum aggregation.
634
+ # :script - The script to use for the sum aggregation.
635
+ # aggs - The Array of additional nested aggregations (optional).
636
+ #
637
+ # Examples
638
+ #
639
+ # Model.sum(:my_agg, {field: 'field_name'})
640
+ # Model.sum(:my_agg, {field: 'field_name'}, aggs: {...})
641
+ #
642
+ # Returns a new Stretchy::Relation.
643
+ def sum(name, options = {}, *aggs)
644
+ options = {sum: options}.merge(*aggs)
645
+ aggregation(name, options)
646
+ end
647
+
648
+ # Public: Perform a terms aggregation.
649
+ #
650
+ # name - The Symbol or String name of the aggregation.
651
+ # options - The Hash options used to refine the aggregation (default: {}):
652
+ # :field - The field to use for the terms aggregation.
653
+ # :size - The size for the terms aggregation. (optional)
654
+ # :min_doc_count - The minimum document count for the terms aggregation. (optional)
655
+ # :shard_min_doc_count - The shard minimum document count for the terms aggregation. (optional)
656
+ # :show_term_doc_count_error - The show_term_doc_count_error for the terms aggregation. (optional) default: false
657
+ # :shard_size - The shard size for the terms aggregation. (optional)
658
+ # :order - The order for the terms aggregation. (optional)
659
+ #
660
+ # aggs - The Array of additional nested aggregations (optional).
661
+ #
662
+ # Examples
663
+ #
664
+ # Model.terms(:my_agg, {field: 'field_name', size: 10, min_doc_count: 1, shard_size: 100})
665
+ # Model.terms(:my_agg, {field: 'field_name', size: 10, min_doc_count: 1, shard_size: 100}, aggs: {...})
666
+ #
667
+ # Returns a new Stretchy::Relation.
668
+ def terms(name, options = {}, *aggs)
669
+ options = {terms: options}.merge(*aggs)
670
+ aggregation(name, options)
671
+ end
672
+
673
+
674
+ # Public: Perform a top_hits aggregation.
675
+ #
676
+ # name - The Symbol or String name of the aggregation.
677
+ # options - The Hash options used to refine the aggregation (default: {}):
678
+ # :size - The size for the top_hits aggregation.
679
+ # :from - The from for the top_hits aggregation.
680
+ # :sort - The sort for the top_hits aggregation.
681
+ # aggs - The Array of additional nested aggregations (optional).
682
+ #
683
+ # Examples
684
+ #
685
+ # Model.top_hits(:my_agg, {size: 10, sort: {...}})
686
+ # Model.top_hits(:my_agg, {size: 10, sort: {...}}, aggs: {...})
687
+ #
688
+ # Returns a new Stretchy::Relation.
689
+ def top_hits(name, options = {}, *aggs)
690
+ options = {top_hits: options}.merge(*aggs)
691
+ aggregation(name, options)
692
+ end
693
+
694
+ # Public: Perform a top_metrics aggregation.
695
+ #
696
+ # name - The Symbol or String name of the aggregation.
697
+ # options - The Hash options used to refine the aggregation (default: {}):
698
+ # :metrics - The metrics to use for the top_metrics aggregation. metrics: [{field: 'field_name', type: 'max'}, {field: 'field_name', type: 'min']
699
+ # :field - The field to use for the top_metrics aggregation. (optional)
700
+ # :size - The size for the top_metrics aggregation. (optional)
701
+ # :sort - The sort for the top_metrics aggregation. (optional)
702
+ # :missing - The missing for the top_metrics aggregation. (optional)
703
+ # aggs - The Array of additional nested aggregations (optional).
704
+ #
705
+ # Examples
706
+ #
707
+ # Model.top_metrics(:my_agg, {metrics: ['metric1', 'metric2']})
708
+ # Model.top_metrics(:my_agg, {metrics: ['metric1', 'metric2']}, aggs: {...})
709
+ #
710
+ # Returns a new Stretchy::Relation.
711
+ def top_metrics(name, options = {}, *aggs)
712
+ options = {top_metrics: options}.merge(*aggs)
713
+ aggregation(name, options)
714
+ end
715
+
716
+ # Public: Perform a value_count aggregation.
717
+ #
718
+ # name - The Symbol or String name of the aggregation.
719
+ # options - The Hash options used to refine the aggregation (default: {}):
720
+ # :field - The field to use for the value_count aggregation.
721
+ # :script - The script to use for the value_count aggregation. (optional) script: {source: "doc['field_name'].value", lang: "painless"}
722
+ # aggs - The Array of additional nested aggregations (optional).
723
+ #
724
+ # Examples
725
+ #
726
+ # Model.value_count(:my_agg, {field: 'field_name'})
727
+ # Model.value_count(:my_agg, {field: 'field_name'}, aggs: {...})
728
+ #
729
+ # Returns a new Stretchy::Relation.
730
+ def value_count(name, options = {}, *aggs)
731
+ options = {value_count: options}.merge(*aggs)
732
+ aggregation(name, options)
733
+ end
734
+
735
+ # Public: Perform a weighted_avg aggregation.
736
+ #
737
+ # name - The Symbol or String name of the aggregation.
738
+ # options - The Hash options used to refine the aggregation (default: {}):
739
+ # :value - The value field to use for the weighted_avg aggregation. {value: { field: 'price', missing: 0}}
740
+ # :weight - The weight field to use for the weighted_avg aggregation. {weight: { field: 'weight', missing: 0}}
741
+ # :format - The format for the weighted_avg aggregation. (optional)
742
+ # aggs - The Array of additional nested aggregations (optional).
743
+ #
744
+ # Examples
745
+ #
746
+ # Model.weighted_avg(:my_agg, {value_field: 'value_field_name', weight_field: 'weight_field_name'})
747
+ # Model.weighted_avg(:my_agg, {value_field: 'value_field_name', weight_field: 'weight_field_name'}, aggs: {...})
748
+ #
749
+ # Returns a new Stretchy::Relation.
750
+ def weighted_avg(name, options = {}, *aggs)
751
+ options = {weighted_avg: options}.merge(*aggs)
752
+ aggregation(name, options)
753
+ end
754
+
755
+
756
+ end
757
+ end
758
+ end