elastiql 0.5.0

An opinionated Elasticsearch query language for Rust.
Documentation
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! Request, response and other types used when [aggregating] documents.
//!
//! [aggregating]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html

pub use super::response::*;
use super::types::*;

use crate::search::query::CompoundQuery;
#[cfg(feature = "graphql")]
use crate::search::query::CompoundQueryInput;

/// An [aggregation] can be seen as a unit-of-work that builds analytic
/// information over a set of documents.
///
/// **Note**: Even though they are all marked optional by GraphQL, you **must**
/// specify **exactly one** aggregate field (apart from the optional
/// `aggregations` field). This is due to a limitation of GraphQL and will be
/// changed once [union input types] are supported.
///
/// [aggregation]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html
/// [union input types]: https://github.com/graphql/graphql-spec/blob/master/rfcs/InputUnion.md
#[cfg(feature = "graphql")]
#[cfg_attr(feature = "builder", derive(typed_builder::TypedBuilder))]
#[derive(async_graphql::InputObject, Clone, Debug)]
#[graphql(name = "AggregationInput")]
#[cfg_attr(feature = "builder", builder(field_defaults(setter(into))))]
pub struct RequestInput {
    /// The name for this aggregation.
    ///
    /// **NOTE**: this must be unique otherwise the query will only return
    /// results for the last one with this name.
    pub name: String,

    /// A `single-value` [*metrics*] aggregation that computes the average of
    /// numeric values that are extracted from the aggregated documents. These
    /// values can be extracted either from specific numeric fields in the
    /// documents, or be generated by a provided script.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub avg: Option<InnerAggregationInput>,

    /// 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.
    ///
    /// When calculating a regular average, each datapoint has an equal "weight"...
    /// it contributes equally to the final value. Weighted averages, on the other
    /// hand, weight each datapoint differently. The amount that each datapoint
    /// contributes to the final value is extracted from the document, or provided
    /// by a script.
    ///
    /// As a formula, a weighted average is the `∑(value * weight) / ∑(weight)`
    ///
    /// A regular average can be thought of as a weighted average where every value
    /// has an implicit weight of `1`.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub weighted_avg: Option<WeightedAverageAggregationInput>,

    /// A `single-value` [*metrics*] aggregation that calculates an approximate
    /// count of distinct values. Values can be extracted either from specific
    /// fields in the document or generated by a script.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub cardinality: Option<InnerAggregationInput>,

    /// A `single-value` [*metrics*] aggregation that keeps track and returns
    /// the maximum value among the numeric values extracted from the aggregated
    /// documents.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub max: Option<InnerAggregationInput>,

    /// A `single-value` [*metrics*] aggregation that keeps track and returns
    /// the minimum value among numeric values extracted from the aggregated
    /// documents.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub min: Option<InnerAggregationInput>,

    /// This `single-value` [*metrics*] aggregation approximates the median
    /// absolute deviation of its search results.
    ///
    /// Median absolute deviation is a measure of variability. It is a robust
    /// statistic, meaning that it is useful for describing data that may have
    /// outliers, or may not be normally distributed. For such data it can be
    /// more descriptive than standard deviation.
    ///
    /// It is calculated as the median of each data point’s deviation from the
    /// median of the entire sample. That is, for a random variable `X`, the
    /// median absolute deviation is `median(|median(X) - Xi|)`.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub median_absolute_deviation: Option<InnerAggregationInput>,

    /// A `multi-value` [*metrics*] aggregation that calculates one or more
    /// percentiles over numeric values extracted from the aggregated documents.
    ///
    /// Percentiles show the point at which a certain percentage of observed
    /// values occur. For example, the 95th percentile is the value which is
    /// greater than 95% of the observed values.
    ///
    /// Percentiles are often used to find outliers. In normal distributions,
    /// the 0.13th and 99.87th percentiles represents three standard deviations
    /// from the mean. Any data which falls outside three standard deviations is
    /// often considered an anomaly.
    ///
    /// When a range of percentiles are retrieved, they can be used to estimate
    /// the data distribution and determine if the data is skewed, bimodal, etc.
    ///
    /// *Note*: [Percentiles are (usually) estimated].
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    /// [Percentiles are (usually) estimated]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html#search-aggregations-metrics-percentile-aggregation-approximation
    #[cfg_attr(feature = "builder", builder(default))]
    pub percentiles: Option<InnerAggregationInput>,

