os_query_builder_rs/full_text/
match.rs1use serde::{Serialize, Serializer};
2use serde::ser::SerializeMap;
3use serde_json::Value;
4use crate::misc::fuzziness::Fuzziness;
5
6use crate::misc::operator::Operator;
7use crate::misc::zero_terms_query::ZeroTermsQuery;
8
9#[derive(Debug, Default, Clone)]
10pub struct Match {
11 field: Option<String>,
12 value: MatchValues,
13}
14
15#[derive(Debug, Default, Clone, Serialize)]
16pub struct MatchValues {
17 query: Option<Value>,
18
19 #[serde(skip_serializing_if = "Option::is_none")]
20 fuzziness: Option<Fuzziness>,
21
22 #[serde(skip_serializing_if = "Option::is_none")]
23 fuzzy_transpositions: Option<bool>,
24
25 #[serde(skip_serializing_if = "Option::is_none")]
26 operator: Option<Operator>,
27
28 #[serde(skip_serializing_if = "Option::is_none")]
29 minimum_should_match: Option<String>,
30
31 #[serde(skip_serializing_if = "Option::is_none")]
32 analyzer: Option<String>,
33
34 #[serde(skip_serializing_if = "Option::is_none")]
35 zero_terms_query: Option<ZeroTermsQuery>,
36
37 #[serde(skip_serializing_if = "Option::is_none")]
38 lenient: Option<bool>,
39
40 #[serde(skip_serializing_if = "Option::is_none")]
41 prefix_length: Option<u64>,
42
43 #[serde(skip_serializing_if = "Option::is_none")]
44 max_expansions: Option<u64>,
45
46 #[serde(skip_serializing_if = "Option::is_none")]
47 boost: Option<f64>,
48}
49
50impl Serialize for Match {
51 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
52 where
53 S: Serializer,
54 {
55 let mut state = serializer.serialize_map(Some(1))?;
56 state.serialize_entry(self.field.as_deref().unwrap_or_default(), &self.value)?;
57 state.end()
58 }
59}
60
61impl Match {
62 pub fn new() -> Self {
63 Self::default()
64 }
65
66 pub fn field<T: Into<String>>(self, field: T) -> Self {
67 Self {
68 field: Some(field.into()),
69 ..self
70 }
71 }
72
73 pub fn value<T: Into<Value>>(self, val: T) -> Self {
74 let value = MatchValues {
75 query: Some(val.into()),
76 ..self.value
77 };
78 Self { value, ..self }
79 }
80
81 pub fn query<T: Into<Value>>(self, value: T) -> Self {
82 self.value(value)
83 }
84
85 pub fn operator<T: Into<Operator>>(self, operator: T) -> Self {
86 let value = MatchValues {
87 operator: Some(operator.into()),
88 ..self.value
89 };
90 Self { value, ..self }
91 }
92
93 pub fn analyzer<T: Into<String>>(self, analyzer: T) -> Self {
94 let value = MatchValues {
95 analyzer: Some(analyzer.into()),
96 ..self.value
97 };
98 Self { value, ..self }
99 }
100
101 pub fn zero_terms_query<T: Into<ZeroTermsQuery>>(self, zero_terms_query: T) -> Self {
102 let value = MatchValues {
103 zero_terms_query: Some(zero_terms_query.into()),
104 ..self.value
105 };
106 Self { value, ..self }
107 }
108
109 pub fn lenient(self, lenient: bool) -> Self {
110 let value = MatchValues {
111 lenient: Some(lenient),
112 ..self.value
113 };
114 Self { value, ..self }
115 }
116
117 pub fn prefix_length<T: Into<u64>>(self, prefix_length: T) -> Self {
118 let value = MatchValues {
119 prefix_length: Some(prefix_length.into()),
120 ..self.value
121 };
122 Self { value, ..self }
123 }
124
125 pub fn max_expansions<T: Into<u64>>(self, max_expansions: T) -> Self {
126 let value = MatchValues {
127 max_expansions: Some(max_expansions.into()),
128 ..self.value
129 };
130 Self { value, ..self }
131 }
132
133 pub fn boost<T: Into<f64>>(self, boost: T) -> Self {
134 let value = MatchValues {
135 boost: Some(boost.into()),
136 ..self.value
137 };
138 Self { value, ..self }
139 }
140
141 pub fn fuzziness<T: Into<Fuzziness>>(self, fuzziness: T) -> Self {
142 let value = MatchValues {
143 fuzziness: Some(fuzziness.into()),
144 ..self.value
145 };
146 Self { value, ..self }
147 }
148
149 pub fn fuzzy_transpositions(self, fuzzy_transpositions: bool) -> Self {
150 let value = MatchValues {
151 fuzzy_transpositions: Some(fuzzy_transpositions),
152 ..self.value
153 };
154 Self { value, ..self }
155 }
156
157 pub fn minimum_should_match<T: Into<String>>(self, minimum_should_match: T) -> Self {
158 let value = MatchValues {
159 minimum_should_match: Some(minimum_should_match.into()),
160 ..self.value
161 };
162 Self { value, ..self }
163 }
164
165}