opensearch-dsl 0.3.1

Strongly typed OpenSearch DSL
Documentation
use crate::{search::*, 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 opensearch_dsl::queries::*;
/// # use opensearch_dsl::queries::params::*;
/// # let query =
/// Query::script_score(
///   Query::r#match("message", "opensearch"),
///   Script::source("doc['my-int'].value / 10"),
/// );
/// ```
/// <https://www.elastic.co/guide/en/opensearch/reference/current/query-dsl-script-score-query.html>
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Self")]
pub struct ScriptScoreQuery {
    query: Box<Query>,

    script: Script,

    #[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
    min_score: 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 {
    /// 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);
deserialize_with_root!("script_score": ScriptScoreQuery);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn serialization() {
        assert_serialize_query(
            Query::script_score(
                Query::r#match("message", "opensearch"),
                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": "opensearch" } } },
                    "script": {
                        "source": "doc['my-int'].value / 10"
                    }
                }
            }),
        );
    }
}