Link Search Menu Expand Document Documentation Menu

地理边界框查询

要搜索包含地理点(geopoint)字段的文档,请使用地理边界框(geo-bounding box)查询。地理边界框查询返回其地理点位于查询中指定边界框内的文档。如果文档包含多个地理点,只要其中至少一个地理点位于边界框内,该文档就符合查询条件。

示例

您可以使用地理边界框查询来搜索包含地理点的文档。

创建将 point 字段映射为 geo_point 的映射

PUT testindex1
{
  "mappings": {
    "properties": {
      "point": {
        "type": "geo_point"
      }
    }
  }
}

将三个地理点作为包含纬度和经度的对象进行索引

PUT testindex1/_doc/1
{
  "point": { 
    "lat": 74.00,
    "lon": 40.71
  }
}

PUT testindex1/_doc/2
{
  "point": { 
    "lat": 72.64,
    "lon": 22.62
  } 
}

PUT testindex1/_doc/3
{
  "point": { 
    "lat": 75.00,
    "lon": 28.00
  }
}

搜索所有文档并过滤出其点位于查询中定义的矩形内的文档

GET testindex1/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "point": {
            "top_left": {
              "lat": 75,
              "lon": 28
            },
            "bottom_right": {
              "lat": 73,
              "lon": 41
            }
          }
        }
      }
    }
  }
}

响应包含匹配的文档

{
  "took" : 20,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "testindex1",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "point" : {
            "lat" : 74.0,
            "lon" : 40.71
          }
        }
      }
    ]
  }
}

上述响应中不包含地理点为 "lat": 75.00, "lon": 28.00 的文档,因为该地理点的精度有限。

精度

地理点坐标在索引时总是向下取整。在查询时,边界框的上限向下取整,下限向上取整。因此,由于舍入误差,地理点位于边界框下边缘和左边缘的文档可能不会包含在结果中。另一方面,位于边界框上边缘和右边缘的地理点即使在边界之外也可能包含在结果中。纬度的舍入误差小于 4.20 × 10−8 度,经度的舍入误差小于 8.39 × 10−8 度(大约 1 厘米)。

指定边界框

您可以通过提供以下任意顶点坐标组合来指定边界框

  • top_leftbottom_right
  • top_rightbottom_left
  • topleftbottomright

以下示例展示了如何使用 topleftbottomright 坐标指定边界框

GET testindex1/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "point": {
            "top": 75,
            "left": 28,
            "bottom": 73,
            "right": 41            
          }
        }
      }
    }
  }
}

参数

地理边界框查询接受以下参数。

参数 数据类型 描述
_name 字符串 过滤器的名称。可选。
validation_method 字符串 验证方法。有效值包括 IGNORE_MALFORMED(接受具有无效坐标的地理点)、COERCE(尝试将坐标强制转换为有效值)和 STRICT(当坐标无效时返回错误)。默认值为 STRICT
type 字符串 指定如何执行过滤器。有效值包括 indexed(索引过滤器)和 memory(在内存中执行过滤器)。默认值为 memory
ignore_unmapped 布尔型 指定是否忽略未映射的字段。如果设置为 true,查询将不会返回任何具有未映射字段的文档。如果设置为 false,当字段未映射时将抛出异常。默认值为 false

支持的格式

您可以以地理点字段类型接受的任何格式指定边界框顶点的坐标。

使用 Geohash 指定边界框

如果您使用 geohash 指定边界框,geohash 将被视为一个矩形。边界框的左上角顶点对应于 top_left geohash 的左上角顶点,边界框的右下角顶点对应于 bottom_right geohash 的右下角顶点。

以下示例展示了如何使用 geohash 指定与前面示例相同的边界框

GET testindex1/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "point": {
            "top_left": "ut7ftjkfxm34",
            "bottom_right": "uuvpkcprc4rc"
          }
        }
      }
    }
  }
}

要指定覆盖 geohash 整个区域的边界框,请将该 geohash 同时作为边界框的 top_leftbottom_right 参数提供

GET testindex1/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "point": {
            "top_left": "ut",
            "bottom_right": "ut"
          }
        }
      }
    }
  }
}

剩余 350 字符

有问题?

想要贡献?