use crate::{search::*, util::*};
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Self")]
pub struct HasParentQuery {
parent_type: String,
query: Box<Query>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
score: Option<bool>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
ignore_unmapped: Option<bool>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
boost: Option<f32>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
_name: Option<String>,
}
impl Query {
pub fn has_parent<T, U>(parent_type: T, query: U) -> HasParentQuery
where
T: ToString,
U: Into<Query>,
{
HasParentQuery {
parent_type: parent_type.to_string(),
query: Box::new(query.into()),
score: None,
ignore_unmapped: None,
boost: None,
_name: None,
}
}
}
impl HasParentQuery {
add_boost_and_name!();
pub fn score(mut self, score: bool) -> Self {
self.score = Some(score);
self
}
pub fn ignore_unmapped(mut self, ignore_unmapped: bool) -> Self {
self.ignore_unmapped = Some(ignore_unmapped);
self
}
}
impl ShouldSkip for HasParentQuery {
fn should_skip(&self) -> bool {
self.parent_type.should_skip() || self.query.should_skip()
}
}
serialize_with_root!("has_parent": HasParentQuery);
deserialize_with_root!("has_parent": HasParentQuery);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialization() {
assert_serialize_query(
Query::has_parent("parent", Query::term("tag", "opensearch")),
json!({
"has_parent": {
"parent_type": "parent",
"query": {
"term": {
"tag": {
"value": "opensearch"
}
}
}
}
}),
);
assert_serialize_query(
Query::has_parent("parent", Query::term("tag", "opensearch"))
.boost(2)
.name("test")
.ignore_unmapped(true)
.score(true),
json!({
"has_parent": {
"parent_type": "parent",
"score": true,
"ignore_unmapped": true,
"query": {
"term": {
"tag": {
"value": "opensearch"
}
}
},
"boost": 2.0,
"_name": "test"
}
}),
);
}
}