use crate::{search::*, util::*};
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Self")]
pub struct DisMaxQuery {
queries: QueryCollection,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
tie_breaker: Option<f32>,
#[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 dis_max() -> DisMaxQuery {
DisMaxQuery::default()
}
}
impl DisMaxQuery {
add_boost_and_name!();
pub fn query<T>(mut self, query: T) -> Self
where
T: IntoIterator,
T::Item: Into<Query>,
{
self.queries.extend(query);
self
}
pub fn tie_breaker(mut self, tie_breaker: f32) -> Self {
self.tie_breaker = Some(tie_breaker);
self
}
}
impl ShouldSkip for DisMaxQuery {
fn should_skip(&self) -> bool {
self.queries.should_skip()
}
}
serialize_with_root!("dis_max": DisMaxQuery);
deserialize_with_root!("dis_max": DisMaxQuery);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialization() {
assert_serialize_query(
Query::dis_max()
.query(Query::r#match("t1", "text"))
.query(Query::r#match("t2", "text")),
json!({
"dis_max": {
"queries": [
{
"match": {
"t1": {
"query": "text"
}
}
},
{
"match": {
"t2": {
"query": "text"
}
}
}
]
}
}),
);
assert_serialize_query(
Query::dis_max().query([Query::r#match("t1", "text"), Query::r#match("t2", "text")]),
json!({
"dis_max": {
"queries": [
{
"match": {
"t1": {
"query": "text"
}
}
},
{
"match": {
"t2": {
"query": "text"
}
}
}
]
}
}),
);
assert_serialize_query(
Query::dis_max()
.query(Query::r#match("t1", "text"))
.query(Query::r#match("t2", "text"))
.tie_breaker(0.5)
.boost(3)
.name("test"),
json!({
"dis_max": {
"queries": [
{
"match": {
"t1": {
"query": "text"
}
}
},
{
"match": {
"t2": {
"query": "text"
}
}
}
],
"tie_breaker": 0.5,
"boost": 3.0,
"_name": "test"
}
}),
);
}
}