    /// A `multi-value` [*metrics*] aggregation that calculates one or more
    /// percentile ranks over numeric values extracted from the aggregated
    /// documents.
    ///
    /// *Note*: [Percentiles are (usually) estimated].
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    /// [Percentiles are (usually) estimated]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html#search-aggregations-metrics-percentile-aggregation-approximation
    #[cfg_attr(feature = "builder", builder(default))]
    pub percentile_ranks: Option<InnerAggregationInput>,

    /// A `multi-value` [*metrics*] aggregation that computes stats over numeric
    /// values extracted from the aggregated documents.
    ///
    /// The stats that are returned consist of: `min`, `max`, `sum`, `count` and
    /// `avg`.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub stats: Option<InnerAggregationInput>,

    /// A `multi-value` [*metrics*] aggregation that computes stats over numeric
    /// values extracted from the aggregated documents.
    ///
    /// The `extended_stats` aggregations is an extended version of the `stats`
    /// aggregation, where additional metrics are added such as
    /// `sum_of_squares`, `variance`, `std_deviation` and
    /// `std_deviation_bounds`.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub extended_stats: Option<InnerAggregationInput>,

    /// A `single-value` [*metrics*] aggregation that sums up numeric values
    /// that are extracted from the aggregated documents.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub sum: Option<InnerAggregationInput>,

    /// A `single-value` [*metrics*] aggregation  that counts the number of
    /// values that are extracted from the aggregated documents. These values
    /// can be extracted either from specific fields in the documents, or be
    /// generated by a provided script. Typically, this aggregator will be used
    /// in conjunction with other `single-value` aggregations.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub value_count: Option<InnerAggregationInput>,

    /// A [*bucketing*] aggregation that creates a single *bucket* for all
    /// documents matching the filters specified.
    ///
    /// [*bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub filters: Option<CompoundQueryInput>,

    /// A [*multi-bucketing*] value source based aggregation where buckets are
    /// dynamically built - one per unique value.
    ///
    /// [*multi-bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub terms: Option<TermsAggregationInput>,

    /// A [`range`] ([*bucketing*]) aggregation.
    ///
    /// [*bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    /// [`range`]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub range: Option<RangeAggregationInput>,

    /// A [`range`] ([*bucketing*]) aggregation that is dedicated for date
    /// values and supports [Date Math] expressions.
    ///
    /// [*bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    /// [`range`]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
    /// [Date Math]: https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math
    #[cfg_attr(feature = "builder", builder(default))]
    pub date_range: Option<DateRangeAggregationInput>,

    /// A [*multi-bucketing*] aggregation similar to the normal [histogram
    /// aggregation], but can only be used with date or date range values.
    ///
    /// [*multi-bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    /// [histogram aggregation]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub date_histogram: Option<DateHistogramAggregationInput>,

    /// A [*multi-bucket*] aggregation similar to the [Date histogram aggregation]
    /// except instead of providing an interval to use as the width of each bucket,
    /// a target number of buckets is provided indicating the number of buckets
    /// needed and the interval of the buckets is automatically chosen to best
    /// achieve that target. The number of buckets returned will always be less than
    /// or equal to this target number.
    ///
    /// [Date histogram aggregation]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html
    /// [*multi-bucket*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub auto_date_histogram: Option<AutoDateHistogramAggregationInput>,

    /// A filtering aggregation used to limit any sub aggregations' processing
    /// to a sample of the top-scoring documents.
    #[cfg_attr(feature = "builder", builder(default))]
    pub sampler: Option<SamplerAggregationInput>,

    /// An aggregation that returns interesting or unusual occurrences of
    /// free-text terms in a set.
    ///
    /// See the official documentation for [significant text] for more
    /// information.
    ///
    /// [significant text]: https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-aggregations-bucket-significanttext-aggregation.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub significant_text: Option<SignificantTextAggregationInput>,

