elasticsearch_dsl/search/queries/term_level/
term_query.rs

1use crate::search::*;
2use crate::util::*;
3use serde::Serialize;
4
5/// Returns documents that contain an **exact** term in a provided field.
6///
7/// You can use the term query to find documents based on a precise value such as a price, a product ID, or a username.
8///
9/// To create a term query with numeric values:
10/// ```
11/// # use elasticsearch_dsl::queries::*;
12/// # use elasticsearch_dsl::queries::params::*;
13/// # let query =
14/// Query::term("test", 123);
15/// ```
16/// To create a term query with string values and optional fields:
17/// ```
18/// # use elasticsearch_dsl::queries::*;
19/// # use elasticsearch_dsl::queries::params::*;
20/// # let query =
21/// Query::term("test", "username")
22///     .boost(2)
23///     .name("test");
24/// ```
25/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html>
26#[derive(Debug, Clone, PartialEq, Serialize)]
27#[serde(remote = "Self")]
28pub struct TermQuery {
29    #[serde(skip)]
30    field: String,
31
32    value: Option<Term>,
33
34    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
35    boost: Option<f32>,
36
37    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
38    _name: Option<String>,
39}
40
41impl Query {
42    /// Creates an instance of [`TermQuery`]
43    ///
44    /// - `field` - Field you wish to search.
45    /// - `value` - Term you wish to find in the provided field.
46    ///   To return a document, the term must exactly match the field value, including whitespace and capitalization.
47    pub fn term<T, U>(field: T, value: U) -> TermQuery
48    where
49        T: ToString,
50        U: Serialize,
51    {
52        TermQuery {
53            field: field.to_string(),
54            value: Term::new(value),
55            boost: None,
56            _name: None,
57        }
58    }
59}
60
61impl TermQuery {
62    add_boost_and_name!();
63}
64
65impl ShouldSkip for TermQuery {
66    fn should_skip(&self) -> bool {
67        self.value.should_skip()
68    }
69}
70
71serialize_with_root_keyed!("term": TermQuery);
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn serialization() {
79        assert_serialize_query(
80            Query::term("test", 123),
81            json!({
82                "term": {
83                    "test": {
84                        "value": 123
85                    }
86                }
87            }),
88        );
89
90        assert_serialize_query(
91            Query::term("test", 123).boost(2).name("test"),
92            json!({
93                "term": {
94                    "test": {
95                        "value": 123,
96                        "boost": 2.0,
97                        "_name": "test"
98                    }
99                }
100            }),
101        );
102
103        assert_serialize_query(
104            Query::bool().filter(Query::term("test", None::<String>)),
105            json!({ "bool": {} }),
106        )
107    }
108}