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
use crate::search::*;
use crate::util::*;
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct CardinalityAggregation {
#[serde(skip_serializing)]
pub(crate) name: String,
cardinality: CardinalityAggregationInner,
}
#[derive(Debug, Clone, Serialize, PartialEq)]
struct CardinalityAggregationInner {
field: String,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
precision_threshold: Option<u16>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
missing: Option<String>,
}
impl Aggregation {
pub fn cardinality(
name: impl Into<String>,
field: impl Into<String>,
) -> CardinalityAggregation {
CardinalityAggregation {
name: name.into(),
cardinality: CardinalityAggregationInner {
field: field.into(),
precision_threshold: None,
missing: None,
},
}
}
}
impl CardinalityAggregation {
pub fn precision_threshold(mut self, precision_threshold: impl Into<u16>) -> Self {
self.cardinality.precision_threshold = Some(precision_threshold.into());
self
}
pub fn missing(mut self, missing: impl Into<String>) -> Self {
self.cardinality.missing = Some(missing.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
test_serialization! {
with_required_fields(
Aggregation::cardinality("test_cardinality", "test_field"),
json!({ "cardinality": { "field": "test_field" } })
);
with_all_fields(
Aggregation::cardinality("test_cardinality", "test_field")
.precision_threshold(100u16)
.missing("N/A"),
json!({
"cardinality": {
"field": "test_field",
"precision_threshold": 100,
"missing": "N/A"
}
})
);
}
}