    /// A [*multi-bucketing*] values source based aggregation that can be
    /// applied on numeric values or numeric range values extracted from the
    /// documents. It dynamically builds fixed size (a.k.a. interval) buckets
    /// over the values.
    ///
    /// [*multi-bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub histogram: Option<HistogramAggregationInput>,

    /// [Variable width histogram] is a [*multi-bucket*] aggregation similar to
    /// [histogram]. However, the width of each bucket is not specified. Rather, a
    /// target number of buckets is provided and bucket intervals are dynamically
    /// determined based on the document distribution.
    ///
    /// [Variable width histogram]: https://www.elastic.co/guide/en/elasticsearch/reference/latest/search-aggregations-bucket-variablewidthhistogram-aggregation.html
    /// [*multi-bucket*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub variable_width_histogram: Option<VariableWidthHistogramInput>,

    /// A parent [*pipeline aggregation*] which executes a [script] which can
    /// perform per bucket computations on specified metrics in the parent
    /// multi-bucket aggregation. The specified metric must be numeric and the
    /// script must return a numeric value.
    ///
    /// [*pipeline aggregation*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html
    /// [script]: https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub bucket_script: Option<BucketScriptInput>,

    /// A parent [*pipeline aggregation*] which executes a [script] which
    /// determines whether the current bucket will be retained in the parent
    /// multi-bucket aggregation. The specified metric must be numeric and the
    /// script must return a boolean value.
    ///
    /// [*pipeline aggregation*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html
    /// [script]: https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub bucket_selector: Option<BucketSelectorInput>,

    /// A parent [*pipeline aggregation*] which sorts the buckets of its parent
    /// `multi-bucket` aggregation. Zero or more sort fields may be specified
    /// together with the corresponding sort order. Each bucket may be sorted based
    /// on its `_key`, `_count` or its sub-aggregations. In addition, parameters
    /// from and size may be set in order to truncate the result buckets.
    ///
    /// [*pipeline aggregation*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub bucket_sort: Option<BucketSortInput>,

    /// A special single [*bucketing*] aggregation that enables aggregating
    /// [nested] documents.
    ///
    /// **Note**: *must* be used with the `aggregations` field as a sibling.
    ///
    /// [*bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    /// [nested]: https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub nested: Option<NestedAggregationInput>,

    /// A special single [*bucketing*] aggregation that enables aggregating on
    /// parent docs from [nested] documents. Effectively this aggregation can
    /// break out of the nested block structure and link to other nested
    /// structures or the root document, which allows nesting other aggregations
    /// that aren’t part of the nested object in a nested aggregation.
    ///
    /// The [`ReverseNestedAggregation`] aggregation must be defined inside a
    /// [`nested`] aggregation.
    ///
    /// [*bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    /// [nested]: https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
    /// [`nested`]: https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
    /// [`ReverseNestedAggregation`]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub reverse_nested: Option<ReverseNestedAggregationInput>,

    /// The custom [metadata] to associate with this aggregation that will be
    /// returned alongside the results for this aggregation.
    ///
    /// If the optional special key `_skip` is set to `true`, the results for
    /// this aggregation will be calculated but not returned with the other
    /// results.
    ///
    /// [metadata]: https://www.elastic.co/guide/en/elasticsearch/reference/current/agg-metadata.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub metadata: Option<crate::scalars::Map>,

    /// The sub aggregations, if any.
    #[cfg_attr(feature = "builder", builder(default))]
    pub aggregations: Option<Vec<RequestInput>>,
}

/// An [aggregation] can be seen as a unit-of-work that builds analytic
/// information over a set of documents.
///
/// [aggregation]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(feature = "graphql", derive(async_graphql::SimpleObject))]
#[cfg_attr(feature = "graphql", graphql(name = "Aggregation"))]
#[cfg_attr(feature = "builder", derive(typed_builder::TypedBuilder))]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "builder", builder(field_defaults(setter(into))))]
pub struct Request {
    /// The name for this aggregation.
    ///
    /// **NOTE**: this must be unique otherwise the query will only return
    /// results for the last one with this name.
    pub name: String,

