use crate::{search::*, util::*};
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Self")]
pub struct BoostingQuery {
positive: Box<Query>,
negative: Box<Query>,
negative_boost: NegativeBoost,
#[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 boosting<Q, B>(positive: Q, negative: Q, negative_boost: B) -> BoostingQuery
where
Q: Into<Query>,
B: Into<NegativeBoost>,
{
BoostingQuery {
positive: Box::new(positive.into()),
negative: Box::new(negative.into()),
negative_boost: negative_boost.into(),
boost: None,
_name: None,
}
}
}
impl BoostingQuery {
add_boost_and_name!();
}
impl ShouldSkip for BoostingQuery {
fn should_skip(&self) -> bool {
self.positive.should_skip() || self.negative.should_skip()
}
}
serialize_with_root!("boosting": BoostingQuery);
deserialize_with_root!("boosting": BoostingQuery);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialization() {
assert_serialize_query(
Query::boosting(Query::term("test1", 123), Query::term("test2", 456), 0.2),
json!({
"boosting": {
"positive": {
"term": {
"test1": {
"value": 123
}
}
},
"negative": {
"term": {
"test2": {
"value": 456
}
}
},
"negative_boost": 0.2
}
}),
);
assert_serialize_query(
Query::boosting(Query::term("test1", 123), Query::term("test2", 456), 0.2)
.boost(3)
.name("test"),
json!({
"boosting": {
"positive": {
"term": {
"test1": {
"value": 123
}
}
},
"negative": {
"term": {
"test2": {
"value": 456
}
}
},
"negative_boost": 0.2,
"boost": 3.0,
"_name": "test"
}
}),
);
}
}