# Geo Distance Query（地理距离查询）

过滤仅包含距离地理位置特定距离内的命中的文档。 假设以下映射和索引文档：

```
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
        }
    }
}
```

然后可以使用 **geo\_distance** 过滤器执行以下简单查询：

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

## Accepted Formats （接受格式） <a href="#geodistancequery-di-li-ju-li-cha-xun-acceptedformats-jie-shou-ge-shi" id="geodistancequery-di-li-ju-li-cha-xun-acceptedformats-jie-shou-ge-shi"></a>

以同样的方式，**geo\_point**类型可以接受地理点的不同表示，过滤器也可以接受它：

## Lat Lon As Properties （Lat Lon作为属性） <a href="#geodistancequery-di-li-ju-li-cha-xun-latlonaspropertieslatlon-zuo-wei-shu-xing" id="geodistancequery-di-li-ju-li-cha-xun-latlonaspropertieslatlon-zuo-wei-shu-xing"></a>

```
GET /my_locations/location/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "12km",
                    "pin.location" : {
                        "lat" : 40,
                        "lon" : -70
                    }
                }
            }
        }
    }
}
```

## Lat Lon As Array （Lat Lon作为阵列） <a href="#geodistancequery-di-li-ju-li-cha-xun-latlonasarraylatlon-zuo-wei-zhen-lie" id="geodistancequery-di-li-ju-li-cha-xun-latlonasarraylatlon-zuo-wei-zhen-lie"></a>

Format in`[lon, lat]`, note, the order of lon/lat here in order to conform with [GeoJSON](http://geojson.org/).

```
GET /my_locations/location/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "12km",
                    "pin.location" : [-70, 40]
                }
            }
        }
    }
}
```

## Lat Lon As String（Lat Lon作为字符串） <a href="#geodistancequery-di-li-ju-li-cha-xun-latlonasstringlatlon-zuo-wei-zi-fu-chuan" id="geodistancequery-di-li-ju-li-cha-xun-latlonasstringlatlon-zuo-wei-zi-fu-chuan"></a>

Format in `lat,lon`**.**&#x20;

```
GET /my_locations/location/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "12km",
                    "pin.location" : "40,-70"
                }
            }
        }
    }
}
```

## Geohash（地理散列） <a href="#geodistancequery-di-li-ju-li-cha-xun-geohash-di-li-san-lie" id="geodistancequery-di-li-ju-li-cha-xun-geohash-di-li-san-lie"></a>

```
GET /my_locations/location/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "12km",
                    "pin.location" : "drm3btev3e86"
                }
            }
        }
    }
}
```

## Options（选项） <a href="#geodistancequery-di-li-ju-li-cha-xun-options-xuan-xiang" id="geodistancequery-di-li-ju-li-cha-xun-options-xuan-xiang"></a>

以下是过滤器上允许的选项：

| `distance（距离）`             | 以指定位置为中心的圆的半径。 落入此圈子的点被认为是匹配的。 距离可以用各种单位指定。 看 [the section called “Distance Units](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/common-options.html#distance-units)[edit](https://github.com/elastic/elasticsearch/edit/5.4/docs/reference/api-conventions.asciidoc)”. |
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **distance\_type**（距离类型）   | 如何计算距离。 可以是弧（默认）或平面（更快，但长距离不准确，靠近极点）。                                                                                                                                                                                                                                                 |
| **optimize\_bbox**（优化bbox） | 是否在距离检查之前首先运行边框检查使用优化。 默认在内存检查中执行的内存。 也可以使用索引值来使用索引值检查（在这种情况下确保**geo\_pointtype**索引**lat lon**），否则禁用边界框优化。\[2.2]                                                                                                                                                                      |
| **\_name**（名称）             | 可选名称字段来标识查询。                                                                                                                                                                                                                                                                          |
| `ignore_malformed（忽略格式错误）` | \[5.0.0] 设置为**true**以接受无效纬度或经度的地理点（默认值为假）。                                                                                                                                                                                                                                            |
| `validation_method（验证方法）`  | 设置为**IGNORE\_MALFORMED**以接受无效纬度或经度的地理点，设置为**COERCE**以额外尝试推断正确的坐标（默认为**STRICT**）。                                                                                                                                                                                                      |

## geo\_point Type（geo\_point类型） <a href="#geodistancequery-di-li-ju-li-cha-xun-geopointtypegeopoint-lei-xing" id="geodistancequery-di-li-ju-li-cha-xun-geopointtypegeopoint-lei-xing"></a>

过滤器需要在相关字段上设置**geo\_point**类型。

## Multi Location Per Document（每个文档的多个位置） <a href="#geodistancequery-di-li-ju-li-cha-xun-multilocationperdocument-mei-ge-wen-dang-de-duo-ge-wei-zhi" id="geodistancequery-di-li-ju-li-cha-xun-multilocationperdocument-mei-ge-wen-dang-de-duo-ge-wei-zhi"></a>

**geo\_distance**过滤器可以处理每个文档的多个位置/点。 一旦单个位置/点与过滤器匹配，文档将被包含在过滤器中。

## Ignore Unmapped（忽略未映射） <a href="#geodistancequery-di-li-ju-li-cha-xun-ignoreunmapped-hu-lve-wei-ying-she" id="geodistancequery-di-li-ju-li-cha-xun-ignoreunmapped-hu-lve-wei-ying-she"></a>

当设置为**true**时，**ignore\_unmapped** 选项将忽略未映射字段，并且将不会匹配此查询的任何文档。 当查询可能具有不同映射的多个索引时，可能很有用。 当设置为**false**（默认值）时，如果字段未映射，则查询将抛出异常。
