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
//! Bucket script aggregation types.

use serde::{Deserialize, Serialize};

use super::GapPolicy;

/// 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(feature = "graphql")]
#[cfg_attr(feature = "builder", derive(typed_builder::TypedBuilder))]
#[derive(async_graphql::InputObject, Serialize, Clone, Debug)]
#[cfg_attr(feature = "builder", builder(field_defaults(setter(into))))]
pub struct BucketScriptInput {
    /// The script to run for this aggregation.
    pub script: String,

    /// A map of script variables and their associated path to the buckets to
    /// use for the variable (see [`buckets_path` Syntax] for more details)
    ///
    /// [`buckets_path` Syntax]: /// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#buckets-path-syntax
    #[cfg_attr(feature = "builder", builder(default))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub buckets_path: Option<crate::scalars::Map>,

    /// The policy to apply when gaps are found in the data
    #[graphql(default_with = "Some(GapPolicy::Skip)")]
    #[cfg_attr(feature = "builder", builder(default))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gap_policy: Option<GapPolicy>,

    /// Format to apply to the output value of this aggregation
    #[cfg_attr(feature = "builder", builder(default))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
}

/// 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(test, derive(PartialEq))]
#[cfg_attr(feature = "graphql", derive(async_graphql::SimpleObject))]
#[cfg_attr(feature = "builder", derive(typed_builder::TypedBuilder))]
#[derive(Serialize, Deserialize, Clone, Debug)]
#[cfg_attr(feature = "builder", builder(field_defaults(setter(into))))]
pub struct BucketScript {
    /// The script to run for this aggregation.
    pub script: String,

    /// A map of script variables and their associated path to the buckets to
    /// use for the variable (see [`buckets_path` Syntax] for more details)
    ///
    /// [`buckets_path` Syntax]: /// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#buckets-path-syntax
    #[cfg_attr(feature = "builder", builder(default))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub buckets_path: Option<crate::scalars::Map>,

    /// The policy to apply when gaps are found in the data
    #[cfg_attr(feature = "builder", builder(default))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub gap_policy: Option<GapPolicy>,

    /// Format to apply to the output value of this aggregation
    #[cfg_attr(feature = "builder", builder(default))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
}

#[cfg(feature = "graphql")]
impl From<BucketScriptInput> for BucketScript {
    #[inline]
    fn from(input: BucketScriptInput) -> Self {
        BucketScript {
            script: input.script,
            buckets_path: input.buckets_path,
            gap_policy: input.gap_policy,
            format: input.format,
        }
    }
}