Skip to content

Commit 6a51e96

Browse files
authored
Enable geo_distance and geo_bounding_box queries on geo_shape field type (#64224) (#64326)
1 parent 193740c commit 6a51e96

File tree

11 files changed

+626
-309
lines changed

11 files changed

+626
-309
lines changed

docs/reference/query-dsl/geo-bounding-box-query.asciidoc

+99-11
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
<titleabbrev>Geo-bounding box</titleabbrev>
55
++++
66

7-
A query allowing to filter hits based on a point location using a
8-
bounding box. Assuming the following indexed document:
7+
Matches <<geo-point,`geo_point`>> and <<geo-shape,`geo_shape`>> values that
8+
intersect a bounding box.
9+
10+
[discrete]
11+
[[geo-bounding-box-query-ex]]
12+
==== Example
13+
Assume the following the following documents are indexed:
914

1015
[source,console]
1116
--------------------------------------------------
@@ -33,11 +38,36 @@ PUT /my_locations/_doc/1
3338
}
3439
}
3540
}
41+
42+
PUT /my_geoshapes
43+
{
44+
"mappings": {
45+
"properties": {
46+
"pin": {
47+
"properties": {
48+
"location": {
49+
"type": "geo_shape"
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
57+
PUT /my_geoshapes/_doc/1
58+
{
59+
"pin": {
60+
"location": {
61+
"type" : "polygon",
62+
"coordinates" : [[[13.0 ,51.5], [15.0, 51.5], [15.0, 54.0], [13.0, 54.0], [13.0 ,51.5]]]
63+
}
64+
}
65+
}
3666
--------------------------------------------------
3767
// TESTSETUP
3868

39-
Then the following simple query can be executed with a
40-
`geo_bounding_box` filter:
69+
Use a `geo_bounding_box` filter to match `geo_point` values that intersect a bounding
70+
box. To define the box, provide geopoint values for two opposite corners.
4171

4272
[source,console]
4373
--------------------------------------------------
@@ -67,6 +97,66 @@ GET my_locations/_search
6797
}
6898
--------------------------------------------------
6999

100+
Use the same filter to match `geo_shape` values that intersect the bounding box:
101+
102+
[source,console]
103+
--------------------------------------------------
104+
GET my_geoshapes/_search
105+
{
106+
"query": {
107+
"bool": {
108+
"must": {
109+
"match_all": {}
110+
},
111+
"filter": {
112+
"geo_bounding_box": {
113+
"pin.location": {
114+
"top_left": {
115+
"lat": 40.73,
116+
"lon": -74.1
117+
},
118+
"bottom_right": {
119+
"lat": 40.01,
120+
"lon": -71.12
121+
}
122+
}
123+
}
124+
}
125+
}
126+
}
127+
}
128+
--------------------------------------------------
129+
130+
To match both `geo_point` and `geo_shape` values, search both indices:
131+
132+
[source,console]
133+
--------------------------------------------------
134+
GET my_locations,my_geoshapes/_search
135+
{
136+
"query": {
137+
"bool": {
138+
"must": {
139+
"match_all": {}
140+
},
141+
"filter": {
142+
"geo_bounding_box": {
143+
"pin.location": {
144+
"top_left": {
145+
"lat": 40.73,
146+
"lon": -74.1
147+
},
148+
"bottom_right": {
149+
"lat": 40.01,
150+
"lon": -71.12
151+
}
152+
}
153+
}
154+
}
155+
}
156+
}
157+
}
158+
--------------------------------------------------
159+
70160
[discrete]
71161
==== Query Options
72162

@@ -291,13 +381,6 @@ GET my_locations/_search
291381
}
292382
--------------------------------------------------
293383

294-
295-
[discrete]
296-
==== geo_point Type
297-
298-
The filter *requires* the `geo_point` type to be set on the relevant
299-
field.
300-
301384
[discrete]
302385
==== Multi Location Per Document
303386

@@ -366,3 +449,8 @@ the upper bounds (top and right edges) might be selected by the query even if
366449
they are located slightly outside the edge. The rounding error should be less
367450
than 4.20e-8 degrees on the latitude and less than 8.39e-8 degrees on the
368451
longitude, which translates to less than 1cm error even at the equator.
452+
453+
Geoshapes also have limited precision due to rounding. Geoshape edges along the
454+
bounding box's bottom and left edges may not match a `geo_bounding_box` query.
455+
Geoshape edges slightly outside the box's top and right edges may still match
456+
the query.

docs/reference/query-dsl/geo-distance-query.asciidoc

+86-11
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
<titleabbrev>Geo-distance</titleabbrev>
55
++++
66

7-
Filters documents that include only hits that exists within a specific
8-
distance from a geo point. Assuming the following mapping and indexed
9-
document:
7+
Matches <<geo-point,`geo_point`>> and <<geo-shape,`geo_shape`>> values within
8+
a given distance of a geopoint.
9+
10+
[discrete]
11+
[[geo-distance-query-ex]]
12+
==== Example
13+
14+
Assume the following the following documents are indexed:
1015

1116
[source,console]
1217
--------------------------------------------------
@@ -34,12 +39,37 @@ PUT /my_locations/_doc/1
3439
}
3540
}
3641
}
42+
43+
PUT /my_geoshapes
44+
{
45+
"mappings": {
46+
"properties": {
47+
"pin": {
48+
"properties": {
49+
"location": {
50+
"type": "geo_shape"
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
57+
58+
PUT /my_geoshapes/_doc/1
59+
{
60+
"pin": {
61+
"location": {
62+
"type" : "polygon",
63+
"coordinates" : [[[13.0 ,51.5], [15.0, 51.5], [15.0, 54.0], [13.0, 54.0], [13.0 ,51.5]]]
64+
}
65+
}
66+
}
3767
--------------------------------------------------
3868
// TESTSETUP
3969

4070

41-
Then the following simple query can be executed with a `geo_distance`
42-
filter:
71+
Use a `geo_distance` filter to match `geo_point` values within a specified
72+
distance of another geopoint:
4373

4474
[source,console]
4575
--------------------------------------------------
@@ -64,6 +94,57 @@ GET /my_locations/_search
6494
}
6595
--------------------------------------------------
6696

97+
Use the same filter to match `geo_shape` values within the given distance:
98+
99+
[source,console]
100+
--------------------------------------------------
101+
GET my_geoshapes/_search
102+
{
103+
"query": {
104+
"bool": {
105+
"must": {
106+
"match_all": {}
107+
},
108+
"filter": {
109+
"geo_distance": {
110+
"distance": "200km",
111+
"pin.location": {
112+
"lat": 40,
113+
"lon": -70
114+
}
115+
}
116+
}
117+
}
118+
}
119+
}
120+
--------------------------------------------------
121+
122+
To match both `geo_point` and `geo_shape` values, search both indices:
123+
124+
[source,console]
125+
--------------------------------------------------
126+
GET my_locations,my_geoshapes/_search
127+
{
128+
"query": {
129+
"bool": {
130+
"must": {
131+
"match_all": {}
132+
},
133+
"filter": {
134+
"geo_distance": {
135+
"distance": "200km",
136+
"pin.location": {
137+
"lat": 40,
138+
"lon": -70
139+
}
140+
}
141+
}
142+
}
143+
}
144+
}
145+
--------------------------------------------------
146+
147+
67148
[discrete]
68149
==== Accepted Formats
69150

@@ -198,12 +279,6 @@ The following are options allowed on the filter:
198279
longitude, set to `COERCE` to additionally try and infer correct
199280
coordinates (default is `STRICT`).
200281

201-
[discrete]
202-
==== geo_point Type
203-
204-
The filter *requires* the `geo_point` type to be set on the relevant
205-
field.
206-
207282
[discrete]
208283
==== Multi Location Per Document
209284

0 commit comments

Comments
 (0)