1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
use crate::search::*;
use crate::util::*;
/// Returns documents matching a `positive` query while reducing the
/// [relevance score](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html#relevance-scores)
/// of documents that also match a `negative` query.
///
/// You can use the `boosting` query to demote certain documents without excluding them from the search results.
///
/// To create boosting query:
/// ```
/// # use elasticsearch_dsl::queries::*;
/// # use elasticsearch_dsl::queries::params::*;
/// # let query =
/// Query::boosting(Query::term("test1", 123), Query::term("test2", 456), 0.2)
/// .boost(3)
/// .name("test");
/// ```
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html>
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(remote = "Self")]
pub struct BoostingQuery {
positive: Box<Query>,
negative: Box<Query>,
negative_boost: NegativeBoost,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
boost: Option<f32>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
_name: Option<String>,
}
impl Query {
/// Creates an instance of [`BoostingQuery`]
///
/// - `positive` - Query you wish to run. Any returned documents must match this query.
/// - `negative` - Query used to decrease the
/// [relevance score](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html#relevance-scores)
/// of matching documents.<br/>
/// If a returned document matches the `positive` query and this query, the `boosting` query
/// calculates the final
/// [relevance score](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html#relevance-scores)
/// for the document as follows:
/// 1. Take the original relevance score from the `positive` query.
/// 2. Multiply the score by the `negative_boost` value.
/// - `negative_boost` - Floating point number between `0` and `1.0` used to decrease the
/// [relevance scores](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html#relevance-scores)
/// of documents matching the `negative` 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);
#[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"
}
}),
);
}
}