elasticsearch_dsl/search/queries/params/
geo_query.rs

1use crate::search::*;
2use serde::Serialize;
3
4/// Strategies to verify the correctness of coordinates
5#[derive(Debug, PartialEq, Eq, Clone, Serialize)]
6#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
7pub enum ValidationMethod {
8    /// accept geo points with invalid latitude or longitude
9    IgnoreMalformed,
10
11    /// try to infer correct latitude or longitude
12    Coerce,
13
14    /// strict mode
15    Strict,
16}
17
18/// Different representations of geo bounding box
19#[derive(Debug, PartialEq, Clone, Serialize)]
20#[serde(untagged)]
21pub enum GeoBoundingBox {
22    /// MainDiagonal vertices of geo bounding box
23    MainDiagonal {
24        /// The coordinates of the upper left vertex
25        top_left: GeoLocation,
26        /// The coordinates of the lower right vertex
27        bottom_right: GeoLocation,
28    },
29
30    /// SubDiagonal vertices of geo bounding box
31    SubDiagonal {
32        /// The coordinates of the upper right vertex
33        top_right: GeoLocation,
34        /// The coordinates of the lower left vertex
35        bottom_left: GeoLocation,
36    },
37
38    /// Well-Known Text (WKT).
39    WellKnownText {
40        /// e.g. `BBOX (-74.1, -71.12, 40.73, 40.01)`
41        wkt: String,
42    },
43
44    /// The vertices of the bounding box can either be set by `top_left` and `bottom_right` or by
45    /// `top_right` and `bottom_left` parameters. More over the names `topLeft`, `bottomRight`, `topRight`
46    /// and `bottomLeft` are supported. Instead of setting the values pairwise, one can use the simple
47    /// names `top`, `left`, `bottom` and `right` to set the values separately.
48    Vertices {
49        /// Set top separately
50        top: f32,
51        /// Set left separately
52        left: f32,
53        /// Set bottom separately
54        bottom: f32,
55        /// Set right separately
56        right: f32,
57    },
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use crate::util::*;
64
65    #[test]
66    fn serialization() {
67        assert_serialize(
68            GeoBoundingBox::MainDiagonal {
69                top_left: GeoLocation::new(40.73, -74.1),
70                bottom_right: GeoLocation::new(40.01, -71.12),
71            },
72            json!({
73                "top_left": [-74.1, 40.73],
74                "bottom_right": [-71.12, 40.01]
75            }),
76        );
77
78        assert_serialize(
79            GeoBoundingBox::WellKnownText {
80                wkt: "BBOX (-74.1, -71.12, 40.73, 40.01)".into(),
81            },
82            json!({
83                "wkt": "BBOX (-74.1, -71.12, 40.73, 40.01)"
84            }),
85        );
86
87        assert_serialize(
88            GeoBoundingBox::Vertices {
89                top: 40.73,
90                left: -74.1,
91                bottom: 40.01,
92                right: -71.12,
93            },
94            json!({
95                "top": 40.73,
96                "left": -74.1,
97                "bottom": 40.01,
98                "right": -71.12
99            }),
100        );
101    }
102}