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
use serde_json::Value;

/// Number of matching terms to be required
#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum TermsSetMinimumShouldMatch {
    /// [Numeric](https://www.elastic.co/guide/en/elasticsearch/reference/current/number.html)
    /// field containing the number of matching terms required to return a document.
    #[serde(rename = "minimum_should_match_field")]
    Field(String),

    /// Custom script containing the number of matching terms required to return a document.
    ///
    /// For parameters and valid values, see
    /// [Scripting](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html).
    ///
    /// For an example query using the `minimum_should_match_script` parameter, see
    /// [How to use the `minimum_should_match_script` parameter](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html#terms-set-query-script).
    #[serde(rename = "minimum_should_match_script")]
    Script(TermsSetScript),
}

impl From<String> for TermsSetMinimumShouldMatch {
    fn from(field: String) -> Self {
        Self::Field(field)
    }
}

impl<'a> From<&'a str> for TermsSetMinimumShouldMatch {
    fn from(field: &'a str) -> Self {
        Self::Field(field.to_string())
    }
}

impl From<TermsSetScript> for TermsSetMinimumShouldMatch {
    fn from(script: TermsSetScript) -> Self {
        Self::Script(script)
    }
}

/// Custom script containing the number of matching terms required to return a document.
///
/// For parameters and valid values, see
/// [Scripting](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html).
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct TermsSetScript {
    source: String,
    params: Option<Value>,
}

impl From<String> for TermsSetScript {
    fn from(source: String) -> Self {
        Self {
            source,
            params: None,
        }
    }
}

impl<'a> From<&'a str> for TermsSetScript {
    fn from(source: &'a str) -> Self {
        Self {
            source: source.to_string(),
            params: None,
        }
    }
}

impl TermsSetScript {
    /// Creates an instance of [TermsSetScript]
    pub fn new(source: impl Into<String>) -> Self {
        Self {
            source: source.into(),
            params: None,
        }
    }

    /// Assign params
    pub fn params(mut self, params: Value) -> Self {
        self.params = Some(params);
        self
    }
}