0%

elasticsearch(十二)geo

地理位置搜索

  • latitude:维度 缩写:lat
  • longitude:经度 缩写:lon

经纬度坐标,只支持WGS84坐标系,坐标范围Lat值为[-90,90],Lon为[-180,180],超出这个访问的点在创建时会报错,通过创建mapping时指定ignore_malformed来忽略格式错误的地理位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#创建geo_point类型字段
PUT /geo_point
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"location":{
"type": "geo_point",
"ignore_malformed": true
}
}
}
}
#创建geo_shape mapping
PUT /geo_shape
{
"mappings": {
"properties": {
"location":{
"type": "geo_shape"
}
}
}
}

存储数据类型

geo point-存储坐标点

方式一

1
2
3
4
5
6
7
8
PUT /geo_point/_doc/1
{
"name":"地点1",
"location":{
"lat":1.0,
"lon":1.0
}
}

方式二

1
2
3
4
5
6
#lat,lot
PUT /geo_point/_doc/2
{
"name":"地点2",
"location":"1.0,3.00"
}

方式三

1
2
3
4
5
6
#[lot,lat]
PUT /geo_point/_doc/3
{
"name":"地点3",
"location":[3.0,3.0]
}

方式四

1
2
3
4
5
6
#WKT  (lot lat)
PUT /geo_point/_doc/4
{
"name":"地点4",
"location":"POINT(1.0 3.0)"
}

方式五

Geo哈希 点击跳转查看外链介绍

geo shape-存储几何图形

点(point)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#创建一个点 [lot,lat]
POST /geo_shape/_doc/1
{
"name":"point-地点s1",
"location":{
"type":"point",
"coordinates":[1.0,1.0]
}
}
#WKT创建 (lot lat)
POST /geo_shape/_doc/2
{
"name":"point-WKT-地点s1",
"location":"POINT (1.0 1.0)"
}

线段(linestring)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#创建线段
POST /geo_shape/_doc/3
{
"name":"linestring-线段",
"location":{
"type":"linestring",
"coordinates":[[0.0,0.0],[0.0,1.0]]
}
}
#WKT方式创建线程,经度纬度之间需要空格,两个点之间英文逗号分割
POST /geo_shape/_doc/4
{
"name":"linestring-WKT-线段",
"location":"LINESTRING(0.0 0.0,0.0 1.0)"
}

矩形(envelope)

1
2
3
4
5
6
7
8
9
#矩形
POST /geo_shape/_doc/8
{
"name":"矩形",
"location":{
"type":"envelope",
"coordinates":[[0.0,1.0],[3.0,0.0]]
}
}

多边形(polygon)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#多边形 最后一个点需要与第一个点重叠形成闭环,外层有两层[],表示可以存储多个多表型
POST /geo_shape/_doc/5
{
"name":"polygon-多边形",
"location":{
"type":"polygon",
"coordinates":[
[
[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]
]
]
}
}
#多线性-WKT 存在多个多边形所以需要两层括号
POST /geo_shape/_doc/6
{
"name":"polygon-WKT-多边形",
"location":"POLYGON ((0.0 0.0,1.0 0.0,1.0 1.0,0.0 1.0,0.0 0.0))"
}

圆形(circle)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#圆形 7+不支持画圆,需要预先创建画圆的管道(整个es集群全局存在),将圆形转换成多边形,只要点足够多,圆形就足够圆
#管道
PUT /_ingest/pipeline/my_circles
{
"description": "圆形转换多边形",
"processors": [
{
"circle": {
"field": "location",
"error_distance": "1",
"shape_type": "geo_shape"
}
}
]
}
POST /geo_shape/_doc/7?pipeline=my_circles
{
"name":"圆形",
"location":{
"type":"circle",
"coordinates":[0.0,0.0],
"radius":"1m"
}
}

圆形处理精度解释:

表示圆的多边形的精度定义为error_distance。这种差异越小,多边形越接近理想圆。下表是旨在帮助捕获在给定不同输入的情况下圆的半径如何影响多边形的边数的表格。最小边数为4,最大为1000。

error_distance越小,生成的点越多,越趋近于圆,但复杂度增加

error_distance 半径(米) 多边形的边数
1 1 4
1 10 14
1 100 45
1 1,000 141
1 10,000 445
1 100,000 1000

查询类型

geo bounding box (矩形查询)

​ 在同一个平面内,两个点确定一个矩形,搜索矩形内的坐标。

  • top_left:矩形左上点坐标
  • bottom_right:矩形右上角表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#矩形查询,两点确定一个矩形,返回矩形内的点
GET /geo_point/_search
{
"query": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 4.0,
"lon": 0.0
},
"bottom_right": {
"lat": 0.0,
"lon": 4.0
}
}
}
}
}

geo distance(半径查询)