    /// A `single-value` [*metrics*] aggregation that computes the average of
    /// numeric values that are extracted from the aggregated documents. These
    /// values can be extracted either from specific numeric fields in the
    /// documents, or be generated by a provided script.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub avg: Option<InnerAggregation>,

    /// 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.
    ///
    /// When calculating a regular average, each datapoint has an equal "weight"...
    /// it contributes equally to the final value. Weighted averages, on the other
    /// hand, weight each datapoint differently. The amount that each datapoint
    /// contributes to the final value is extracted from the document, or provided
    /// by a script.
    ///
    /// As a formula, a weighted average is the `∑(value * weight) / ∑(weight)`
    ///
    /// A regular average can be thought of as a weighted average where every value
    /// has an implicit weight of `1`.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub weighted_avg: Option<WeightedAverageAggregation>,

    /// A `single-value` [*metrics*] aggregation that calculates an approximate
    /// count of distinct values. Values can be extracted either from specific
    /// fields in the document or generated by a script.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub cardinality: Option<InnerAggregation>,

    /// A `single-value` [*metrics*] aggregation that keeps track and returns
    /// the maximum value among the numeric values extracted from the aggregated
    /// documents.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub max: Option<InnerAggregation>,

    /// A `single-value` [*metrics*] aggregation that keeps track and returns
    /// the minimum value among numeric values extracted from the aggregated
    /// documents.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub min: Option<InnerAggregation>,

    /// This `single-value` [*metrics*] aggregation approximates the [median
    /// absolute deviation] of its search results.
    ///
    /// Median absolute deviation is a measure of variability. It is a robust
    /// statistic, meaning that it is useful for describing data that may have
    /// outliers, or may not be normally distributed. For such data it can be
    /// more descriptive than standard deviation.
    ///
    /// It is calculated as the median of each data point’s deviation from the
    /// median of the entire sample. That is, for a random variable `X`, the
    /// [median absolute deviation] is `median(|median(X) - Xi|)`.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    /// [median absolute deviation]: https://en.wikipedia.org/wiki/Median_absolute_deviation
    #[cfg_attr(feature = "builder", builder(default))]
    pub median_absolute_deviation: Option<InnerAggregation>,

    /// A `multi-value` [*metrics*] aggregation that calculates one or more
    /// percentiles over numeric values extracted from the aggregated documents.
    ///
    /// Percentiles show the point at which a certain percentage of observed
    /// values occur. For example, the 95th percentile is the value which is
    /// greater than 95% of the observed values.
    ///
    /// Percentiles are often used to find outliers. In normal distributions,
    /// the 0.13th and 99.87th percentiles represents three standard deviations
    /// from the mean. Any data which falls outside three standard deviations is
    /// often considered an anomaly.
    ///
    /// When a range of percentiles are retrieved, they can be used to estimate
    /// the data distribution and determine if the data is skewed, bimodal, etc.
    ///
    /// *Note*: [Percentiles are (usually) estimated].
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    /// [Percentiles are (usually) estimated]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html#search-aggregations-metrics-percentile-aggregation-approximation
    #[cfg_attr(feature = "builder", builder(default))]
    pub percentiles: Option<InnerAggregation>,

    /// A `multi-value` [*metrics*] aggregation that calculates one or more
    /// percentile ranks over numeric values extracted from the aggregated
    /// documents.
    ///
    /// *Note*: [Percentiles are (usually) estimated].
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    /// [Percentiles are (usually) estimated]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html#search-aggregations-metrics-percentile-aggregation-approximation
    #[cfg_attr(feature = "builder", builder(default))]
    pub percentile_ranks: Option<InnerAggregation>,

    /// A `multi-value` [*metrics*] aggregation that computes stats over numeric
    /// values extracted from the aggregated documents.
    ///
    /// The stats that are returned consist of: `min`, `max`, `sum`, `count` and
    /// `avg`.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub stats: Option<InnerAggregation>,

    /// A `multi-value` [*metrics*] aggregation that computes stats over numeric
    /// values extracted from the aggregated documents.
    ///
    /// The `extended_stats` aggregations is an extended version of the `stats`
    /// aggregation, where additional metrics are added such as
    /// `sum_of_squares`, `variance`, `std_deviation` and
    /// `std_deviation_bounds`.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub extended_stats: Option<InnerAggregation>,

