# /
# 地理查询(Geo queries)
Elasticsearch支持两种类型的地理数据:
- 支持lat/lon对的geo_point字段和
- 支持点、线、圆、多边形、多重多边形等的geo_shape字段。
Geo-bounding box
Geo-distance
Geo-polygon
Geo-shape
1
2
3
4
2
3
4
# 矩形查询(Geo-bounding box query)
匹配与边界框geo_bounding_box相交的geo_point和geo_shape值。
# 在geo_bounding_box中查找geo_point
使用geo_bounding_box过滤器匹配与边界框相交的geo_point值。
#使用geo_point添加一个点
PUT /my_locations
{
"mappings": {
"properties": {
"pin": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
}
}
PUT /my_locations/_doc/1
{
"pin": {
"location": {
"lat": 40.12,
"lon": -71.34
}
}
}
#在geo_bounding_box中查找geo_point
GET my_locations/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_bounding_box": {//要定义长方体,请提供两个相对角的地理点值。
"pin.location": {
"top_left": {
"lat": 40.73,
"lon": -74.1
},
"bottom_right": {
"lat": 40.01,
"lon": -71.12
}
}
}
}
}
}
}
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
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
# 在geo_bounding_box中查找geo_shape
使用相同的过滤器匹配与边界框相交的geo_shape值:
注意:7.10.2不支持。
#使用geo_shape添加一个点
DELETE my_geoshapes
PUT /my_geoshapes
{
"mappings": {
"properties": {
"pin": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
}
}
PUT /my_geoshapes/_doc/1
{
"pin": {
"location": {
"type" : "point",
"coordinates" : [-71.34 ,40.12]
}
}
}
#使用geo_shape添加一个多边形
PUT /my_geoshapes/_doc/2
{
"pin": {
"location": {
"type" : "polygon",
"coordinates" : [[[13.0 ,51.5], [15.0, 51.5], [15.0, 54.0], [13.0, 54.0], [13.0 ,51.5]]]
}
}
}
#在geo_bounding_box中查找geo_shape
GET my_geoshapes/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_bounding_box": {
"pin.location": {
"top_left": {
"lat": 51.5,
"lon": 13.0
},
"bottom_right": {
"lat": 40.01,
"lon": -71.12
}
}
}
}
}
}
}
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
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
# 在geo_bounding_box中查询geo_point与geo_shape
要同时匹配geo_point和geo_shape值,请搜索这两个索引:
GET my_locations,my_geoshapes/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_bounding_box": {
"pin.location": {
"top_left": {
"lat": 40.73,
"lon": -74.1
},
"bottom_right": {
"lat": 40.01,
"lon": -71.12
}
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 形状查询(Geo-shape query)
# 空间关系
INTERSECTS - (default) 返回与查询几何相交的所有文档。
DISJOINT - 返回与查询几何不相交的所有文档。
WITHIN - 返回在查询几何内的所有文档。不支持线几何。
CONTAINS - 返回包含查询几何的所有文档。
# 内联形状定义(Inline Shape Definition)
地理图像查询使用GeoJSON来表示形状。
# 在geo_shape中查找geo_shape
#存放一个geo_shape类型的点:
DELETE example
PUT /example
{
"mappings": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
POST /example/_doc?refresh
{
"name": "Wind & Wetter, Berlin, Germany",
"location": {
"type": "point",
"coordinates": [ 13.400544, 52.530286 ]
}
}
#在GeoJSON表示的多边形envelope上查询该点:
GET /example/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "envelope",
"coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
},
"relation": "within"
}
}
}
}
}
}
#返回一个geo_shape类型的点
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "example",
"_type" : "_doc",
"_id" : "Z6xCHYwBV6iNAAFtuE67",
"_score" : 1.0,
"_source" : {
"name" : "Wind & Wetter, Berlin, Germany",
"location" : {
"type" : "point",
"coordinates" : [
13.400544,
52.530286
]
}
}
}
]
}
}
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
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
# 在geo_shape中查找geo_point
#存放一个geo_point类型的点:
DELETE example_points
PUT /example_points
{
"mappings": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
PUT /example_points/_doc/1?refresh
{
"name": "Wind & Wetter, Berlin, Germany",
"location": [13.400544, 52.530286]
}
#使用相同的查询,将返回一个geo_point类型的点。
GET /example_points/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "envelope",
"coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
},
"relation": "intersects"
}
}
}
}
}
}
#返回一个geo_point类型的点
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "example_points",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "Wind & Wetter, Berlin, Germany",
"location" : [
13.400544,
52.530286
]
}
}
]
}
}
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
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
# 预索引形状(Pre-Indexed Shape)
以下是将过滤器与预索引形状一起使用的示例:
DELETE shapes
PUT /shapes
{
"mappings": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
#提前创建一个多边形
PUT /shapes/_doc/deu
{
"location": {
"type": "envelope",
"coordinates" : [[13.0, 53.0], [14.0, 52.0]]
}
}
#在这个多边形上查找点(索引example是一个点列表)
GET /example/_search
{
"query": {
"bool": {
"filter": {
"geo_shape": {
"location": {
"indexed_shape": {
"index": "shapes",
"id": "deu",
"path": "location"
}
}
}
}
}
}
}
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
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
# 距离查询(Geo-distance)
匹配geo_distance内的geo_point和geo_shape值。
# 在geo_distance范围内查找geo_point
DELETE my_locations
PUT /my_locations
{
"mappings": {
"properties": {
"pin": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
}
}
PUT /my_locations/_doc/1
{
"pin": {
"location": {
"lat": 40.12,
"lon": -71.34
}
}
}
#在方圆200公里范围内查找geo_point
GET /my_locations/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "200km",
"pin.location": {
"lat": 40,
"lon": -70
}
}
}
}
}
}
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
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
# 在geo_distance范围内查找geo_shape
DELETE my_geoshapes
PUT /my_geoshapes
{
"mappings": {
"properties": {
"pin": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
}
}
PUT /my_geoshapes/_doc/1
{
"pin": {
"location": {
"type" : "polygon",
"coordinates" : [[[13.0 ,51.5], [15.0, 51.5], [15.0, 54.0], [13.0, 54.0], [13.0 ,51.5]]]
}
}
}
#在方圆200公里范围内查找geo_shape
GET my_geoshapes/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "200km",
"pin.location": {
"lat": 51.5,
"lon": 13.0
}
}
}
}
}
}
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
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
# 在geo_distance范围内查找geo_point与geo_shape
GET my_locations,my_geoshapes/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "200km",
"pin.location": {
"lat": 40,
"lon": -70
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 多边形查询Geo-polygon query
注意:7.12中已弃用。使用Geo-shape代替。
# Geo-shape field type
Elasticsearch Type
point
linestring
polygon
multipoint
multilinestring
multipolygon
geometrycollection
envelope
circle
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# point
POST /example/_doc
{
"location" : {
"type" : "point",
"coordinates" : [-77.03653, 38.897676]
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# linestring
POST /example/_doc
{
"location" : {
"type" : "linestring",
"coordinates" : [[-77.03653, 38.897676], [-77.009051, 38.889939]]
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# polygon
POST /example/_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] ]
]
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# geometrycollection
POST /example/_doc
{
"location" : {
"type": "geometrycollection",
"geometries": [
{
"type": "point",
"coordinates": [100.0, 0.0]
},
{
"type": "linestring",
"coordinates": [ [101.0, 0.0], [102.0, 1.0] ]
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# envelope
POST /example/_doc
{
"location" : {
"type" : "envelope",
"coordinates" : [ [100.0, 1.0], [101.0, 0.0] ]
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# circle
注意:以下写法新版本已经不支持。
POST /circle-example/_doc
{
"location" : {
"type" : "circle",
"coordinates" : [101.0, 1.0],
"radius" : "100m"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
相反,使用圆形摄取处理器将圆形近似为多边形。
#圆形处理器Circle processor
PUT _ingest/pipeline/polygonize_circles
{
"description": "translate circle to polygon",
"processors": [
{
"circle": {
"field": "circle",
"error_distance": 28.0,
"shape_type": "geo_shape"
}
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
PUT circles
{
"mappings": {
"properties": {
"circle": {
"type": "geo_shape"
}
}
}
}
PUT circles/_doc/2?pipeline=polygonize_circles
{
"circle": {
"type": "circle",
"radius": "40m",
"coordinates": [30, 10]
}
}
GET circles/_doc/2
#返回结果是一个多边形
{
"_index" : "circles",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"circle" : {
"type" : "Polygon",//是一个多边形
"coordinates" : [
[
[
30.000365257263184,
10.0
],
[
30.000111397193788,
10.00034284530941
],
[
29.999706043744222,
10.000213571721195
],
[
29.999706043744222,
9.999786428278805
],
[
30.000111397193788,
9.99965715469059
],
[
30.000365257263184,
10.0
]
]
]
}
}
}
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
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