以某个点为圆心查找指定半径的圆内的坐标。

distance:距离单位,默认是米,支持以下选项

1
2
3
4
5
6
7
8
9
Mile(英里):mi 或者 miles
Yard(码):yd 或者yards
Feet(英尺):ft 或者feet
Inch(英寸):in 或者inch
Kilometer(公里):km 或者kilometers
Meter(米):m后者meters
Centimeter(厘米):cm 或者centimeters
Millimeter(毫米):mm 或者millimeters
Nautical mile(海里):NM, nmi, 或者nauticalmiles

distance_type:计算距离的方式

1
2
arc(默认值):更准确,但是速度慢
plane:(更快,但在长距离和极点附近不准确)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#半径查询,基于点指定半径进行搜索
GET /geo_point/_search
{
"query": {
"bool": {
"filter": [
{
"geo_distance": {
"distance": "200km",
"location": {
"lat": 1.1,
"lon": 1.1
}
}
}
]
}
}
}
#半径查询,基于点指定半径进行搜索,距离排序
GET /geo_point/_search
{
"query": {
"bool": {
"filter": [
{
"geo_distance": {
"distance": "200km",
"location": {
"lat": 1.1,
"lon": 1.1
}
}
}
]
}
},
"sort": [
{
"_geo_distance": {
"order": "asc",
"location": {
"lat": 1.1,
"lon": 1.1
}
}
}
]
}

geo_polygon(多边形)

查找给定多个点连成的多边形内的坐标。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#多边形查询 不需要收尾封闭
GET /geo_point/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"geo_polygon": {
"location": {
"points": [
{
"lat": 0.0,
"lon": 2.0
},
{
"lat": 0.0,
"lon": 5.0
},
{
"lat": 4.0,
"lon": 3.0
}
]
}
}
}
]
}
}
}

geo_shape(特殊几何形状)

支持指定几何图形相交、包含或是不相交等图形检索

创建数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#创建mapping
PUT /geo_shape_test
{
"mappings": {
"properties": {
"location":{
"type": "geo_shape"
}
}
}
}
#A
POST /geo_shape_test/_doc/A
{
"name":"红框-A",
"location":{
"type":"envelope",
"coordinates":[[1.0,7.0],[6.0,1.0]]
}
}
#B
POST /geo_shape_test/_doc/B
{
"name":"绿框-B",
"location":{
"type":"envelope",
"coordinates":[[4.0,8.0],[8.0,5.0]]
}
}
#C
POST /geo_shape_test/_doc/C
{
"name":"蓝框-C",
"location":{
"type":"envelope",
"coordinates":[[2.0,4.0],[4.0,2.0]]
}
}
#P1
POST /geo_shape_test/_doc/P1
{
"name":"P1",
"location":{
"type":"point",
"coordinates":[3.0,3.0]
}
}
#P2
POST /geo_shape_test/_doc/P2
{
"name":"P2",
"location":{
"type":"point",
"coordinates":[5.0,6.0]
}
}
#P3
POST /geo_shape_test/_doc/P3
{
"name":"P3",
"location":{
"type":"point",
"coordinates":[7.0,7.0]
}
}
#P4
POST /geo_shape_test/_doc/P4
{
"name":"P4",
"location":{
"type":"point",
"coordinates":[3.0,5.0]
}
}
#P5
POST /geo_shape_test/_doc/P5
{
"name":"P5",
"location":{
"type":"point",
"coordinates":[7.0,3.0]
}
}

Relations(空间关系)

  • intersects- (默认,相交或包含其中的文档)
    Return all documents whose shape field intersects the query geometry。
    返回形状字段与查询几何图形相交的所有文档
  • disjoint(没有相交)
    Return all documents whose shape field has nothing in common with the query geometry 
    返回形状字段与查询几何形状没有任何共同之处的所有文档
  • within(被查询形状包含)
    Return all documents whose shape field is within the query geometry。    
    返回形状字段位于查询几何形状内的所有文档
  • contains(包含查询形状)
    Return all documents whose shape field contains the query geometry。
    返回其形状字段包含查询几何图形的所有文档

Inline Shape Definition:内联形状,指定形状查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
GET /geo_shape_test/_search
{
"query": {
"bool": {
"filter": [
{
"geo_shape": {
"location": {
"shape": {
"type": "envelope",
"coordinates": [
[
1.0,
7.0
],
[
6.0,
1.0
]
]
},
"relation": "within"
}
}
}
]
}
}
}

Pre-Indexed Shape:预定义形状,使用已创建形状查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
GET /geo_shape_test/_search
{
"query": {
"bool": {
"filter": [
{
"geo_shape": {
"location": {
"indexed_shape": {
"index": "geo_shape_test",
"id": "A",
"path": "location"
},
"relation": "within"
}
}
}
]
}
}
}