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