elasticsearch_dsl/search/aggregations/metrics/
sum_aggregation.rs

1use crate::search::*;
2use crate::util::*;
3
4/// A `single-value` metrics aggregation that sums up numeric values that are extracted from the
5/// aggregated documents. These values can be extracted either from specific numeric or histogram fields.
6///
7/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html>
8#[derive(Debug, Clone, Serialize, PartialEq)]
9pub struct SumAggregation {
10    sum: SumAggregationInner,
11}
12
13#[derive(Debug, Clone, Serialize, PartialEq)]
14struct SumAggregationInner {
15    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
16    field: Option<String>,
17
18    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
19    script: Option<Script>,
20
21    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
22    missing: Option<Number>,
23}
24
25impl Aggregation {
26    /// Creates an instance of [`SumAggregation`]
27    ///
28    /// - `field` - field to aggregate
29    pub fn sum<T>(field: T) -> SumAggregation
30    where
31        T: ToString,
32    {
33        SumAggregation {
34            sum: SumAggregationInner {
35                field: field.to_string().into(),
36                script: None,
37                missing: None,
38            },
39        }
40    }
41
42    /// Creates an instance of [`SumAggregation`]
43    ///
44    /// - `script` - script to aggregate
45    pub fn sum_script(script: Script) -> SumAggregation {
46        SumAggregation {
47            sum: SumAggregationInner {
48                script: script.into(),
49                field: None,
50                missing: None,
51            },
52        }
53    }
54}
55
56impl SumAggregation {
57    /// The `missing` parameter defines how documents that are missing a value should be treated. By
58    /// default documents missing the value will be ignored but it is also possible to treat them
59    /// as if they had a value.
60    pub fn missing<T>(mut self, missing: T) -> Self
61    where
62        T: Into<Number>,
63    {
64        self.sum.missing = Some(missing.into());
65        self
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn serialization() {
75        assert_serialize_aggregation(
76            Aggregation::sum("test_field"),
77            json!({ "sum": { "field": "test_field" } }),
78        );
79
80        assert_serialize_aggregation(
81            Aggregation::sum("test_field").missing(100.1),
82            json!({
83                "sum": {
84                    "field": "test_field",
85                    "missing": 100.1
86                }
87            }),
88        );
89
90        assert_serialize_aggregation(
91            Aggregation::sum_script(Script::source("_score")),
92            json!({
93                "sum": {
94                    "script": {
95                        "source": "_score"
96                    }
97                }
98            }),
99        );
100    }
101}