elasticsearch_dsl/search/queries/geo/
geo_shape_lookup_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 GeoShapeLookupQuery {
21    #[serde(skip)]
22    field: String,
23
24    #[serde(skip)]
25    shape: Shape,
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 Shape {
39    indexed_shape: IndexedShape,
40
41    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
42    relation: Option<SpatialRelation>,
43}
44
45#[derive(Debug, Clone, PartialEq, Serialize)]
46struct IndexedShape {
47    id: String,
48
49    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
50    index: Option<String>,
51
52    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
53    path: Option<String>,
54
55    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
56    routing: Option<String>,
57}
58
59impl Query {
60    /// Creates an instance of [`GeoShapeLookupQuery`]
61    ///
62    /// - `field` - Field you wish to search
63    /// - `id` - The ID of the document that containing the pre-indexed shape
64    pub fn geo_shape_lookup<S, T>(field: S, id: T) -> GeoShapeLookupQuery
65    where
66        S: ToString,
67        T: ToString,
68    {
69        GeoShapeLookupQuery {
70            field: field.to_string(),
71            shape: Shape {
72                indexed_shape: IndexedShape {
73                    id: id.to_string(),
74                    index: None,
75                    path: None,
76                    routing: None,
77                },
78                relation: None,
79            },
80            ignore_unmapped: None,
81            boost: None,
82            _name: None,
83        }
84    }
85}
86
87impl GeoShapeLookupQuery {
88    /// Name of the index where the pre-indexed shape is. Defaults to shapes
89    pub fn index<S>(mut self, index: S) -> Self
90    where
91        S: ToString,
92    {
93        self.shape.indexed_shape.index = Some(index.to_string());
94        self
95    }
96
97    /// The field specified as path containing the pre-indexed shape. Defaults to shape
98    pub fn path<S>(mut self, path: S) -> Self
99    where
100        S: ToString,
101    {
102        self.shape.indexed_shape.path = Some(path.to_string());
103        self
104    }
105
106    /// The routing of the shape document
107    pub fn routing<S>(mut self, routing: S) -> Self
108    where
109        S: ToString,
110    {
111        self.shape.indexed_shape.routing = Some(routing.to_string());
112        self
113    }
114
115    /// The [geo_shape strategy](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html#spatial-strategy)
116    /// mapping parameter determines which spatial relation operators may be
117    /// used at search time.
118    pub fn relation(mut self, relation: SpatialRelation) -> Self {
119        self.shape.relation = Some(relation);
120        self
121    }
122
123    /// When set to true the `ignore_unmapped` option will ignore an unmapped
124    /// field and will not match any documents for this query. This can be
125    /// useful when querying multiple indexes which might have different
126    /// mappings. When set to `false` (the default value) the query will throw
127    /// an exception if the field is not mapped.
128    pub fn ignore_unmapped(mut self, ignore_unmapped: bool) -> Self {
129        self.ignore_unmapped = Some(ignore_unmapped);
130        self
131    }
132
133    add_boost_and_name!();
134}
135
136impl ShouldSkip for GeoShapeLookupQuery {}
137
138serialize_with_root_key_value_pair!("geo_shape": GeoShapeLookupQuery, field, shape);
139
140#[cfg(test)]
141mod tests {
142    use super::*;
143
144    #[test]
145    fn test_serialization() {
146        assert_serialize_query(
147            Query::geo_shape_lookup("pin.location", "id"),
148            json!({
149                "geo_shape": {
150                    "pin.location": {
151                        "indexed_shape": {
152                            "id": "id",
153                        }
154                    },
155                }
156            }),
157        );
158
159        assert_serialize_query(
160            Query::geo_shape_lookup("pin.location", "id")
161                .boost(2)
162                .name("test")
163                .ignore_unmapped(true)
164                .relation(SpatialRelation::Within)
165                .routing("routing")
166                .index("index")
167                .path("path"),
168            json!({
169                "geo_shape": {
170                    "_name": "test",
171                    "boost": 2.0,
172                    "ignore_unmapped": true,
173                    "pin.location": {
174                        "indexed_shape": {
175                            "id": "id",
176                            "index": "index",
177                            "path": "path",
178                            "routing": "routing"
179                        },
180                        "relation": "WITHIN"
181                    },
182                }
183            }),
184        );
185    }
186}