use crate::{search::*, util::*};
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct AvgAggregation {
avg: AvgAggregationInner,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
struct AvgAggregationInner {
field: String,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
missing: Option<Number>,
}
impl Aggregation {
pub fn avg<T>(field: T) -> AvgAggregation
where
T: ToString,
{
AvgAggregation {
avg: AvgAggregationInner {
field: field.to_string(),
missing: None,
},
}
}
}
impl AvgAggregation {
pub fn missing<T>(mut self, missing: T) -> Self
where
T: Into<Number>,
{
self.avg.missing = Some(missing.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialization() {
assert_serialize_aggregation(
Aggregation::avg("test_field"),
json!({ "avg": { "field": "test_field" } }),
);
assert_serialize_aggregation(
Aggregation::avg("test_field").missing(100.1),
json!({
"avg": {
"field": "test_field",
"missing": 100.1
}
}),
);
}
}