Link Search Menu Expand Document Documentation Menu

地理距离查询

地理距离查询返回其地理点与给定地理点在指定距离内的文档。如果文档的多个地理点中至少有一个匹配查询,则该文档匹配查询。

被搜索的文档字段必须映射为 geo_point

示例

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

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

索引一个地理点,指定其纬度和经度

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

搜索其 point 对象与指定 point 之间距离在指定 distance 内的文档

GET /testindex1/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "50mi",
          "point": {
            "lat": 73.5,
            "lon": 40.5
          }
        }
      }
    }
  }
}

响应包含匹配的文档

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

参数

地理距离查询接受以下参数。

参数 数据类型 描述
_name 字符串 过滤器的名称。可选。
distance 字符串 点匹配的距离范围。此距离是以指定点为圆心的圆的半径。有关支持的距离单位,请参阅距离单位。必填。
distance_type 字符串 指定如何计算距离。有效值为 arcplane(后者更快,但对于长距离或靠近极点的点不准确)。可选。默认值为 arc
validation_method 字符串 验证方法。有效值为 IGNORE_MALFORMED(接受坐标无效的地理点)、COERCE(尝试将坐标强制转换为有效值)和 STRICT(当坐标无效时返回错误)。可选。默认值为 STRICT
ignore_unmapped 布尔型 指定是否忽略未映射字段。如果设置为 true,则查询不会返回任何包含未映射字段的文档。如果设置为 false,则当字段未映射时会抛出异常。可选。默认值为 false

支持的格式

索引文档和搜索文档时,可以以地理点字段类型接受的任何格式指定地理点坐标。

剩余 350 字符

有问题?

想要贡献?