elasticsearch_dsl/search/queries/geo/
geo_shape_query.rs1use crate::search::*;
2use crate::util::*;
3use serde::Serialize;
4
5#[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 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 pub fn relation(mut self, relation: SpatialRelation) -> Self {
73 self.shape.relation = Some(relation);
74 self
75 }
76
77 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}