Skip to content

Enable geo_distance and geo_bounding_box queries on geo_shape field type #64224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 95 additions & 10 deletions docs/reference/query-dsl/geo-bounding-box-query.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<titleabbrev>Geo-bounding box</titleabbrev>
++++

A query allowing to filter hits based on a point location using a
bounding box. Assuming the following indexed document:
A query allowing to filter hits based on the intersection of a bounding box with
data stored on a `geo_shape` or `geo_point` field. Assuming the following indexed documents:

[source,console]
--------------------------------------------------
Expand Down Expand Up @@ -33,11 +33,36 @@ PUT /my_locations/_doc/1
}
}
}

PUT /my_geoshapes
{
"mappings": {
"properties": {
"pin": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
}
}

PUT /my_geoshapes/_doc/1
{
"pin": {
"location": {
"type" : "polygon",
"coordinates" : [[[13.0 ,51.5], [15.0, 51.5], [15.0, 54.0], [13.0, 54.0], [13.0 ,51.5]]]
}
}
}
--------------------------------------------------
// TESTSETUP

Then the following simple query can be executed with a
`geo_bounding_box` filter:
`geo_bounding_box` filter against a `geo_point` field:

[source,console]
--------------------------------------------------
Expand Down Expand Up @@ -67,6 +92,67 @@ GET my_locations/_search
}
--------------------------------------------------


The same simple query can be executed against the 'geo_shape field:

[source,console]
--------------------------------------------------
GET my_locations,my_geoshapes/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_bounding_box": {
"pin.location": {
"top_left": {
"lat": 40.73,
"lon": -74.1
},
"bottom_right": {
"lat": 40.01,
"lon": -71.12
}
}
}
}
}
}
}
--------------------------------------------------

Or it can be executed in both indexes at the same time:

[source,console]
--------------------------------------------------
GET my_geoshapes/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_bounding_box": {
"pin.location": {
"top_left": {
"lat": 40.73,
"lon": -74.1
},
"bottom_right": {
"lat": 40.01,
"lon": -71.12
}
}
}
}
}
}
}
--------------------------------------------------

[discrete]
==== Query Options

Expand Down Expand Up @@ -291,13 +377,6 @@ GET my_locations/_search
}
--------------------------------------------------


[discrete]
==== geo_point Type

The filter *requires* the `geo_point` type to be set on the relevant
field.

[discrete]
==== Multi Location Per Document

Expand Down Expand Up @@ -366,3 +445,9 @@ the upper bounds (top and right edges) might be selected by the query even if
they are located slightly outside the edge. The rounding error should be less
than 4.20e-8 degrees on the latitude and less than 8.39e-8 degrees on the
longitude, which translates to less than 1cm error even at the equator.

Likewise, geoshapes have the same precision limitations. Therefore, shape edges
along the lower bounds (bottom and left edges of the bounding box) might not make it into
the bounding box due to the rounding error. At the same time edges alongs
the upper bounds (top and right edges) might be selected by the query even if
they are located slightly outside the edge.
88 changes: 79 additions & 9 deletions docs/reference/query-dsl/geo-distance-query.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
++++

Filters documents that include only hits that exists within a specific
distance from a geo point. Assuming the following mapping and indexed
document:
distance from a `geo_shape` or `geo_point` field. Assuming the following mapping and indexed
documents:

[source,console]
--------------------------------------------------
Expand Down Expand Up @@ -34,12 +34,37 @@ PUT /my_locations/_doc/1
}
}
}

PUT /my_geoshapes
{
"mappings": {
"properties": {
"pin": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
}
}

PUT /my_geoshapes/_doc/1
{
"pin": {
"location": {
"type" : "polygon",
"coordinates" : [[[13.0 ,51.5], [15.0, 51.5], [15.0, 54.0], [13.0, 54.0], [13.0 ,51.5]]]
}
}
}
--------------------------------------------------
// TESTSETUP


Then the following simple query can be executed with a `geo_distance`
filter:
filter against a `geo_point` field:

[source,console]
--------------------------------------------------
Expand All @@ -64,6 +89,57 @@ GET /my_locations/_search
}
--------------------------------------------------

The same simple query can be executed against the 'geo_shape field:

[source,console]
--------------------------------------------------
GET my_locations,my_geoshapes/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "200km",
"pin.location": {
"lat": 40,
"lon": -70
}
}
}
}
}
}
--------------------------------------------------

Or it can be executed in both indexes at the same time:

[source,console]
--------------------------------------------------
GET my_geoshapes/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "200km",
"pin.location": {
"lat": 40,
"lon": -70
}
}
}
}
}
}
--------------------------------------------------


[discrete]
==== Accepted Formats

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

[discrete]
==== geo_point Type

The filter *requires* the `geo_point` type to be set on the relevant
field.

[discrete]
==== Multi Location Per Document

Expand Down
Loading