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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use crate::search::*;
use crate::util::*;
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
pub struct CombinedFieldsQuery {
#[serde(rename = "combined_fields")]
inner: Inner,
}
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
struct Inner {
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
fields: Vec<String>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
query: String,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
auto_generate_synonyms_phrase_query: Option<bool>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
operator: Option<Operator>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
minimum_should_match: Option<MinimumShouldMatch>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
zero_terms_query: Option<ZeroTermsQuery>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
boost: Option<Boost>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
_name: Option<String>,
}
impl Query {
pub fn combined_fields<F, S>(fields: F, query: S) -> CombinedFieldsQuery
where
F: IntoIterator,
F::Item: ToString,
S: Into<String>,
{
CombinedFieldsQuery {
inner: Inner {
fields: fields.into_iter().map(|s| s.to_string()).collect(),
query: query.into(),
auto_generate_synonyms_phrase_query: None,
operator: None,
minimum_should_match: None,
zero_terms_query: None,
boost: None,
_name: None,
},
}
}
}
impl CombinedFieldsQuery {
pub fn auto_generate_synonyms_phrase_query(
mut self,
auto_generate_synonyms_phrase_query: bool,
) -> Self {
self.inner.auto_generate_synonyms_phrase_query = Some(auto_generate_synonyms_phrase_query);
self
}
pub fn operator(mut self, operator: Operator) -> Self {
self.inner.operator = Some(operator);
self
}
pub fn minimum_should_match(
mut self,
minimum_should_match: impl Into<MinimumShouldMatch>,
) -> Self {
self.inner.minimum_should_match = Some(minimum_should_match.into());
self
}
pub fn zero_terms_query(mut self, zero_terms_query: ZeroTermsQuery) -> Self {
self.inner.zero_terms_query = Some(zero_terms_query);
self
}
add_boost_and_name!();
}
impl ShouldSkip for CombinedFieldsQuery {
fn should_skip(&self) -> bool {
self.inner.fields.should_skip() || self.inner.query.should_skip()
}
}
#[cfg(test)]
mod tests {
use super::*;
test_serialization! {
with_required_fields(
Query::combined_fields(["test"], "search text"),
json!({
"combined_fields": {
"query": "search text",
"fields": ["test"],
}
})
);
with_all_fields(
Query::combined_fields(["test"], "search text")
.auto_generate_synonyms_phrase_query(true)
.operator(Operator::And)
.minimum_should_match("22")
.zero_terms_query(ZeroTermsQuery::None)
.boost(2)
.name("test"),
json!({
"combined_fields": {
"query": "search text",
"fields": ["test"],
"auto_generate_synonyms_phrase_query": true,
"operator": "AND",
"minimum_should_match": "22",
"zero_terms_query": "none",
"boost": 2,
"_name": "test",
}
})
);
}
}