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整合相关的操作