Skip to main content

opensearch_dsl/search/queries/params/
terms_set_query.rs

1use serde_json::Value;
2
3/// Number of matching terms to be required
4#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
5pub enum TermsSetMinimumShouldMatch {
6    /// [Numeric](https://www.elastic.co/guide/en/opensearch/reference/current/number.html)
7    /// field containing the number of matching terms required to return a
8    /// document.
9    #[serde(rename = "minimum_should_match_field")]
10    Field(String),
11
12    /// Custom script containing the number of matching terms required to return a
13    /// document.
14    ///
15    /// For parameters and valid values, see
16    /// [Scripting](https://www.elastic.co/guide/en/opensearch/reference/current/modules-scripting.html).
17    ///
18    /// For an example query using the `minimum_should_match_script` parameter,
19    /// see [How to use the `minimum_should_match_script` parameter](https://www.elastic.co/guide/en/opensearch/reference/current/query-dsl-terms-set-query.html#terms-set-query-script).
20    #[serde(rename = "minimum_should_match_script")]
21    Script(TermsSetScript),
22}
23
24impl From<String> for TermsSetMinimumShouldMatch {
25    fn from(field: String) -> Self {
26        Self::Field(field)
27    }
28}
29
30impl<'a> From<&'a str> for TermsSetMinimumShouldMatch {
31    fn from(field: &'a str) -> Self {
32        Self::Field(field.to_string())
33    }
34}
35
36impl From<TermsSetScript> for TermsSetMinimumShouldMatch {
37    fn from(script: TermsSetScript) -> Self {
38        Self::Script(script)
39    }
40}
41
42/// Custom script containing the number of matching terms required to return a
43/// document.
44///
45/// For parameters and valid values, see
46/// [Scripting](https://www.elastic.co/guide/en/opensearch/reference/current/modules-scripting.html).
47#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
48pub struct TermsSetScript {
49    source: String,
50    params: Option<Value>,
51}
52
53impl From<String> for TermsSetScript {
54    fn from(source: String) -> Self {
55        Self {
56            source,
57            params: None,
58        }
59    }
60}
61
62impl<'a> From<&'a str> for TermsSetScript {
63    fn from(source: &'a str) -> Self {
64        Self {
65            source: source.to_string(),
66            params: None,
67        }
68    }
69}
70
71impl TermsSetScript {
72    /// Creates an instance of [TermsSetScript]
73    pub fn new<T>(source: T) -> Self
74    where
75        T: ToString,
76    {
77        Self {
78            source: source.to_string(),
79            params: None,
80        }
81    }
82
83    /// Assign params
84    pub fn params(mut self, params: Value) -> Self {
85        self.params = Some(params);
86        self
87    }
88}