elasticsearch_dsl/search/queries/geo/
geo_bounding_box_query.rs

1use crate::search::*;
2use crate::util::*;
3use serde::Serialize;
4
5/// Matches [geo_point](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html)
6/// and [geo_shape](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html)
7/// values that intersect a bounding box.
8///
9/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html>
10#[derive(Debug, Clone, PartialEq, Serialize)]
11#[serde(remote = "Self")]
12pub struct GeoBoundingBoxQuery {
13    #[serde(skip)]
14    field: String,
15
16    #[serde(skip)]
17    bounding_box: GeoBoundingBox,
18
19    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
20    validation_method: Option<ValidationMethod>,
21
22    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
23    boost: Option<f32>,
24
25    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
26    _name: Option<String>,
27}
28impl Query {
29    /// Creates an instance of [`GeoBoundingBoxQuery`]
30    ///
31    /// - `field` - Field you wish to search.
32    /// - `bounding_box` - A series of vertex coordinates of a geo bounding box
33    pub fn geo_bounding_box<T, U>(field: T, bounding_box: U) -> GeoBoundingBoxQuery
34    where
35        T: ToString,
36        U: Into<GeoBoundingBox>,
37    {
38        GeoBoundingBoxQuery {
39            field: field.to_string(),
40            bounding_box: bounding_box.into(),
41            validation_method: None,
42            boost: None,
43            _name: None,
44        }
45    }
46}
47
48impl GeoBoundingBoxQuery {
49    /// Set to `IGNORE_MALFORMED` to accept geo points with invalid latitude or longitude, set to
50    /// `COERCE` to also try to infer correct latitude or longitude. (default is `STRICT`).
51    pub fn validation_method(mut self, validation_method: ValidationMethod) -> Self {
52        self.validation_method = Some(validation_method);
53        self
54    }
55
56    add_boost_and_name!();
57}
58
59impl ShouldSkip for GeoBoundingBoxQuery {}
60
61serialize_with_root_key_value_pair!("geo_bounding_box": GeoBoundingBoxQuery, field, bounding_box);
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn serialization() {
69        assert_serialize_query(
70            Query::geo_bounding_box(
71                "pin.location",
72                GeoBoundingBox::WellKnownText {
73                    wkt: "BBOX (-74.1, -71.12, 40.73, 40.01)".into(),
74                },
75            ),
76            json!({
77                "geo_bounding_box": {
78                    "pin.location": {
79                        "wkt": "BBOX (-74.1, -71.12, 40.73, 40.01)"
80                    }
81                }
82            }),
83        );
84
85        assert_serialize_query(
86            Query::geo_bounding_box(
87                "pin.location",
88                GeoBoundingBox::MainDiagonal {
89                    top_left: GeoLocation::new(40.73, -74.1),
90                    bottom_right: GeoLocation::new(40.01, -71.12),
91                },
92            )
93            .validation_method(ValidationMethod::Strict)
94            .name("test_name"),
95            json!({
96                "geo_bounding_box": {
97                    "validation_method": "STRICT",
98                    "_name": "test_name",
99                    "pin.location": {
100                        "top_left": [ -74.1, 40.73 ],
101                        "bottom_right": [ -71.12, 40.01 ]
102                    }
103                }
104            }),
105        );
106
107        assert_serialize_query(
108            Query::geo_bounding_box(
109                "pin.location",
110                GeoBoundingBox::Vertices {
111                    top: 40.73,
112                    left: -74.1,
113                    bottom: 40.01,
114                    right: -71.12,
115                },
116            )
117            .validation_method(ValidationMethod::Strict)
118            .name("test_name")
119            .boost(1),
120            json!({
121                "geo_bounding_box": {
122                    "validation_method": "STRICT",
123                    "_name": "test_name",
124                    "boost": 1.0,
125                    "pin.location": {
126                        "top": 40.73,
127                        "left": -74.1,
128                        "bottom": 40.01,
129                        "right": -71.12
130                    }
131                }
132            }),
133        )
134    }
135}