    /// A `single-value` [*metrics*] aggregation that sums up numeric values
    /// that are extracted from the aggregated documents.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub sum: Option<InnerAggregation>,

    /// A `single-value` [*metrics*] aggregation  that counts the number of
    /// values that are extracted from the aggregated documents. These values
    /// can be extracted either from specific fields in the documents, or be
    /// generated by a provided script. Typically, this aggregator will be used
    /// in conjunction with other `single-value` aggregations.
    ///
    /// [*metrics*]:  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub value_count: Option<InnerAggregation>,

    /// A [*bucketing*] aggregation that creates a single *bucket* for all
    /// documents matching the filters specified.
    ///
    /// [*bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub filters: Option<CompoundQuery>,

    /// A [*multi-bucketing*] value source based aggregation where buckets are
    /// dynamically built - one per unique value.
    ///
    /// [*multi-bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub terms: Option<TermsAggregation>,

    /// A [`range`] ([*bucketing*]) aggregation.
    ///
    /// [*bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    /// [`range`]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub range: Option<RangeAggregation>,

    /// A [`range`] ([*bucketing*]) aggregation that is dedicated for date
    /// values and supports [Date Math] expressions.
    ///
    /// [*bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    /// [`range`]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
    /// [Date Math]: https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math
    #[cfg_attr(feature = "builder", builder(default))]
    pub date_range: Option<DateRangeAggregation>,

    /// A [*multi-bucketing*] aggregation similar to the normal [histogram
    /// aggregation], but can only be used with date or date range values.
    ///
    /// [*multi-bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    /// [histogram aggregation]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub date_histogram: Option<DateHistogramAggregation>,

    /// A [*multi-bucket*] aggregation similar to the [Date histogram aggregation]
    /// except instead of providing an interval to use as the width of each bucket,
    /// a target number of buckets is provided indicating the number of buckets
    /// needed and the interval of the buckets is automatically chosen to best
    /// achieve that target. The number of buckets returned will always be less than
    /// or equal to this target number.
    ///
    /// [Date histogram aggregation]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html
    /// [*multi-bucket*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub auto_date_histogram: Option<AutoDateHistogramAggregation>,

    /// A [*multi-bucketing*] values source based aggregation that can be
    /// applied on numeric values or numeric range values extracted from the
    /// documents. It dynamically builds fixed size (a.k.a. interval) buckets
    /// over the values.
    ///
    /// [*multi-bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub histogram: Option<HistogramAggregation>,

    /// [Variable width histogram] is a [*multi-bucket*] aggregation similar to
    /// [histogram]. However, the width of each bucket is not specified. Rather, a
    /// target number of buckets is provided and bucket intervals are dynamically
    /// determined based on the document distribution.
    ///
    /// [Variable width histogram]: https://www.elastic.co/guide/en/elasticsearch/reference/latest/search-aggregations-bucket-variablewidthhistogram-aggregation.html
    /// [*multi-bucket*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub variable_width_histogram: Option<VariableWidthHistogram>,

    /// A filtering aggregation used to limit any sub aggregations' processing
    /// to a sample of the top-scoring documents.
    #[cfg_attr(feature = "builder", builder(default))]
    pub sampler: Option<SamplerAggregation>,

    /// An aggregation that returns interesting or unusual occurrences of
    /// free-text terms in a set.
    ///
    /// See the official documentation for [significant text] for more
    /// information.
    ///
    /// [significant text]: https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-aggregations-bucket-significanttext-aggregation.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub significant_text: Option<SignificantTextAggregation>,

    /// A parent [*pipeline aggregation*] which executes a [script] which can
    /// perform per bucket computations on specified metrics in the parent
    /// multi-bucket aggregation. The specified metric must be numeric and the
    /// script must return a numeric value.
    ///
    /// [*pipeline aggregation*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html
    /// [script]: https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub bucket_script: Option<BucketScript>,

    /// A parent [*pipeline aggregation*] which executes a [script] which
    /// determines whether the current bucket will be retained in the parent
    /// multi-bucket aggregation. The specified metric must be numeric and the
    /// script must return a boolean value.
    ///
    /// [*pipeline aggregation*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html
    /// [script]: https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub bucket_selector: Option<BucketSelector>,

