Elasticsearch地理位置查询
2023-02-18 16:33:36 时间
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
}
]
}
}
}
}'
参考
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/geo-queries.html