use crate::{util::ShouldSkip, Query};
#[derive(Default, Clone, PartialEq, Debug, Serialize)]
pub struct NestedFieldSort {
path: String,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
filter: Option<Query>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
max_children: Option<u32>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
nested: Option<Box<NestedFieldSort>>,
}
impl NestedFieldSort {
pub fn path<T>(path: T) -> Self
where
T: ToString,
{
Self {
path: path.to_string(),
filter: None,
max_children: None,
nested: None,
}
}
pub fn filter<T>(mut self, filter: T) -> Self
where
T: Into<Option<Query>>,
{
self.filter = filter.into();
self
}
pub fn max_children(mut self, max_children: u32) -> Self {
self.max_children = Some(max_children);
self
}
pub fn nested(mut self, nested: NestedFieldSort) -> Self {
self.nested = Some(Box::new(nested));
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{util::assert_serialize, Query};
#[test]
fn serialization() {
assert_serialize(NestedFieldSort::path("offer"), json!({ "path": "offer" }));
assert_serialize(
NestedFieldSort::path("offer").max_children(2),
json!({ "path": "offer", "max_children": 2 }),
);
assert_serialize(
NestedFieldSort::path("offer").filter(Query::term("offer.color", "blue")),
json!({
"path": "offer",
"filter": {
"term" : { "offer.color" : {"value": "blue"} }
}
}),
);
assert_serialize(
NestedFieldSort::path("parent")
.filter(Query::range("parent.age").gte(21))
.nested(
NestedFieldSort::path("parent.child")
.filter(Query::r#match("parent.child.name", "matt")),
),
json!({
"path": "parent",
"filter": {
"range": {"parent.age": {"gte": 21}}
},
"nested": {
"path": "parent.child",
"filter": {
"match": {"parent.child.name": {"query": "matt"}}
}
}
}),
);
}
}