elasticsearch_dsl/search/aggregations/metrics/
min_aggregation.rs

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