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
use crate::search::*;
use crate::util::*;
/// A query allowing you to modify the score of documents that are retrieved by
/// a query. This can be useful if, for example, a score function is
/// computationally expensive and it is sufficient to compute the score on a
/// filtered set of documents.
///
/// To create script score query:
/// ```
/// # use elasticsearch_dsl::queries::*;
/// # use elasticsearch_dsl::queries::params::*;
/// # let query =
/// Query::script_score(
/// Query::r#match("message", "elasticsearch"),
/// Script::source("doc['my-int'].value / 10"),
/// );
/// ```
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-score-query.html>
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(remote = "Self")]
pub struct ScriptScoreQuery {
query: Box<Query>,
script: Script,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
min_score: Option<f32>,
#[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 [`ScriptScoreQuery`]
///
/// - `query` - Query used to return documents
/// - `script` - Script used to compute the score of documents returned by
/// the `query`
pub fn script_score<Q>(query: Q, script: Script) -> ScriptScoreQuery
where
Q: Into<Query>,
{
ScriptScoreQuery {
query: Box::new(query.into()),
script,
min_score: None,
boost: None,
_name: None,
}
}
}
impl ScriptScoreQuery {
add_boost_and_name!();
}
impl ShouldSkip for ScriptScoreQuery {}
serialize_with_root!("script_score": ScriptScoreQuery);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialization() {
assert_serialize_query(
Query::script_score(
Query::r#match("message", "elasticsearch"),
Script::source("doc['my-int'].value / 10"),
)
.name("_named_query")
.boost(1.1),
json!({
"script_score": {
"_name": "_named_query",
"boost": 1.1,
"query": { "match": { "message": { "query": "elasticsearch" } } },
"script": {
"source": "doc['my-int'].value / 10"
}
}
}),
);
}
}