ES 位置查询之geo_point
创建语句PUT geo{"mappings": {"properties": {"name":{"type": "keyword"},"location":{"type": "geo_point"}}}}其中 location字段的分词器类型为 geo_point, 这样就可以对地理相关的的数据进行操作, geo是地理的geographic单词缩写, 当为这个类型时数据格
·
创建语句
PUT geo
{
"mappings": {
"properties": {
"name":{
"type": "keyword"
},
"location":{
"type": "geo_point"
}
}
}
}
其中 location字段的分词器类型为 geo_point, 这样就可以对地理相关的的数据进行操作, geo是地理的geographic单词缩写, 当为这个类型时数据格式为 "经度, 纬度"
geo_distance query
查询距离一个经纬点指定距离范围内的其它数据
geo_distance query可以查找在一个中心点指定范围内的地理点文档
GET geo/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"geo_distance": {
"distance": "200km",
"location": {
"lat": 39.08,
"lon": 117.19
}
}
}
]
}
}
}
查询距离位置点 lat(精度), lon(纬度) 200km的文档数据, 查询语句中的位置为天津, 最后得到结果为北京
按距离查询经纬点的远近排序
GET geo/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_geo_distance": {
"location": "39.9088145109,116.3973999023",
"unit": "km"
}
}
]
}
geo_bounding_box query 矩形范围查询
geo_bounding_box query用于查找落入指定的矩形内的地理坐标。查询中由两个点确定一个矩形
GET geo/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}}
],
"filter": [
{"geo_bounding_box": {
"location": {
"top_left": {
"lat": 38.48,
"lon": 106.23
},
"bottom_right": {
"lat": 28.68,
"lon": 115.85
}
}
}}
]
}
}
}
查询语句就如图片一样, geo_bounding_box 表示一个方框, 传入左上和右下两个坐标点确定一个矩形, 查出的结果就是 西安和郑州
geo_polygon query 多边形范围内查询
这种是多个点确定一个图形范围, 不仅仅是三角形, 图片是三角形, N边形都可以
GET geo/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"geo_polygon": {
"location": {
"points": [
{
"lat": 40.84,
"lon": 111.75
},
{
"lat": 29.56,
"lon": 106.55
},
{
"lat": 31.23,
"lon": 121.47
}
]
}
}
}
]
}
}
}
查出来的结果为 西安和郑州
更多推荐
已为社区贡献2条内容
所有评论(0)