Geo-bounding box queries
You can use a geo-bounding box query to search for documents that contain geopoints.
Create a mapping with the field mapped as geo_point
:
Index three geopoints as objects with latitudes and longitudes:
PUT testindex1/_doc/1
{
"point": {
"lat": 74.00,
"lon": 40.71
}
}
PUT testindex1/_doc/2
{
"point": {
"lat": 72.64,
"lon": 22.62
}
}
PUT testindex1/_doc/3
{
"point": {
"lat": 75.00,
}
}
Search for all documents and filter the documents whose points lie within the rectangle defined in the query:
{
"took" : 20,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex1",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"point" : {
"lat" : 74.0,
"lon" : 40.71
}
}
]
}
}
The preceding response does not include the document with a geopoint of "lat": 75.00, "lon": 28.00
because of the geopoint’s limited .
Geopoint coordinates are always rounded down at index time. At query time, the upper boundaries of the bounding box are rounded down, and the lower boundaries are rounded up. Therefore, the documents with geopoints that lie on the lower and left edges of the bounding box might not be included in the results due to rounding error. On the other hand, geopoints that lie on the upper and right edges of the bounding box might be included in the results even though they are outside the boundaries. The rounding error is less than 4.20 × 10−8 degrees for latitude and less than 8.39 × 10−8 degrees for longitude (around 1 cm).
You can specify the bounding box by providing any of the following combinations of its vertex coordinates:
top_left
andbottom_right
top_right
andbottom_left
top
,left
,bottom
, andright
The following example shows how to specify the bounding box using the top
, left
, , and right
coordinates:
You can specify coordinates of the bounding box vertices in any format that the geopoint field type accepts.
If you use a geohash to specify the bounding box, the geohash is treated as a rectangle. The upper-left vertex of the bounding box corresponds to the upper-left vertex of the top_left
geohash, and the lower-right vertex of the bounding box corresponds to the lower-right vertex of the bottom_right
geohash.
The following example shows how to use a geohash to specify the same bounding box as the previous examples:
GET testindex1/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_bounding_box": {
"point": {
"top_left": "ut7ftjkfxm34",
"bottom_right": "uuvpkcprc4rc"
}
}
}
}
}
}
To specify a bounding box that covers the whole area of a geohash, provide that geohash as both top_left
and parameters of the bounding box: