ElasticSearch 地理定位搜索----Geo-shape / geo_shape 的使用方法

地理位置拾取http://api.map.baidu.com/lbsapi/getpoint/index.html

geo_shape Mapping

orientation

(可选)定义如何解释多边形顶点顺序。

要设置RIGHT:

  • right
  • counterclockwise
  • ccw

要设置LEFT:

  • left
  • clockwise
  • cw

strategy

  • term:策略只支持点类型(points_only参数将自动设置为true),
  • recursive:策略支持所有形状类型。(圆形必须设置)

recursive

用于查询时与定位标识的策略

  • INTERSECTS 相交
  • DISJOINT 不相交
  • WITHIN 内
  • CONTAINS 包含

type (形状)

GeoJSON TypeWKT TypeElasticsearch TypeDescription
PointPOINTpoint单一的地理坐标
LineStringLINESTRINGlinestring给定两个或多个点的任意直线。
PolygonPOLYGONpolygon一个封闭的多边形,其第一个点和最后一个点必须匹配,最少4个顶点。
MultiPointMULTIPOINTmultipoint一组不相连但可能相关的点。
MultiLineStringMULTILINESTRINGmultilinestring由独立行字符串组成的数组。
MultiPolygonMULTIPOLYGONmultipolygon一组独立的多边形。
GeometryCollectionGEOMETRYCOLLECTIONgeometrycollection一个与multi*形状相似的GeoJSON形状,除了多个类型可以共存(例如,一个Point和一个LineString)。
N/ABBOXenvelope通过仅指定左上角和右下角点来指定的边界矩形(或包络线)。
N/AN/Acircle以圆心和半径为单位指定的圆,默认为米。

geo_shape Query

envelope 矩形搜索

"geo_shape": {
  "location": {
    "shape": {
      "type": "envelope",
      "coordinates": [ [ 左上角经,纬], [  右下角经,纬 ] ]
    },
    "relation": "范围策略"
  }
}

relation:
INTERSECTS -(默认)查询几何图形相交的文档。
DISJOINT -查询几何体没有任何共同之处的文档。
WITHIN -查询几何中的文档。不支持直线几何图形。
CONTAINS -查询几何图形的所有文档。

circle 圆形

 "geo_shape": {
    "location": {
      "shape": {
        "type": "circle",
        "coordinates":  [116.40026,39.911401] ,
        "radius":"半径 m km"
      },
      "relation": "intersects"
    }
  }
}

自定义图形

案例

http://api.map.baidu.com/lbsapi/getpoint/index.html
可以查地图点的位置 》》》也可以反查》》》》

Point 点

查询
在这里插入图片描述

插入了2中方式,取出的数据格式也是不同的

POST /example/_doc
{
  "name": "天安门",
  "class":"景点",
  "location": {
    "type": "point",
    "coordinates": [ 116.404073,39.915185 ]
  }
}
POST /example/_doc
{
  "name": "英雄纪念碑",
  "class":"景点",
  "location" : "POINT (116.404037 39.910868)"

}

GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
			  "coordinates": [ [116.401646,39.915661], [116.405796,39.912119] ]
            },
            "relation": "WITHIN"
          }
        }
      }
    }
  }
}

扩大范围>>>>>>>

[ [116.401646,39.915661], [116.406586,39.908993] ]

在这里插入图片描述

linestring 线

蓝色是数据线》》》红色是搜索范围

在这里插入图片描述

POST /example/_doc
{
  "name": "天安门西到天安门东地铁线",
  "class":"景点",
  "location": {
    "type": "linestring",
    "coordinates": [[116.398009,39.913798],[116.407855,39.914075]]
  }
}

POST /example/_doc
{
  "name": "天安门西到天安门东地铁线",
  "class":"景点",
  "location" : "LINESTRING (116.398009 39.913798, 116.407855 39.914075)"
}

intersects 可查 WITHIN 无

GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [116.40392,39.915237], [116.410567,39.91283] ]
            },
            "relation": "intersects" //相交
          }
        }
      }
    }
  }
}

在这里插入图片描述

Polygon

多边形

首尾必须相同,也就是闭合的,顺时针的点(左上,右上,右下,左下)
**加粗样式**
注意哦 这恶心到人的嵌套

POLYGON ((经 空格 纬,经 纬,经 纬))

POST /example/_doc
{
  "name": "国家博物馆",
  "class":"景点",
  "location": {
    "type": "polygon",
    "coordinates":
                  [
                    [
                      [116.406238,39.9098],
                      [116.409328,39.913618],
                      [116.409471,39.909924],
                      [116.409471,39.909924],
                      [116.406238,39.9098]
                    ]
                  ]  
  }
}

使用的和point一样的地址

GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [116.40392,39.915237], [116.410567,39.91283] ]
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

在这里插入图片描述

带孔多边形

蓝色》天安门广场(挖空)
红色》纪念堂
黄色》搜索范围
在这里插入图片描述

在未挖孔的情况下 我们对纪念堂进行矩形搜索是两个都可以搜到

POST /example/_doc
{
  "name": "天安门广场",
  "class":"景点",
  "location": {
    "type": "polygon",
    "orientation" : "clockwise",
    "coordinates":
                  [
                    [
                      [116.402195,39.913615],
                      [116.405519,39.913753],
                      [116.405825,39.906935],
                      [116.402591,39.906991],
                      [116.402195,39.913615]
                    ]
                  ]  
  }
}

POST /example/_doc
{
  "name": "xxx纪念堂",
  "class":"景点",
  "location": {
    "type": "polygon",
    "orientation" : "clockwise",
    "coordinates":
                  [
                    [
                      [116.403467,39.909364],
                      [116.404967,39.908284],
                      [116.404985,39.90825],
                      [116.403503,39.908236],
                      [116.403467,39.909364]
                    ]
                  ]  
  }
}
GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [116.403467,39.909461], [116.404976,39.908181] ]
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}
{

    "hits" : [
      {
        "_index" : "example",
        "_type" : "_doc",
        "_id" : "aS_7wnoBHlOFqM1d80as",
        "_score" : 1.0,
        "_source" : {
          "name" : "天安门广场",
          "class" : "景点",
          "location" : {
            "type" : "polygon",
            "coordinates" : [
           。。。。。。。。。。。。。。。。。
          }
        }
      },
      {
        "_index" : "example",
        "_type" : "_doc",
        "_id" : "ai_7wnoBHlOFqM1d_kYd",
        "_score" : 1.0,
        "_source" : {
          "name" : "xxx纪念堂",
          "class" : "景点",
          "location" : {
            "type" : "polygon",
            "coordinates" : [
       。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
      }
    ]
  }
}

挖孔

POST /example/_doc
{
  "name": "天安门广场",
  "class":"景点",
  "location": {
    "type": "polygon",
    "orientation" : "clockwise",
    "coordinates":
                  [
                    [
                      [116.402195,39.913615],
                      [116.405519,39.913753],
                      [116.405825,39.906935],
                      [116.402591,39.906991],
                      [116.402195,39.913615]
                    ],
                    [
                      [116.403467,39.909364],
                      [116.404994,39.909467],
                      [116.404967,39.908284],
                      [116.403503,39.908236],
                      [116.403467,39.909364]
                    ]
                  ]  
  }
}

POST /example/_doc
{
  "name": "xxx纪念堂",
  "class":"景点",
  "location": {
    "type": "polygon",
    "coordinates":
                  [
                    [
                      [116.403467,39.909364],
                      [116.404967,39.908284],
                      [116.404985,39.90825],
                      [116.403503,39.908236],
                      [116.403467,39.909364]
                    ]
                  ]  
  }
}




GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [116.403709,39.909163], [116.404608,39.908527] ]
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

在这里插入图片描述

MultiPoint 多点

蓝色>>定位
红色>>查询区
在这里插入图片描述

POST /example/_doc
{
  "name": "天安门西地铁站出口",
  "class":"景点",
  "location": {
    "type": "multipoint",
    "coordinates":[[116.397141,39.91407], [116.398686,39.914067], 
    [116.39715,39.913368]]
  }
}
GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [[116.398264,39.91424], [116.399261,39.913164]]
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

在这里插入图片描述

MultiLineString 多线

蓝色>>地铁6号线
红色>>搜索区域

在这里插入图片描述

数组1上半段 数据2下半段

POST /example/_doc
{
  "name": "地铁8号线",
  "class":"景点",
  "location": {
    "type": "multilinestring",
    "coordinates":[
    [[116.400802,39.976916], [116.402814,39.939972],[116.414312,39.939087],[116.417762,39.930235]],
    [[116.405114,39.896643], [116.405904,39.880754]]
    ]
  }
}


GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [[116.396921,39.947689], [116.439177,39.928658]]
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

MultiPolygon 多图形

没写明白

Geometry Collection 图形集合

》我们把大会堂和大会堂宾馆 都算作人民大会堂范围
在这里插入图片描述

POST /example/_doc
{
  "name": "人民大会堂",
  "class":"景点",
  "location": {
    "type": "geometrycollection",
    "geometries":
                  [
                    {
                      "type":"polygon",
                      "coordinates":[[[116.398212,39.913383],[116.401859,39.913383],
                      [116.402182,39.909343],[116.39841,39.90926],[116.398212,39.913383]]]
                    },{
                      "type":"point",
                      "coordinates":[116.400206,39.90854]
                    }
                  ]  
  }
}
//宾馆
GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [116.39841,39.90926], [116.400691,39.908132] ]
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

//大会堂
GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [116.397844,39.914407], [116.402263,39.911972] ]
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

