地理坐标点需要显式声明对应字段类型为 geo_point
创建索引和映射:
PUT /test_locations_index
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"location": {
"type": "geo_point"
}
}
}
}
添加文档
- 字符串形式
PUT /test_locations_index/_doc/1
{
"name":"test1",
"location":"60.666,70.777"
}
- 对象形式
PUT /test_locations_index/_doc/2
{
"name":"test2",
"location":{
"lat":60.777,
"lon":71.888
}
}
- 数组形式
PUT /test_locations_index/_doc/3
{
"name":"test3",
"location":[60.888,72.999]
}
使用过滤器查询范围内的坐标
geo_bounding_box:指定矩形的边界,找出落在指定矩形框中的点
GET /test_locations_index/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 65.73,
"lon": 69.12
},
"bottom_right": {
"lat": 58.00,
"lon": 74.1
}
}
}
}
}
}
}
geo_distance :找到某个点半径范围内的点
GET /test_locations_index/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "300km",
"location": {
"lat": 60,
"lon": 70
}
}
}
}
}
}