elasticsearch_dsl/search/aggregations/metrics/
sum_aggregation.rs1use crate::search::*;
2use crate::util::*;
3
4#[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 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 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 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}