💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# Geo Bounding Box Query(地理边框查询) 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-geo-bounding-box-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-geo-bounding-box-query.html) 译文链接 : [Geo Bounding Box Query(地理边框查询)](/pages/viewpage.action?pageId=5505153) 贡献者 : [@yangbin](http://www.apache.wiki/display/~yangbin),[ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina) 允许使用边框对基于点位置的点击进行过滤的查询。 假设以下索引文档:  ``` 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(查询选项)  | 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 "Typeedit")** 默认值是 Memory。 | ### **Accepted Formats(接**受格式) 以同样的方式,**geo_point** 类型可以接受地理点的不同表示,过滤器也可以接受它: ### **Lat Lon As Properties(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.01, "lon" : -71.12 } } } } } } } ``` ### **Lat Lon As Array (Lat Lon 作为排列) ** 格式在 **[lon,lat]** 中,注意,这里的 **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 作为字符串)  格式在 **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(地理散列)  ``` GET /_search { "query": { "bool" : { "must" : { "match_all" : {} }, "filter" : { "geo_bounding_box" : { "pin.location" : { "top_left" : "dr5r9ydj2y73", "bottom_right" : "drj7teegpus6" } } } } } } ``` ### **Vertices(顶点)** 边框的顶点可以由 **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类型)** 过滤器需要在相关字段上设置 **geo_point** 类型。 ### Multi Location Per Document(每个文档的多个位置) 过滤器可以与每个文档的多个位置/点配合使用。 一旦单个位置/点与过滤器匹配,文档将被包含在过滤器中。 ### **Type(类型)** 默认情况下,边框执行的类型设置为内存,这意味着在内存中检查文档是否在边框范围内。 在某些情况下,索引选项的执行速度会更快(但是请注意,在这种情况下,**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(忽略未映射) 当设置为 **true** 时,**ignore_unmapped** 选项将忽略未映射字段,并且将不匹配此查询的任何文档。 当查询可能具有不同映射的多个索引时,这可能很有用。 当设置为 **false**(默认值)时,如果字段未映射,则查询将抛出异常。