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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use serde::{Deserialize, Deserializer, Serialize};
use crate::{search::*, util::*};
/// Queries documents that contain fields indexed using the `shape` type.
///
/// Requires the [`shape` Mapping](https://www.elastic.co/guide/en/opensearch/reference/current/shape.html).
///
/// <https://www.elastic.co/guide/en/opensearch/reference/current/query-dsl-shape-query.html>
#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Self")]
pub struct ShapeQuery {
#[serde(skip)]
field: String,
#[serde(skip)]
shape: InlineShape,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
ignore_unmapped: Option<bool>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
boost: Option<f32>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
_name: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
struct InlineShape {
shape: Shape,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
relation: Option<SpatialRelation>,
}
impl Query {
/// Creates an instance of [`ShapeQuery`]
///
/// - `field` - Field you wish to search
/// - `shape` - Shape you with to search
pub fn shape<S, T>(field: S, shape: T) -> ShapeQuery
where
S: ToString,
T: Into<Shape>,
{
ShapeQuery {
field: field.to_string(),
shape: InlineShape {
shape: shape.into(),
relation: None,
},
ignore_unmapped: None,
boost: None,
_name: None,
}
}
}
impl ShapeQuery {
add_boost_and_name!();
/// The [shape strategy](https://www.elastic.co/guide/en/opensearch/reference/current/geo-shape.html#spatial-strategy)
/// mapping parameter determines which spatial relation operators may be
/// used at search time.
pub fn relation(mut self, relation: SpatialRelation) -> Self {
self.shape.relation = Some(relation);
self
}
/// When set to true the `ignore_unmapped` option will ignore an unmapped
/// field and will not match any documents for this query. This can be
/// useful when querying multiple indexes which might have different
/// mappings. When set to `false` (the default value) the query will throw
/// an exception if the field is not mapped.
pub fn ignore_unmapped(mut self, ignore_unmapped: bool) -> Self {
self.ignore_unmapped = Some(ignore_unmapped);
self
}
}
impl ShouldSkip for ShapeQuery {}
serialize_with_root_key_value_pair!("shape": ShapeQuery, field, shape);
impl<'de> Deserialize<'de> for ShapeQuery {
fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
use std::fmt;
struct WrapperVisitor;
impl<'de> serde::de::Visitor<'de> for WrapperVisitor {
type Value = ShapeQuery;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("struct shape")
}
fn visit_map<A>(self, mut map: A) -> Result<ShapeQuery, A::Error>
where
A: serde::de::MapAccess<'de>,
{
let mut query: ShapeQuery = ShapeQuery::default();
let mut found_value = false;
while let Some(key) = map.next_key::<String>()? {
if key == "shape" {
let inner_map =
map.next_value::<serde_json::Map<String, serde_json::Value>>()?;
for (k, v) in inner_map.iter() {
match k.as_str() {
"field" => match v.as_str() {
Some(field) => {
query.field = field.to_string();
}
None => {
return Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Other("not a string"),
&"a string",
));
}
},
"boost" => match v.as_f64() {
Some(boost) => {
query.boost = Some(boost as f32);
}
None => {
return Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Other("not a float"),
&"a float",
));
}
},
"_name" => match v.as_str() {
Some(_name) => {
query._name = Some(_name.to_string());
}
None => {
return Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Other("not a string"),
&"a string",
));
}
},
"ignore_unmapped" => match v.as_bool() {
Some(ignore_unmapped) => {
query.ignore_unmapped = Some(ignore_unmapped);
}
None => {
return Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Other("not a boolean"),
&"a boolean",
));
}
},
"value" => {
let shape = serde_json::from_value::<InlineShape>(v.clone());
match shape {
Ok(shape) => {
query.shape = shape;
found_value = true;
}
Err(e) => {
return Err(serde::de::Error::custom(format!(
"error parsing distance: {}",
e
)));
}
}
}
_ => {
query.field = k.to_owned();
let value = serde_json::from_value::<InlineShape>(v.clone());
match value {
Ok(value) => {
query.shape = value;
found_value = true;
}
Err(e) => {
return Err(serde::de::Error::custom(format!(
"error parsing {}: {}",
k, e
)));
}
}
}
}
}
}
}
if found_value {
Ok(query)
} else {
Err(serde::de::Error::missing_field("location value"))
}
}
}
deserializer.deserialize_struct("Wrapper", &["shape"], WrapperVisitor)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serialization() {
assert_serialize_query(
Query::shape("pin.location", Shape::point([2.2, 1.1])),
json!({
"shape": {
"pin.location": {
"shape": {
"type": "point",
"coordinates": [2.2, 1.1]
}
},
}
}),
);
assert_serialize_query(
Query::shape("pin.location", Shape::point([2.2, 1.1]))
.boost(2)
.name("test")
.ignore_unmapped(true)
.relation(SpatialRelation::Within),
json!({
"shape": {
"_name": "test",
"boost": 2.0,
"ignore_unmapped": true,
"pin.location": {
"shape": {
"type": "point",
"coordinates": [2.2, 1.1]
},
"relation": "WITHIN"
},
}
}),
);
}
}