文章目錄
ES
支持基於地理位置的搜索和聚合分析
經緯度:
lat
: latitude 維度
lon
: longitude 經度
一、建立 geo_point
mapping
PUT /hotel_index
{
"mappings": {
"doc": {
"dynamic": "false",
"properties": {
"name": { "type": "text", "fields": { "keyword": {"ignore_above": 256, "type": "keyword"} } },
"location": {
"type": "geo_point"
}
}
}
}
}
二、寫入 geo_point
三種方式
推薦第一種寫法,比較清晰。
(1)對象形式
PUT /hotel_index/doc/1
{
"name": "新街口",
"location": {
"lat": 39.967157,
"lon": 116.322631
}
}
(2)字符串形式
經緯度:
lat
: latitude 維度
lon
: longitude 經度
表達式:lat,lon
若 lon,lat,則會報錯
PUT /hotel_index/doc/2
{
"name": "北京交通大學",
"location": "39.957866,116.349652"
}
(3)數組形式
經緯度:
lat
: latitude 維度
lon
: longitude 經度
這一種方式是經度在前,維度在後。
即 [lon, lat]
若改爲 [lat,lon], 則會報錯
PUT /hotel_index/doc/3
{
"name": "北京理工大學",
"location": [39.967157,116.322631]
}
三、查詢
(1)geo_bouding_box
查詢
geo_bouding_box
查詢某個矩形的地理位置範圍內的座標點
左上角座標和右下角座標,繪製出一個矩形。
GET /hotel_index/doc/_search
{
"query": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 42,
"lon": -72
},
"bottom_right": {
"lat": 40,
"lon": -74
}
}
}
}
}
(2)geo_distance
當前位置一定範圍內
geo_distance
是距離搜索,以一個點周圍擴散的距離範圍
GET /hotel_index/doc/_search
{
"query": {
"bool": {
"filter": {
"geo_distance": {
"distance": "2500m",
"location": {
"lat": 39.957866,
"lon": 116.349652
}
}
}
}
}
}