elasticsearch地理位置查询

编辑于 2022-12-17 13:59:13 阅读 725

Elasticsearch支持两种类型的地理数据:支持lat/lon对的geo_point字段和支持点、线、圆圈、多边形、多多边形等的geo_shape字段。

下面只介绍geo_point

创建名称为geo的索引

curl --location --request PUT 'localhost:9200/geo' \
--header 'Content-Type: application/json' \
--data-raw '{
  "settings": {
    "number_of_replicas": 3,
    "number_of_shards": 5
  },
   "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "location":{
        "type": "geo_point"
      }
    }
  }
}'

添加测试数据

curl --location --request PUT 'localhost:9200/geo/_doc/2' \
--header 'Content-Type: application/json' \
--data-raw '{
  "name":"海淀公园",
  "location":
  {
    "lon":116.302509,
    "lat":39.991152
  }
}'


curl --location --request PUT 'localhost:9200/geo/_doc/1' \
--header 'Content-Type: application/json' \
--data-raw '{
  "name":"天安门",
  "location":
  {
    "lon":116.403981,
    "lat":39.914492
  }
}'


curl --location --request PUT 'localhost:9200/geo/_doc/3' \
--header 'Content-Type: application/json' \
--data-raw '{
  "name":"北京动物园",
  "location":
  {
    "lon":116.343184,
    "lat":39.947468
  }
}'

geo_point支持三种类型的查询

  • geo_distance
  • geo_bounding_box
  • geo_polygon

geo_distance:直线距离检索,如给定点A,要求返回地图上距离点A三千米的商家

查找索引内距离北京站(116.433733,39.908404)3000米内的点

涉及的参数如下

  • location:确定一个点;
  • distance:确定一个半径,单位米
  • distance_type:确定一个图形的类型,一般是圆形,arc
curl --location --request GET 'localhost:9200/geo/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "geo_distance": {
      "location": {
        "lon":116.433733
        ,"lat":39.908404
      },
      "distance":3000,
      "distance_type":"arc"
    }
  }
}'

geo_bounding_box:以两个点确定一个矩形,获取在矩形内的全部数据

查找索引内位于中央民族大学(116.326943,39.95499)以及京站(116.433733,39.908404)矩形的点

涉及的参数如下

  • top_left: 左上角的矩形起始点经纬度;
  • bottom_right: 右下角的矩形结束点经纬度
curl --location --request GET 'localhost:9200/geo/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "geo_bounding_box": {
      "location": {
        "top_left": {
          "lon": 116.326943,
          "lat": 39.95499
        },
        "bottom_right": {
          "lon": 116.433446,
          "lat": 39.908737
        }
      }
    }
  }
}'

geo_polygon:以多个点,确定多边形,获取多边形内的全部数据

查找索引内位于西苑桥(116.300209,40.003423),巴沟山水园(116.29561,39.976004)以及北京科技大学(116.364528,39.996348)三角形内的点

涉及的参数如下

  • points:是个数组,存储多变形定点的经纬度,每个点用大括号包起来
curl --location --request GET 'localhost:9200/geo/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": {
    "geo_polygon": {
      "location": {
        "points": [
          {
            "lon": 116.29561,
            "lat": 39.976004
          },
          {
            "lon": 116.364528,
            "lat": 39.996348
          },
          {
            "lon": 116.300209,
            "lat": 40.003423
          }
        ]
      }
    }
  }
}'

地理位置排序

检索结果可以按与指定点的距离排序,当可以按距离排序时, 按距离打分 通常是一个更好的解决方案。但是要计算当前距离,所以还是使用这个排序。搜索示例:

{
  "query": {
    "geo_polygon": {
      "location": {
        "points": [
          {
            "lon": 116.29561,
            "lat": 39.976004
          },
          {
            "lon": 116.364528,
            "lat": 39.996348
          },
          {
            "lon": 116.300209,
            "lat": 40.003423
          }
        ]
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "location": { 
          "lat":  40.715,
          "lon": -73.998
        },
        "order": "asc",
        "unit": "km", 
        "distance_type": "plane" 
      }
    }
  ]
}

解读以下: (注意看sort对象)

  • 计算每个文档中 location 字段与指定的 lat/lon 点间的距离。
  • 将距离以 km 为单位写入到每个返回结果的 sort 键中。
  • 使用快速但精度略差的 plane 计算方式。

结果

{
    "took": 33,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "geo",
                "_type": "_doc",
                "_id": "2",
                "_score": null,
                "_source": {
                    "name": "海淀公园",
                    "location": {
                        "lon": 116.302509,
                        "lat": 39.991152
                    }
                },
                "sort": [
                    16125.943696542714
                ]
            }
        ]
    }
}

参考

https://www.elastic.co/guide/en/elasticsearch/reference/7.17/geo-queries.html

https://blog.csdn.net/wuxintdrh/article/details/115367301

https://www.cnblogs.com/johnvwan/p/15644841.html

广而告之,我的新作品《语音助手》上架Google Play了,欢迎下载体验