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