> For the complete documentation index, see [llms.txt](https://xiaoxiami.gitbook.io/elasticsearch/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/35query-dsldslfang-shi-cha-8be229/357di-li-wei-zhi-cha-xun-geo-queries/geo-bounding-box-queryff08-di-li-bian-kuang-cha-xun-ff09.md).

# Geo Bounding Box 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\_bounding\_box**过滤器执行以下简单查询：

```
GET /_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
                        }
                    }
                }
            }
        }
    }
}
```

## Query Options（查询选项） <a href="#geoboundingboxquery-di-li-bian-kuang-cha-xun-queryoptions-cha-xun-xuan-xiang" id="geoboundingboxquery-di-li-bian-kuang-cha-xun-queryoptions-cha-xun-xuan-xiang"></a>

| Option（选项）                | Description（描述）                                                                                                                                                                      |
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `_name（名称）`               | 可选名称字段来标识过滤器。                                                                                                                                                                        |
| `ignore_malformed（忽略异常）`  | \[5.0.0] 设置为**true**以接受无效纬度或经度的地理点（默认值为假）。                                                                                                                                           |
| `validation_method（验证方法）` | 设置为**IGNORE\_MALFORMED**以接受无效纬度或经度的地理点，设置为**COERCE**也尝试推断正确的纬度或经度。 （默认为**STRICT**）。                                                                                                  |
| `type（类型）`                | 设置为索引或内存之一，以定义此过滤器是否将在内存或索引中执行。 有关详细信息，请参阅下面的[**Type**](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-geo-bounding-box-query.html#geo-bbox-type)默认值是 Memory。 |

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

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

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

```
GET /_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
                        }
                    }
                }
            }
        }
    }
}
```

## **Lat Lon As Array （Lat Lon 作为排列）**  <a href="#geoboundingboxquery-di-li-bian-kuang-cha-xun-latlonasarraylatlon-zuo-wei-pai-lie" id="geoboundingboxquery-di-li-bian-kuang-cha-xun-latlonasarraylatlon-zuo-wei-pai-lie"></a>

格式&#x5728;**\[lon，lat]**&#x4E2D;，注意，这里的**lon / lat**的顺序是为了符合[**GeoJSON**](http://geojson.org/)**。**

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

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

格式在**lat，lon**。

```
GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "pin.location" : {
                        "top_left" : "40.73, -74.1",
                        "bottom_right" : "40.01, -71.12"
                    }
                }
            }
    }
}
```

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

```
GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "pin.location" : {
                        "top_left" : "dr5r9ydj2y73",
                        "bottom_right" : "drj7teegpus6"
                    }
                }
            }
        }
    }
}
```

## **Vertices（顶点）** <a href="#geoboundingboxquery-di-li-bian-kuang-cha-xun-vertices-ding-dian" id="geoboundingboxquery-di-li-bian-kuang-cha-xun-vertices-ding-dian"></a>

边框的顶点可以由**top\_left** 和**bottom\_right**或**top\_rightand bottom\_left**参数设置。 更多的名称**topLeft，bottomRight，topRight和bottomLeft**是支持的。 而不是成对设置值，可以使用简单的名称顶部，左侧，底部和右侧单独设置值。

```
GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "pin.location" : {
                        "top" : 40.73,
                        "left" : -74.1,
                        "bottom" : 40.01,
                        "right" : -71.12
                    }
                }
            }
        }
    }
}
```

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

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

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

过滤器可以与每个文档的多个位置/点配合使用。 一旦单个位置/点与过滤器匹配，文档将被包含在过滤器中。

## **Type（类型）** <a href="#geoboundingboxquery-di-li-bian-kuang-cha-xun-type-lei-xing" id="geoboundingboxquery-di-li-bian-kuang-cha-xun-type-lei-xing"></a>

默认情况下，边框执行的类型设置为内存，这意味着在内存中检查文档是否在边框范围内。 在某些情况下，索引选项的执行速度会更快（但是请注意，在这种情况下，**geo\_point**类型必须具有**lat**和**lon**索引）。 请注意，使用索引选项时，不支持每个文档字段的多个位置。 这是一个例子：

```
GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "pin.location" : {
                        "top_left" : {
                            "lat" : 40.73,
                            "lon" : -74.1
                        },
                        "bottom_right" : {
                            "lat" : 40.10,
                            "lon" : -71.12
                        }
                    },
                    "type" : "indexed"
                }
            }
        }
    }
}
```

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

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