Envelope 矩形

在这里插入图片描述

POST /example/_doc
{
  "name": "故宫",
  "class":"景点",
  "location": {
    "type": "envelope",
    "coordinates" : [ [116.398634,39.928739], [116.407904,39.920162] ]
  }
}




GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [116.404096,39.924395], [116.410492,39.918419] ]
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

circle 圆 (不推荐)

circle类型需要使用已弃用的 recursive 前缀树策略进行geo_shape字段映射。
测距范围100米,以音乐厅为中心
在这里插入图片描述
mapping中一定要有 “strategy”: “recursive”

{
  "mappings": {
    "properties": {
      "字段名称": {
        "type": "geo_shape",
        "strategy": "recursive"
      }
    }
  }
}

POST /example/_doc
{
  "name": "国家大剧院",
  "class":"景点",
  "location": {
    "type": "circle",
    "coordinates" : [116.396146,39.911045],
    "radius":"50m"
  }
}

在这里插入图片描述
》》》》》》》实际290m的范围

GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "circle",
              "coordinates":  [116.40026,39.911401] ,
              "radius":"290m"
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

在这里插入图片描述

Pre-Indexed Shape 索引图形

就是把PUT进去的图形作为搜索图形,,,
蓝色》》设置商场点
绿色》》矩形作为范围索引使用
在这里插入图片描述

添加3条商场信息

POST /example/_doc
{
  "name": "君太商场",
  "class":"景点",
  "location": {
    "type": "point",
    "coordinates" : [116.380893,39.9154835]
  }
}
POST /example/_doc
{
  "name": "汉光百货",
  "class":"景点",
  "location": {
    "type": "point",
    "coordinates" : [116.37994,39.9156]
  }
}
POST /example/_doc
{
  "name": "大悦城",
  "class":"景点",
  "location": {
    "type": "point",
    "coordinates" : [116.379859,39.916894]
  }
}

创建缓存形状的索引和添加索引数据 必须设置索引ID

PUT cache_shapes
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}
POST /cache_shapes/_doc/xidan
{
  "name": "西单商圈",
  "class":"景点",
  "location": {
    "type": "envelope",
    "coordinates" :[[116.378638,39.918824],[116.382878,39.913386]]
  }
}
  • id -包含预索引形状的文档的id。
  • index -预索引形状所在的索引名称。默认的形状。
  • path -指定为包含预索引形状的路径的字段。默认的形状。
  • routing -如果需要,形状文件的路由。
GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "indexed_shape": {
               "index": "cache_shapes",//缓存索引名
               "id": "xidan",//索引内id
               "path": "location" 
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

在这里插入图片描述

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值