elasticsearch_dsl/search/queries/geo/
geo_shape_lookup_query.rs1use crate::search::*;
2use crate::util::*;
3use serde::Serialize;
4
5#[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 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 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 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 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 pub fn relation(mut self, relation: SpatialRelation) -> Self {
119 self.shape.relation = Some(relation);
120 self
121 }
122
123 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}