    /// A parent [*pipeline aggregation*] which sorts the buckets of its parent
    /// `multi-bucket` aggregation. Zero or more sort fields may be specified
    /// together with the corresponding sort order. Each bucket may be sorted based
    /// on its `_key`, `_count` or its sub-aggregations. In addition, parameters
    /// from and size may be set in order to truncate the result buckets.
    ///
    /// [*pipeline aggregation*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub bucket_sort: Option<BucketSort>,

    /// A special single [*bucketing*] aggregation that enables aggregating
    /// [nested] documents.
    ///
    /// **Note**: *must* be used with the `aggregations` field as a sibling.
    ///
    /// [*bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    /// [nested]: https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub nested: Option<NestedAggregation>,

    /// A special single [*bucketing*] aggregation that enables aggregating on
    /// parent docs from [nested] documents. Effectively this aggregation can
    /// break out of the nested block structure and link to other nested
    /// structures or the root document, which allows nesting other aggregations
    /// that aren’t part of the nested object in a nested aggregation.
    ///
    /// The [`ReverseNestedAggregation`] aggregation must be defined inside a
    /// [`nested`] aggregation.
    ///
    /// [*bucketing*]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
    /// [nested]: https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
    /// [`nested`]: https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
    /// [`ReverseNestedAggregation`]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub reverse_nested: Option<ReverseNestedAggregation>,

    /// The custom [metadata] to associate with this aggregation that will be
    /// returned alongside the results for this aggregation.
    ///
    /// If the optional special key `_skip` is set to `true`, the results for
    /// this aggregation will be calculated but not returned with the other
    /// results.
    ///
    /// [metadata]: https://www.elastic.co/guide/en/elasticsearch/reference/current/agg-metadata.html
    #[cfg_attr(feature = "builder", builder(default))]
    pub metadata: Option<crate::scalars::Map>,

    /// The sub aggregation, if any.
    #[cfg_attr(feature = "builder", builder(default))]
    pub aggregations: Option<Vec<Request>>,
}

// TODO: auto generate this with a proc_macro?
#[cfg(feature = "graphql")]
impl From<RequestInput> for Request {
    #[inline]
    fn from(aggregation: RequestInput) -> Self {
        Self {
            name: aggregation.name,
            avg: aggregation.avg.map(Into::into),
            weighted_avg: aggregation.weighted_avg.map(Into::into),
            cardinality: aggregation.cardinality.map(Into::into),
            max: aggregation.max.map(Into::into),
            min: aggregation.min.map(Into::into),
            median_absolute_deviation: aggregation.median_absolute_deviation.map(Into::into),
            percentiles: aggregation.percentiles.map(Into::into),
            percentile_ranks: aggregation.percentile_ranks.map(Into::into),
            stats: aggregation.stats.map(Into::into),
            extended_stats: aggregation.extended_stats.map(Into::into),
            sum: aggregation.sum.map(Into::into),
            value_count: aggregation.value_count.map(Into::into),
            filters: aggregation.filters.map(Into::into),
            terms: aggregation.terms.map(Into::into),
            range: aggregation.range.map(Into::into),
            date_range: aggregation.date_range.map(Into::into),
            date_histogram: aggregation.date_histogram.map(Into::into),
            auto_date_histogram: aggregation.auto_date_histogram.map(Into::into),
            histogram: aggregation.histogram.map(Into::into),
            variable_width_histogram: aggregation.variable_width_histogram.map(Into::into),
            sampler: aggregation.sampler.map(Into::into),
            significant_text: aggregation.significant_text.map(Into::into),
            bucket_script: aggregation.bucket_script.map(Into::into),
            bucket_selector: aggregation.bucket_selector.map(Into::into),
            bucket_sort: aggregation.bucket_sort.map(Into::into),
            nested: aggregation.nested.map(Into::into),
            reverse_nested: aggregation.reverse_nested.map(Into::into),
            metadata: aggregation.metadata,
            aggregations: aggregation
                .aggregations
                .map(|aggs| aggs.into_iter().map(Into::into).collect()),
        }
    }
}