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
use crate::search::*;
use crate::util::*;

/// Returns documents that contain one or more **exact** terms in a provided field.
/// The terms query is the same as the term query, except you can search for multiple values.
///
/// To create a terms query with numeric values:
/// ```
/// # use elasticsearch_dsl::queries::*;
/// # use elasticsearch_dsl::queries::params::*;
/// # let query =
/// Query::terms("test", vec![123]);
/// ```
/// To create a terms query with string values and optional fields:
/// ```
/// # use elasticsearch_dsl::queries::*;
/// # use elasticsearch_dsl::queries::params::*;
/// # let query =
/// Query::terms("test", vec!["username"])
///     .boost(2)
///     .name("test");
/// ```
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html>
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(remote = "Self")]
pub struct TermsQuery {
    #[serde(skip)]
    field: String,

    #[serde(skip)]
    terms: Terms,

    #[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 [`TermsQuery`]
    ///
    /// - `field` - Field you wish to search.
    /// - `values` - An array of terms you wish to find in the provided field. To return a
    /// document, one or more terms must exactly match a field value,
    /// including whitespace and capitalization.<br/>
    /// By default, Elasticsearch limits the `terms` query to a maximum of
    /// 65,536 terms. You can change this limit using the
    /// [`index.max_terms_count setting`](https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-max-terms-count).<br/>
    /// > To use the field values of an existing document as search terms,
    /// use the terms lookup parameters.
    pub fn terms<S, I>(field: S, terms: I) -> TermsQuery
    where
        S: ToString,
        I: Into<Terms>,
    {
        TermsQuery {
            field: field.to_string(),
            terms: terms.into(),
            boost: None,
            _name: None,
        }
    }
}

impl TermsQuery {
    add_boost_and_name!();
}

impl ShouldSkip for TermsQuery {
    fn should_skip(&self) -> bool {
        self.terms.should_skip()
    }
}

serialize_with_root_key_value_pair!("terms": TermsQuery, field, terms);

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

    #[test]
    fn serialization() {
        assert_serialize_query(
            Query::terms("test", [12, 13, 123]),
            json!({"terms": { "test": [12, 13, 123] } }),
        );

        assert_serialize_query(
            Query::terms("test", [123]).boost(2).name("test"),
            json!({
                "terms": {
                    "test": [123],
                    "boost": 2.0,
                    "_name": "test",
                }
            }),
        );
    }

    #[test]
    fn should_skip_when_there_are_no_values() {
        let values: Vec<i32> = Vec::new();
        let query = Query::terms("test", values);

        assert!(query.should_skip())
    }

    #[test]
    fn should_not_skip_when_there_are_no_values() {
        let query = Query::terms("test", [123]);

        assert!(!query.should_skip())
    }
}