elasticsearch_dsl/search/queries/geo/
geo_shape_query.rs

1use crate::search::*;
2use crate::util::*;
3use serde::Serialize;
4
5/// Filter documents indexed using the `geo_shape` or `geo_point` type.
6///
7/// Requires the
8/// [`geo_shape` mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html)
9/// or the
10/// [`geo_point` mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html).
11///
12/// The `geo_shape` query uses the same grid square representation as the
13/// `geo_shape` mapping to find documents that have a shape that intersects
14/// with the query shape. It will also use the same Prefix Tree configuration
15/// as defined for the field mapping.
16///
17/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html>
18#[derive(Debug, Clone, PartialEq, Serialize)]
19#[serde(remote = "Self")]
20pub struct GeoShapeQuery {
21    #[serde(skip)]
22    field: String,
23
24    #[serde(skip)]
25    shape: InlineShape,
26
27    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
28    ignore_unmapped: Option<bool>,
29
30    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
31    boost: Option<f32>,
32
33    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
34    _name: Option<String>,
35}
36
37#[derive(Debug, Clone, PartialEq, Serialize)]
38struct InlineShape {
39    shape: GeoShape,
40
41    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
42    relation: Option<SpatialRelation>,
43}
44
45impl Query {
46    /// Creates an instance of [`GeoShapeQuery`]
47    ///
48    /// - `field` - Field you wish to search
49    /// - `shape` - Shape you with to search
50    pub fn geo_shape<S, T>(field: S, shape: T) -> GeoShapeQuery
51    where
52        S: ToString,
53        T: Into<GeoShape>,
54    {
55        GeoShapeQuery {
56            field: field.to_string(),
57            shape: InlineShape {
58                shape: shape.into(),
59                relation: None,
60            },
61            ignore_unmapped: None,
62            boost: None,
63            _name: None,
64        }
65    }
66}
67
68impl GeoShapeQuery {
69    /// The [geo_shape strategy](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html#spatial-strategy)
70    /// mapping parameter determines which spatial relation operators may be
71    /// used at search time.
72    pub fn relation(mut self, relation: SpatialRelation) -> Self {
73        self.shape.relation = Some(relation);
74        self
75    }
76
77    /// When set to true the `ignore_unmapped` option will ignore an unmapped
78    /// field and will not match any documents for this query. This can be
79    /// useful when querying multiple indexes which might have different
80    /// mappings. When set to `false` (the default value) the query will throw
81    /// an exception if the field is not mapped.
82    pub fn ignore_unmapped(mut self, ignore_unmapped: bool) -> Self {
83        self.ignore_unmapped = Some(ignore_unmapped);
84        self
85    }
86
87    add_boost_and_name!();
88}
89
90impl ShouldSkip for GeoShapeQuery {}
91
92serialize_with_root_key_value_pair!("geo_shape": GeoShapeQuery, field, shape);
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    fn test_serialization() {
100        assert_serialize_query(
101            Query::geo_shape("pin.location", GeoShape::point([2.2, 1.1])),
102            json!({
103                "geo_shape": {
104                    "pin.location": {
105                        "shape": {
106                            "type": "point",
107                            "coordinates": [2.2, 1.1]
108                        }
109                    },
110                }
111            }),
112        );
113
114        assert_serialize_query(
115            Query::geo_shape("pin.location", GeoShape::point([2.2, 1.1]))
116                .boost(2)
117                .name("test")
118                .ignore_unmapped(true)
119                .relation(SpatialRelation::Within),
120            json!({
121                "geo_shape": {
122                    "_name": "test",
123                    "boost": 2.0,
124                    "ignore_unmapped": true,
125                    "pin.location": {
126                        "shape": {
127                            "type": "point",
128                            "coordinates": [2.2, 1.1]
129                        },
130                        "relation": "WITHIN"
131                    },
132                }
133            }),
134        );
135    }
136}