在笔者之前的文章中,已经介绍过Es对空间类型geo_Point的支持了。今天笔者来介绍一下另一种重要的空间类型geo_shape。
使用geo_shape类型,可以支持对任意空间几何形状的检索,比如圆形、矩形、多边形。如果我们存储的数据是线、面类型,都可以用geo_shape。另外,geo_shape也是可以兼容点的存储的。
2 geo_shape选项如果要使用geo_shape类型进行数据存储,需要在创建mapping时显式指定。可以支持的选项包括:
- tree:可以显示指定前缀树的实现,支持geohash或者quadtree;
- precision:存储精度,默认是50m(对应层级21);
- tree_levels:树的层级,默认21(对应精度50m);
- strategy:检索策略,默认是term,可以指定为recursive;
在插入geo_shape类型的数据之前,首先需要创建mapping(可以理解为关系型数据库中的表结构)
PUT /test { "mappings": { "properties": { "location": { "type": "geo_shape", "precision": "5km" } } } }
4 geo_shape数据存储示例geo_shape存储的是空间数据,支持geojson或者WKT格式。
geojson/WKT/es中的空间类型
4.1 存储Point类型数据——geojson格式
POST /test/_doc { "location" : { "type" : "point", "coordinates" : [-77.03653, 38.897676] } }
4.2 存储Point类型数据——WKT格式
POST /test/_doc { "location" : "POINT (-77.03653 38.897676)" }
4.3 存储多边形(polygon)数据
POST /test/_doc { "location" : { "type" : "polygon", "coordinates" : [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] } } POST /test/_doc { "location" : "POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0))" }
4.4 存储线(multilinestring)的数据
POST /test/_doc { "location" : { "type" : "multilinestring", "coordinates" : [ [ [102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0] ], [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0] ], [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8] ] ] } }
5 更多本文主要介绍了geo_shape的基础存储示例,后续会出一系列文章深度讲解Es对空间查询的支持。
欢迎关注笔者,每天分享架构干货。
,