| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 | [[query-dsl-geo-distance-query]]=== Geo Distance QueryFilters documents that include only hits that exists within a specificdistance from a geo point. Assuming the following mapping and indexeddocument:[source,js]--------------------------------------------------PUT /my_locations{    "mappings": {        "location": {            "properties": {                "pin": {                    "properties": {                        "location": {                            "type": "geo_point"                        }                    }                }            }        }    }}PUT /my_locations/location/1{    "pin" : {        "location" : {            "lat" : 40.12,            "lon" : -71.34        }    }}--------------------------------------------------// CONSOLE// TESTSETUPThen the following simple query can be executed with a `geo_distance`filter:[source,js]--------------------------------------------------GET /my_locations/location/_search{    "query": {        "bool" : {            "must" : {                "match_all" : {}            },            "filter" : {                "geo_distance" : {                    "distance" : "200km",                    "pin.location" : {                        "lat" : 40,                        "lon" : -70                    }                }            }        }    }}--------------------------------------------------// CONSOLE[float]==== Accepted FormatsIn much the same way the `geo_point` type can accept differentrepresentations of the geo point, the filter can accept it as well:[float]===== Lat Lon As Properties[source,js]--------------------------------------------------GET /my_locations/location/_search{    "query": {        "bool" : {            "must" : {                "match_all" : {}            },            "filter" : {                "geo_distance" : {                    "distance" : "12km",                    "pin.location" : {                        "lat" : 40,                        "lon" : -70                    }                }            }        }    }}--------------------------------------------------// CONSOLE[float]===== Lat Lon As ArrayFormat in `[lon, lat]`, note, the order of lon/lat here in order toconform with http://geojson.org/[GeoJSON].[source,js]--------------------------------------------------GET /my_locations/location/_search{    "query": {        "bool" : {            "must" : {                "match_all" : {}            },            "filter" : {                "geo_distance" : {                    "distance" : "12km",                    "pin.location" : [-70, 40]                }            }        }    }}--------------------------------------------------// CONSOLE[float]===== Lat Lon As StringFormat in `lat,lon`.[source,js]--------------------------------------------------GET /my_locations/location/_search{    "query": {        "bool" : {            "must" : {                "match_all" : {}            },            "filter" : {                "geo_distance" : {                    "distance" : "12km",                    "pin.location" : "40,-70"                }            }        }    }}--------------------------------------------------// CONSOLE[float]===== Geohash[source,js]--------------------------------------------------GET /my_locations/location/_search{    "query": {        "bool" : {            "must" : {                "match_all" : {}            },            "filter" : {                "geo_distance" : {                    "distance" : "12km",                    "pin.location" : "drm3btev3e86"                }            }        }    }}--------------------------------------------------// CONSOLE[float]==== OptionsThe following are options allowed on the filter:[horizontal]`distance`::    The radius of the circle centred on the specified location. Points which    fall into this circle are considered to be matches. The `distance` can be    specified in various units. See <<distance-units>>.`distance_type`::    How to compute the distance. Can either be `sloppy_arc` (default), `arc` (slightly more precise but significantly slower) or `plane` (faster, but inaccurate on long distances and close to the poles).`optimize_bbox`::    Whether to use the optimization of first running a bounding box check    before the distance check. Defaults to `memory` which will do in memory    checks. Can also have values of `indexed` to use indexed value check (make    sure the `geo_point` type index lat lon in this case), or `none` which    disables bounding box optimization. deprecated[2.2]`_name`::    Optional name field to identify the query`ignore_malformed`::       deprecated[5.0.0,Use `validation_method` instead] Set to `true` to accept geo points with invalid latitude or    longitude (default is `false`).`validation_method`::    Set to `IGNORE_MALFORMED` to accept geo points with invalid latitude or    longitude, set to `COERCE` to additionally try and infer correct    coordinates (default is `STRICT`).[float]==== geo_point TypeThe filter *requires* the `geo_point` type to be set on the relevantfield.[float]==== Multi Location Per DocumentThe `geo_distance` filter can work with multiple locations / points perdocument. Once a single location / point matches the filter, thedocument will be included in the filter.[float]==== Ignore UnmappedWhen set to `true` the `ignore_unmapped` option will ignore an unmapped fieldand will not match any documents for this query. This can be useful whenquerying multiple indexes which might have different mappings. When set to`false` (the default value) the query will throw an exception if the fieldis not mapped.
 |