elasticsearch 地理查询的使用(一)

elasticsearch 地理查询的使用(一)

  • geo_point(地理位置)

     注意:地理位置的mapping要先声明出来,系统是无法识别这一个类型为地理位置或者是地理形状的类型
    

    主要使用于地图上的一个点的mapping映射

    在kibana上的使用如下
	创建索引为cn_large_cities的索引及其映射规则
	PUT cn_large_cities
	{
	  "mappings": {
	        "city": {
	            "properties": {
	                "city": {"type": "string"},
	                "state": {"type": "string"},
	                "location":{"type": "geo_point"}
	            }
	        }
	    }
	}
	可以通过这样的方式查询当前的映射是否生效
	GET cn_large_cities/_mapping
--------------------------------添加测试数据-------------------------------------
POST cn_large_cities/city
{
  "city": "Beijing", 
  "state": "BJ",
  "location":{"lat": "39.91667", "lon": "11.41667"}
}

POST cn_large_cities/city
{
  "city": "Shanghai", 
  "state": "SH",
  "location": {"lat":"34.50000","lon": "121.43333"}
}

POST cn_large_cities/city
{
  "city": "Xiamen", 
  "state": "FJ",
  "location":{"lat": "24.46667","lon": "118.10000"}
}

POST cn_large_cities/city
{
  "city": "Fuzhou",
  "state": "FJ",
  "location":{"lat": "26.08333","lon": "119.30000"}
}

POST cn_large_cities/city
{
  "city":"Guangzhou",
  "state":"GD",
  "location":{"lat": "23.16667","lon": "113.23333"}
}
--------------------------------添加测试数据-------------------------------------
GET cn_large_cities/city/_search(查询所有的数据)

GET cn_large_cities/city/_search(使用geo_distance距离查询离厦门1km之内的城市)
{
  "query": {
      "bool": {
        "must": {
            "match_all": {}
          },
        "filter": {
          "geo_distance":{
            "distance": "1km",
            "location":{
            "lat":24.46667,
            "lon": 118.10000
            }
          }
      }
    }
    
  }
}

GET cn_large_cities/city/_search(使用距离查询器查询距离厦门500km的城市)
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": {
        "geo_distance": {
          "distance": "500km",
          "location": {
            "lat": 24.46667,
            "lon": 118.10000
          }
        }
      }
    }
  }
}

GET cn_large_cities/city/_search(使用距离查询器查询距离厦门800km的城市)
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": {
        "geo_distance": {
          "distance": "800km",
          "location": {
            "lat": 24.46667,
            "lon": 118.10000
          }
        }
      }
    }
  }
}

GET cn_large_cities/city/_search(使用sort中的_geo_distance排序器进行排序的操作按照距离由远到近的方式)
{
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": 24.46667,
          "lon": 118.10000
        },
        "order": "asc"
      }
    }
  ]
}
{
  "query": {
    "bool": {
      "must": [
        {"match_all": {}}
      ]
    }
  }
}

GET cn_large_cities/city/_search(使用geo_distance查询离厦门900km的城市并且使用geo_distance聚合出离厦门400km到900km的环形下的城市个数)注意:5.0以上版本无法使用geo_distance_ange这个筛选器
{
  "query": {
    "bool": {"must": [
      {"match_all": {}}
    ],
    "filter": {
      "geo_distance": {
        "distance": "900km",
        "location": {
          "lat": 24.46667,
          "lon": 118.10000
        }
      }
    }
    }
  },
  "aggs": {
    "distanceAgg": {
      "geo_distance": {
        "field": "location",
        "unit": "km", 
        "origin": {
          "lat": 24.46667,
          "lon": 118.10000
        },
        "ranges": [
          {
            "from": 400,
            "to": 900
          }
        ]
      }
    }
  }
}
  • 地理形状的使用geo_shape(地理形状)
PUT cn_large_cities_shape(创建出一个映射,其中包含类型为geo_shape)
{
  "mappings": {
        "city": {
            "properties": {
                "city": {"type": "string"},
                "state": {"type": "string"},
                "location":{"type": "geo_shape"}
            }
        }
    }
}

POST cn_large_cities_shape/city(索引一条以厦门为中心半径为500km的圆)
{
  "city":"XiaMenYuan",
  "state":"XM",
  "location":{
    "type":"circle",
    "coordinates":[118.10000,24.46667],
    "radius":"500km"
  }
}

POST cn_large_cities_shape/city(索引一条以福州为中心半径为600km的圆)
{
  "city":"FuZhouYuan",
  "state":"FZ",
  "location":{
    "type":"circle",
    "coordinates":[119.30000,26.08333],
    "radius":"600km"
  }
}

GET cn_large_cities_shape/city/_search(以经度为119.30000,纬度为26.08333半径为900km创建一个圆查询出包含在这个圆里面的数据)
{
  "query": {
    "bool": {"must": [
      {"match_all": {}}
    ],
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "circle",
              "coordinates":[119.30000,26.08333],
              "radius":"900km"
          },
           "relation": "within"
        }
      }
    }
  }
}
}

更多的geo_shapQuery查询可以看这个官方文档的介绍

地理形状(geo_shape)可创建的地理形状一共有(点,线,矩形,圆,多边形:多边形必须闭合及起点要为多边形的终点其他的 线,矩形,圆,多变形参照官方文档

后续是与springboot整合相关的操作

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麦片王子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值