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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use serde::{Deserialize, Deserializer, Serialize};
use super::{SortMode, SortOrder};
use crate::{
util::{KeyValuePair, ShouldSkip},
DistanceUnit, GeoDistanceType, GeoLocation,
};
/// Sorts search hits by other field values
///
/// <https://www.elastic.co/guide/en/opensearch/reference/current/sort-search-results.html#sort-search-results>
#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Self")]
pub struct GeoDistanceSort {
#[serde(skip)]
field: String,
#[serde(skip)]
points: Vec<GeoLocation>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
order: Option<SortOrder>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
unit: Option<DistanceUnit>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
mode: Option<SortMode>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
distance_type: Option<GeoDistanceType>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
ignore_unmapped: Option<bool>,
}
impl GeoDistanceSort {
/// Creates an instance of [GeoDistanceSort]
pub fn new<T, U>(field: T, points: U) -> Self
where
T: ToString,
U: IntoIterator,
U::Item: Into<GeoLocation>,
{
Self {
field: field.to_string(),
points: points.into_iter().map(Into::into).collect(),
order: None,
unit: None,
mode: None,
distance_type: None,
ignore_unmapped: None,
}
}
/// Creates an instance of [GeoDistanceSort] by ascending order
pub fn ascending<T, U>(field: T, points: U) -> Self
where
T: ToString,
U: IntoIterator,
U::Item: Into<GeoLocation>,
{
Self::new(field, points).order(SortOrder::Asc)
}
/// Creates an instance of [GeoDistanceSort] by descending order
pub fn descending<T, U>(field: T, points: U) -> Self
where
T: ToString,
U: IntoIterator,
U::Item: Into<GeoLocation>,
{
Self::new(field, points).order(SortOrder::Desc)
}
/// Explicit order
///
/// <https://www.elastic.co/guide/en/opensearch/reference/current/sort-search-results.html#_sort_order>
pub fn order(mut self, order: SortOrder) -> Self {
self.order = Some(order);
self
}
/// The unit to use when computing sort values
///
/// <https://www.elastic.co/guide/en/opensearch/reference/current/sort-search-results.html#geo-sorting>
pub fn unit(mut self, unit: DistanceUnit) -> Self {
self.unit = Some(unit);
self
}
/// Sort mode for numeric fields
///
/// <https://www.elastic.co/guide/en/opensearch/reference/current/sort-search-results.html#_sort_mode_option>
pub fn mode(mut self, mode: SortMode) -> Self {
self.mode = Some(mode);
self
}
/// How to compute the distance. Can either be arc (default), or plane
/// (faster, but inaccurate on long distances and close to the poles).
///
/// <https://www.elastic.co/guide/en/opensearch/reference/current/sort-search-results.html#_sort_mode_option>
pub fn distance_type(mut self, distance_type: GeoDistanceType) -> Self {
self.distance_type = Some(distance_type);
self
}
/// Indicates if the unmapped field should be treated as a missing value.
/// Setting it to `true` is equivalent to specifying an `unmapped_type` in
/// the field sort. The default is `false` (unmapped field cause the search
/// to fail).
///
/// <https://www.elastic.co/guide/en/opensearch/reference/current/sort-search-results.html#_sort_mode_option>
pub fn ignore_unmapped(mut self, ignore_unmapped: bool) -> Self {
self.ignore_unmapped = Some(ignore_unmapped);
self
}
}
impl IntoIterator for GeoDistanceSort {
type IntoIter = std::option::IntoIter<Self::Item>;
type Item = Self;
fn into_iter(self) -> Self::IntoIter {
Some(self).into_iter()
}
}
serialize_with_root_key_value_pair!("_geo_distance": GeoDistanceSort, field, points);
impl<'de> Deserialize<'de> for GeoDistanceSort {
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 = GeoDistanceSort;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("struct shape")
}
fn visit_map<A>(self, mut map: A) -> Result<GeoDistanceSort, A::Error>
where
A: serde::de::MapAccess<'de>,
{
let mut query: GeoDistanceSort = GeoDistanceSort::default();
let mut found_value = false;
while let Some(key) = map.next_key::<String>()? {
if key == "_geo_distance" {
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",
));
}
},
"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",
));
}
},
"order" => {
let value = serde_json::from_value::<SortOrder>(v.clone());
match value {
Ok(value) => {
query.order = Some(value);
}
Err(e) => {
return Err(serde::de::Error::custom(format!(
"error parsing order: {}",
e
)));
}
}
}
"unit" => {
let value = serde_json::from_value::<DistanceUnit>(v.clone());
match value {
Ok(value) => {
query.unit = Some(value);
}
Err(e) => {
return Err(serde::de::Error::custom(format!(
"error parsing unit: {}",
e
)));
}
}
}
"mode" => {
let value = serde_json::from_value::<SortMode>(v.clone());
match value {
Ok(value) => {
query.mode = Some(value);
}
Err(e) => {
return Err(serde::de::Error::custom(format!(
"error parsing mode: {}",
e
)));
}
}
}
"distance_type" => {
let value =
serde_json::from_value::<GeoDistanceType>(v.clone());
match value {
Ok(value) => {
query.distance_type = Some(value);
}
Err(e) => {
return Err(serde::de::Error::custom(format!(
"error parsing distance_type: {}",
e
)));
}
}
}
_ => {
query.field = k.to_owned();
let value =
serde_json::from_value::<Vec<GeoLocation>>(v.clone());
match value {
Ok(value) => {
query.points = 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("point values"))
}
}
}
deserializer.deserialize_struct("Wrapper", &["_geo_distance"], WrapperVisitor)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::assert_serialize;
#[test]
fn serialization() {
assert_serialize(
GeoDistanceSort::new("test", GeoLocation::new(1.2, 3.3)),
json!({
"_geo_distance": {
"test": [ [3.3, 1.2] ]
}
}),
);
assert_serialize(
GeoDistanceSort::ascending("test", GeoLocation::new(1.2, 3.3)),
json!({
"_geo_distance": {
"test": [ [3.3, 1.2] ],
"order": "asc",
}
}),
);
assert_serialize(
GeoDistanceSort::descending("test", GeoLocation::new(1.2, 3.3))
.order(SortOrder::Asc)
.unit(DistanceUnit::Inches)
.mode(SortMode::Max)
.distance_type(GeoDistanceType::Arc)
.ignore_unmapped(true),
json!({
"_geo_distance": {
"test": [ [3.3, 1.2] ],
"unit": "in",
"order": "asc",
"mode": "max",
"distance_type": "arc",
"ignore_unmapped": true,
}
}),
);
}
}