123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- [[query-dsl-geo-bounding-box-query]]
- === Geo-bounding box query
- ++++
- <titleabbrev>Geo-bounding box</titleabbrev>
- ++++
- Matches <<geo-point,`geo_point`>> and <<geo-shape,`geo_shape`>> values that
- intersect a bounding box.
- [discrete]
- [[geo-bounding-box-query-ex]]
- ==== Example
- Assume the following the following documents are indexed:
- [source,console]
- --------------------------------------------------
- PUT /my_locations
- {
- "mappings": {
- "properties": {
- "pin": {
- "properties": {
- "location": {
- "type": "geo_point"
- }
- }
- }
- }
- }
- }
- PUT /my_locations/_doc/1
- {
- "pin": {
- "location": {
- "lat": 40.12,
- "lon": -71.34
- }
- }
- }
- 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
- Use a `geo_bounding_box` filter to match `geo_point` values that intersect a bounding
- box. To define the box, provide geopoint values for two opposite corners.
- [source,console]
- --------------------------------------------------
- GET my_locations/_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
- }
- }
- }
- }
- }
- }
- }
- --------------------------------------------------
- Use the same filter to match `geo_shape` values that intersect the bounding box:
- [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
- }
- }
- }
- }
- }
- }
- }
- --------------------------------------------------
- To match both `geo_point` and `geo_shape` values, search both indices:
- [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
- }
- }
- }
- }
- }
- }
- }
- --------------------------------------------------
- [discrete]
- ==== Query Options
- [cols="<,<",options="header",]
- |=======================================================================
- |Option |Description
- |`_name` |Optional name field to identify the filter
- |`validation_method` |Set to `IGNORE_MALFORMED` to
- accept geo points with invalid latitude or longitude, set to
- `COERCE` to also try to infer correct latitude or longitude. (default is `STRICT`).
- |=======================================================================
- [[query-dsl-geo-bounding-box-query-accepted-formats]]
- [discrete]
- ==== Accepted Formats
- In much the same way the `geo_point` type can accept different
- representations of the geo point, the filter can accept it as well:
- [discrete]
- ===== Lat Lon As Properties
- [source,console]
- --------------------------------------------------
- GET my_locations/_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]
- ===== Lat Lon As Array
- Format in `[lon, lat]`, note, the order of lon/lat here in order to
- conform with http://geojson.org/[GeoJSON].
- [source,console]
- --------------------------------------------------
- GET my_locations/_search
- {
- "query": {
- "bool": {
- "must": {
- "match_all": {}
- },
- "filter": {
- "geo_bounding_box": {
- "pin.location": {
- "top_left": [ -74.1, 40.73 ],
- "bottom_right": [ -71.12, 40.01 ]
- }
- }
- }
- }
- }
- }
- --------------------------------------------------
- [discrete]
- ===== Lat Lon As String
- Format in `lat,lon`.
- [source,console]
- --------------------------------------------------
- GET my_locations/_search
- {
- "query": {
- "bool": {
- "must": {
- "match_all": {}
- },
- "filter": {
- "geo_bounding_box": {
- "pin.location": {
- "top_left": "POINT (-74.1 40.73)",
- "bottom_right": "POINT (-71.12 40.01)"
- }
- }
- }
- }
- }
- }
- --------------------------------------------------
- [discrete]
- ===== Bounding Box as Well-Known Text (WKT)
- [source,console]
- --------------------------------------------------
- GET my_locations/_search
- {
- "query": {
- "bool": {
- "must": {
- "match_all": {}
- },
- "filter": {
- "geo_bounding_box": {
- "pin.location": {
- "wkt": "BBOX (-74.1, -71.12, 40.73, 40.01)"
- }
- }
- }
- }
- }
- }
- --------------------------------------------------
- [discrete]
- ===== Geohash
- [source,console]
- --------------------------------------------------
- GET my_locations/_search
- {
- "query": {
- "bool": {
- "must": {
- "match_all": {}
- },
- "filter": {
- "geo_bounding_box": {
- "pin.location": {
- "top_left": "dr5r9ydj2y73",
- "bottom_right": "drj7teegpus6"
- }
- }
- }
- }
- }
- }
- --------------------------------------------------
- When geohashes are used to specify the bounding the edges of the
- bounding box, the geohashes are treated as rectangles. The bounding
- box is defined in such a way that its top left corresponds to the top
- left corner of the geohash specified in the `top_left` parameter and
- its bottom right is defined as the bottom right of the geohash
- specified in the `bottom_right` parameter.
- In order to specify a bounding box that would match entire area of a
- geohash the geohash can be specified in both `top_left` and
- `bottom_right` parameters:
- [source,console]
- --------------------------------------------------
- GET my_locations/_search
- {
- "query": {
- "geo_bounding_box": {
- "pin.location": {
- "top_left": "dr",
- "bottom_right": "dr"
- }
- }
- }
- }
- --------------------------------------------------
- In this example, the geohash `dr` will produce the bounding box
- query with the top left corner at `45.0,-78.75` and the bottom right
- corner at `39.375,-67.5`.
- [discrete]
- ==== Vertices
- The vertices of the bounding box can either be set by `top_left` and
- `bottom_right` or by `top_right` and `bottom_left` parameters. More
- over the names `topLeft`, `bottomRight`, `topRight` and `bottomLeft`
- are supported. Instead of setting the values pairwise, one can use
- the simple names `top`, `left`, `bottom` and `right` to set the
- values separately.
- [source,console]
- --------------------------------------------------
- GET my_locations/_search
- {
- "query": {
- "bool": {
- "must": {
- "match_all": {}
- },
- "filter": {
- "geo_bounding_box": {
- "pin.location": {
- "top": 40.73,
- "left": -74.1,
- "bottom": 40.01,
- "right": -71.12
- }
- }
- }
- }
- }
- }
- --------------------------------------------------
- [discrete]
- ==== Multi Location Per Document
- The filter can work with multiple locations / points per document. Once
- a single location / point matches the filter, the document will be
- included in the filter
- [discrete]
- ==== Ignore Unmapped
- When set to `true` the `ignore_unmapped` option will ignore an unmapped field
- and will not match any documents for this query. This can be useful when
- querying multiple indexes which might have different mappings. When set to
- `false` (the default value) the query will throw an exception if the field
- is not mapped.
- [discrete]
- ==== Notes on Precision
- Geopoints have limited precision and are always rounded down during index time.
- During the query time, upper boundaries of the bounding boxes are rounded down,
- while lower boundaries are rounded up. As a result, the points along on 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 points alongside
- 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.
- Geoshapes also have limited precision due to rounding. Geoshape edges along the
- bounding box's bottom and left edges may not match a `geo_bounding_box` query.
- Geoshape edges slightly outside the box's top and right edges may still match
- the query.
|