?? 博客主頁:水滴技術(shù)
?? 支持水滴:點(diǎn)贊?? + 收藏? + 留言??
?? 訂閱專欄:大數(shù)據(jù)核心技術(shù)從入門到精通
大家好,我是水滴~~
地理信息查詢是 Elasticsearch 的重要特性之一,其 GEO 功能主要用于地理信息的存儲(chǔ)和搜索。本篇主要內(nèi)容:介紹 Elasticsearch 的兩種地理數(shù)據(jù)類型,并通過一些實(shí)例來講述geo_bounding_box
查詢、geo_distance
查詢和 geo_shape
查詢。
一、地理數(shù)據(jù)類型
在介紹地理查詢前,我們先來了解下地理數(shù)據(jù)類型。Elasticsearch 支持兩種地理數(shù)據(jù)類型:geo_point
和 geo_shape
。在之前的文章《Elasticsearch 核心技術(shù)(五):常用數(shù)據(jù)類型詳解》中有介紹過,學(xué)過的友友可以跳過本章節(jié)。
注意:Elasticsearch 使用的是
WGS-84
坐標(biāo)系,如果你是直接從高德地圖中獲取的經(jīng)緯度坐標(biāo),需要轉(zhuǎn)換一下再存儲(chǔ),否則會(huì)有精度問題。當(dāng)然,如果對(duì)精度要求沒有那么高,請(qǐng)忽略。
1.1、geo_point 地理點(diǎn)類型
geo_point
數(shù)據(jù)類型用來存儲(chǔ)一個(gè)由經(jīng)緯度組成的地理點(diǎn),也就是地理坐標(biāo)。
地理坐標(biāo)在生活中有很多用處,比如:在一個(gè)陌生的城市,可以導(dǎo)航到指定的酒店;點(diǎn)外賣時(shí)可以查看附近有哪些好吃的;走累了可以看看哪里有共享單車等等。
1.1.1、創(chuàng)建一個(gè)含有 geo_point 字段的索引
下面示例創(chuàng)建一個(gè) index_geo_point
索引,其中 location
字段為 geo_point
類型。
PUT index_geo_point
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"location": {
"type": "geo_point"
}
}
}
}
1.1.2、通過“對(duì)象”指定 geo_point
可以通過一個(gè)對(duì)象來表示地理點(diǎn),其中 lat
表示緯度,lon
表示經(jīng)度。
POST index_geo_point/_doc/1
{
"id": 1,
"location": {
"lat": 39.917846,
"lon": 116.397058
}
}
1.1.3、通過“字符串”指定 geo_point
可以通過一個(gè)字符串表示地理點(diǎn),格式為:"lat, lon"
。
注意:字符串的地理點(diǎn)的順序?yàn)?lat,lon,這與數(shù)組和WKT相反。
POST index_geo_point/_doc/2
{
"id": 2,
"location": "39.917846, 116.397058"
}
1.1.4、通過“地理哈?!敝付?geo_point
可以通過地理點(diǎn)的哈希值來表示地理點(diǎn)。
POST index_geo_point/_doc/3
{
"id": 3,
"location": "drm3btev3e86"
}
1.1.5、通過“數(shù)組”指定 geo_point
可以通過一個(gè)數(shù)組來表示地理點(diǎn),格式為:[lon, lat]
POST index_geo_point/_doc/4
{
"id": 4,
"location": [116.397058, 39.917846]
}
1.1.6、通過“WKT”指定 geo_point
可以通過 Well-Known Text 格式的 POINT
來表示地理點(diǎn),格式為:"POINT(lon, lat)"
POST index_geo_point/_doc/5
{
"id": 5,
"location": "POINT(116.397058, 39.917846)"
}
1.2、geo_shape 地理形狀類型
geo_shape
數(shù)據(jù)類型用于存儲(chǔ)地理形狀,例如:直線、矩形、多邊形等。
也有很多場(chǎng)景只使用一個(gè)地理點(diǎn)是無法滿足的,比如:一所學(xué)校、一處旅游景點(diǎn)、一座城市等等,想要表示這樣大面積的地點(diǎn),就需要用到 geo_shape
了。
下面一起看下 geo_shape
類型是如何指定的。
1.2.1、創(chuàng)建一個(gè)含有 geo_shape 字段的索引
下面示例創(chuàng)建一個(gè) index_geo_shape
索引,其中 location
字段為 geo_shape
類型。
PUT index_geo_shape
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"location": {
"type": "geo_shape"
}
}
}
}
1.2.2、通過 Point 指定單個(gè)地理坐標(biāo)
Point
類型用于指定單個(gè)地理坐標(biāo)點(diǎn),坐標(biāo)數(shù)組格式為:[lon, lat]
POST index_geo_shape/_doc/1
{
"id": 1,
"location": {
"type": "Point",
"coordinates": [116.397058, 39.917846]
}
}
1.2.3、通過 LineString 指定一條線
LineString
類型用于指定一條線,該線由兩個(gè)或多個(gè)位置點(diǎn)組成。
POST index_geo_shape/_doc/2
{
"id": 2,
"location": {
"type": "LineString",
"coordinates": [ [116.397058, 39.917846], [116.397058, 38.111111] ]
}
}
1.2.4、通過 Polygon 指定一個(gè)多邊形
Polygon
類型用于指定一個(gè)多邊形,該多邊形要求第一個(gè)點(diǎn)和最后一個(gè)點(diǎn)必須相同,表示該多邊形是閉合的。
POST index_geo_shape/_doc/3
{
"id": 3,
"location": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
}
}
這里要注意,
coordinates
是一個(gè)數(shù)組,表示可以指定多個(gè)多邊形。
下面示例指定了兩個(gè)多邊形,一個(gè)數(shù)組表示多邊形的外部邊界,另一個(gè)數(shù)組表示多邊形的內(nèi)部邊界,即表示一個(gè)帶孔的多邊形。
POST index_geo_shape/_doc/4
{
"id": 4,
"location" : {
"type" : "Polygon",
"coordinates" : [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],
[ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]
]
}
}
1.2.5、通過 MultiPoint 指定多個(gè)點(diǎn)
MultiPoint
類型用于指定多個(gè)點(diǎn)。
POST index_geo_shape/_doc/5
{
"id": 5,
"location" : {
"type" : "MultiPoint",
"coordinates" : [
[102.0, 2.0], [103.0, 2.0]
]
}
}
1.2.6、通過 MultiLineString 指定多條線
MultiLineString
類型用于指定多條線。
POST index_geo_shape/_doc/6
{
"id": 6,
"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] ]
]
}
}
1.2.7、通過 MultiPolygon 指定多個(gè)多邊形
MultiPolygon
類型用于指定多個(gè)多邊形。
POST index_geo_shape/_doc/7
{
"id": 7,
"location" : {
"type" : "MultiPolygon",
"coordinates" : [
[ [[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]] ],
[ [[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]] ]
]
}
}
1.2.8、通過 GeometryCollection 指定地理形狀的集合
GeometryCollection
類型用于指定一個(gè)地理形狀的集合,即可以包含多個(gè)上面所屬的地理形狀。
POST index_geo_shape/_doc/8
{
"id": 8,
"location" : {
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",
"coordinates": [100.0, 0.0]
},
{
"type": "LineString",
"coordinates": [ [101.0, 0.0], [102.0, 1.0] ]
}
]
}
}
1.2.9、通過 envelope 指定矩形或包絡(luò)線
envelope
類型用于指定一個(gè)矩形或包絡(luò)線,它由兩個(gè)坐標(biāo)組成,分別表示左上角和右下角的坐標(biāo)。
POST index_geo_shape/_doc/9
{
"id": 9,
"location" : {
"type" : "envelope",
"coordinates" : [ [100.0, 1.0], [101.0, 0.0] ]
}
}
1.2.10、通過 circle 指定圓形
circle
類型用于指定一個(gè)圓形,需要指定圓心坐標(biāo)和半徑。
注意:映射字段時(shí),圓形不能使用默認(rèn)的策略,需要將
strategy
指定為recursive
。如下:
PUT index_geo_shape2
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"location": {
"type": "geo_shape",
"strategy": "recursive"
}
}
}
}
添加文檔時(shí),radius
用于指定半徑,如果未指定單位,默認(rèn)為米
POST index_geo_shape/_doc/10
{
"id": 10,
"location" : {
"type" : "circle",
"coordinates" : [101.0, 1.0],
"radius" : "100m"
}
}
二、地理查詢
地理查詢主要用于上文講的兩種地理數(shù)據(jù)類型 geo_point
和 geo_shape
的查詢,查詢的方法包括下面四種:geo_bounding_box
、geo_distance
、geo_polygon
和 geo_shape
。
下面我們分別介紹它們具體的應(yīng)用。
2.1、geo_bounding_box 矩形過濾
geo_bounding_box
用于查詢所有落入矩形的地理點(diǎn)所屬文檔。查詢時(shí)需要指定兩個(gè)坐標(biāo)點(diǎn)左上角和右下角,這兩個(gè)點(diǎn)就可以確定一個(gè)矩形,而查詢的結(jié)果為,所有定位點(diǎn)落入該矩形的文檔。
下面我們舉例說明:
2.1.1、查詢矩形內(nèi)的定位點(diǎn)(文檔)
我們以“故宮博物院”為例,取其左上角和右下角的坐標(biāo)點(diǎn),會(huì)形成一個(gè)矩形,然后查詢?cè)摼匦蝺?nèi)的坐標(biāo)點(diǎn)(所屬文檔)。
上圖中,故宮的左上角坐標(biāo)點(diǎn)為
116.391978,39.922561
,右下角坐標(biāo)點(diǎn)為116.402149,39.913443
。
另外找了 6 個(gè)點(diǎn),分別代表 6 個(gè)文檔的位置,它們的坐標(biāo)點(diǎn)如下:
1:116.39775,39.92029
2:116.395947,39.916208
3:116.410624,39.91871
4:116.397235,39.909823
5:116.385304,39.917591
6:116.396548,39.92832
從肉眼可以看出,1 和 2 號(hào)坐標(biāo)點(diǎn)在故宮的矩形框內(nèi),我們通過該實(shí)例,看能否準(zhǔn)確的查出內(nèi)容。
我們使用索引 es_location_001
演示該示例,索引的創(chuàng)建,及文檔的添加請(qǐng)見附錄一,這里我們只講查詢**。**
查詢時(shí)需要指定
geo_bounding_box
類型,并指定top_left
左上角的坐標(biāo),bottom_right
右下角的坐標(biāo)。
GET es_location_001/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 39.922561,
"lon": 116.391978
},
"bottom_right": {
"lat": 39.913443,
"lon": 116.402149
}
}
}
}
}
}
}
查詢結(jié)果:通過查詢結(jié)果可以看出 1、2 文檔被查出,正是我們猜測(cè)的結(jié)果。
{
"took" : 929,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "es_location_001",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 1,
"location" : [
116.39775,
39.92029
]
}
},
{
"_index" : "es_location_001",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"id" : 2,
"location" : [
116.395947,
39.916208
]
}
}
]
}
}
2.2、geo_distance 距離查詢(圓形過濾)
geo_distance
用于查詢距離中心點(diǎn)指定范圍內(nèi)的地理點(diǎn)所屬文檔,也就是圓形過濾。查詢時(shí)需要指定圓心的坐標(biāo),以及半徑長(zhǎng)度,就可以查詢方圓內(nèi)的文檔了。
下面我們舉例說明:
2.2.1、查詢“附近的人”
我們還是使用 es_location_001
索引那 6 個(gè)坐標(biāo)點(diǎn)來演示,而假設(shè)“我”當(dāng)前的坐標(biāo)為 116.410539,39.912983
,然后看下附近兩公里內(nèi)的人,如下圖:
查詢時(shí)需要指
geo_distance
并指定定圓心坐標(biāo)(“我”的位置)和半徑距離distance
GET /es_location_001/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "2km",
"location": {
"lat": 39.912983,
"lon": 116.410539
}
}
}
}
}
}
查詢結(jié)果:通過查詢結(jié)果可以看出,1、2、3、4個(gè)點(diǎn)距離“我”在兩公里內(nèi)。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "es_location_001",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 1,
"location" : [
116.39775,
39.92029
]
}
},
{
"_index" : "es_location_001",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"id" : 2,
"location" : [
116.395947,
39.916208
]
}
},
{
"_index" : "es_location_001",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"id" : 3,
"location" : [
116.410624,
39.91871
]
}
},
{
"_index" : "es_location_001",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"id" : 4,
"location" : [
116.397235,
39.909823
]
}
}
]
}
}
2.2.2、查詢“附近的人”并按距離排序
上面的示例只顯示了查詢結(jié)果,并沒有顯示每個(gè)“人”距離“我”有多遠(yuǎn),也沒有排序。下面我們看怎么實(shí)現(xiàn)排序的。
按距離排序,需要在上面查詢基礎(chǔ)上增加
sort
選項(xiàng),其中:_geo_distance
為“我”的坐標(biāo);order
指定asc
表示由近到遠(yuǎn)排序;unit
指定km
,表示結(jié)果顯示的距離單位。
GET /es_location_001/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "2km",
"location": {
"lat": 39.912983,
"lon": 116.410539
}
}
}
}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 39.912983,
"lon": 116.410539
},
"order": "asc",
"unit": "km"
}
}
]
}
查詢結(jié)果:查詢結(jié)果便是按距離排序的結(jié)果,并且返回的 sort
選項(xiàng)為每個(gè)“人”距離“我”的距離
{
"took" : 26,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "es_location_001",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : {
"id" : 3,
"location" : [
116.410624,
39.91871
]
},
"sort" : [
0.6368510653173584
]
},
{
"_index" : "es_location_001",
"_type" : "_doc",
"_id" : "4",
"_score" : null,
"_source" : {
"id" : 4,
"location" : [
116.397235,
39.909823
]
},
"sort" : [
1.187873070108458
]
},
{
"_index" : "es_location_001",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : {
"id" : 2,
"location" : [
116.395947,
39.916208
]
},
"sort" : [
1.2951412302092007
]
},
{
"_index" : "es_location_001",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : {
"id" : 1,
"location" : [
116.39775,
39.92029
]
},
"sort" : [
1.360073305586383
]
}
]
}
}
2.3、geo_polygon 多邊形查詢(已過時(shí))
在 7.12
版本中已棄用,官方推薦使用 geo_shape
。
2.4、geo_shape 地理形狀查詢
geo_shape
用于查詢地理形狀的空間關(guān)系。例如:與指定地理形狀相交、包含、不相交的地理形狀等。
而 Elasticsearch 支持四種空間關(guān)系:
-
intersects
:相交,用于查詢所有與指定圖形相交的文檔。 -
disjoint
:不相交,用于查詢所有與指定圖形不相交的文檔。 -
within
:在…之內(nèi),用于查詢所有在指定圖形之內(nèi)的文檔。 -
contains
:包含,用于查詢所有包含指定圖形的文檔。
下面我們舉例說明:
我們先準(zhǔn)備三個(gè)圖形,我用的是矩形,如下圖所示。并將它們添加到 es_location_002
索引中,索引的創(chuàng)建,及文檔的添加請(qǐng)見附錄二。
2.4.1、查詢 intersects 相交關(guān)系
如下圖,查詢與藍(lán)色三角形“相交”的圖形有哪些,通過肉眼可以看出,1 和 2 與之相交。
查詢相交的語句:
GET es_location_002/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "Polygon",
"coordinates": [
[ [116.382215,39.921606], [116.396892,39.919039], [116.387965,39.912719], [116.382215,39.921606] ]
]
},
"relation": "intersects"
}
}
}
}
}
}
查詢結(jié)果:1 和 2 圖形被查出,驗(yàn)證成功
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "es_location_002",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"id" : 2,
"location" : {
"type" : "Polygon",
"coordinates" : [
[
[
116.391913,
39.922199
],
[
116.40187,
39.922396
],
[
116.402127,
39.91318
],
[
116.392085,
39.913114
],
[
116.391913,
39.922199
]
]
]
}
}
},
{
"_index" : "es_location_002",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 1,
"location" : {
"type" : "Polygon",
"coordinates" : [
[
[
116.378009,
39.922199
],
[
116.385819,
39.92233
],
[
116.385819,
39.914958
],
[
116.378009,
39.914958
],
[
116.378009,
39.922199
]
]
]
}
}
}
]
}
}
2.4.2、查詢 disjoint 不相交關(guān)系
還是剛才的藍(lán)色三角形,查詢與藍(lán)色三角形“不相交”的圖形有哪些,通過肉眼可以看出,3 與之不相交。
查詢不相交的語句:
GET es_location_002/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "Polygon",
"coordinates": [
[ [116.382215,39.921606], [116.396892,39.919039], [116.387965,39.912719], [116.382215,39.921606] ]
]
},
"relation": "disjoint"
}
}
}
}
}
}
查詢結(jié)果:3 圖形被查詢,驗(yàn)證成功
{
"took" : 115,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "es_location_002",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"id" : 3,
"location" : {
"type" : "Polygon",
"coordinates" : [
[
[
116.407105,
39.923844
],
[
116.417405,
39.924437
],
[
116.417491,
39.912127
],
[
116.407277,
39.911863
],
[
116.407105,
39.923844
]
]
]
}
}
}
]
}
}
2.4.3、查詢 within 在…之內(nèi)關(guān)系
如下圖,我畫了一個(gè)大的藍(lán)色矩形,將 1 和 2 包含在內(nèi),那么查詢所有在藍(lán)色矩形內(nèi)的文檔,看下 1 和 2 圖形能否被查出。
查詢?cè)谒{(lán)色矩形之內(nèi)語句:
GET es_location_002/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "Polygon",
"coordinates": [
[ [116.374404,39.92391], [116.404616,39.924108], [116.404359,39.911732], [116.374576,39.911337], [116.374404,39.92391] ]
]
},
"relation": "within"
}
}
}
}
}
}
查詢結(jié)果:1 和 2 圖形被查出
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "es_location_002",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"id" : 2,
"location" : {
"type" : "Polygon",
"coordinates" : [
[
[
116.391913,
39.922199
],
[
116.40187,
39.922396
],
[
116.402127,
39.91318
],
[
116.392085,
39.913114
],
[
116.391913,
39.922199
]
]
]
}
}
},
{
"_index" : "es_location_002",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 1,
"location" : {
"type" : "Polygon",
"coordinates" : [
[
[
116.378009,
39.922199
],
[
116.385819,
39.92233
],
[
116.385819,
39.914958
],
[
116.378009,
39.914958
],
[
116.378009,
39.922199
]
]
]
}
}
}
]
}
}
注意:在…之內(nèi)關(guān)系也屬于相交關(guān)系,通過查詢相交的圖形,也能查出 1 和 2 圖形來。
查詢相交語句:
GET es_location_002/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "Polygon",
"coordinates": [
[ [116.374404,39.92391], [116.404616,39.924108], [116.404359,39.911732], [116.374576,39.911337], [116.374404,39.92391] ]
]
},
"relation": "intersects"
}
}
}
}
}
}
查詢結(jié)果:1 和 2 圖形被查出
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "es_location_002",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"id" : 2,
"location" : {
"type" : "Polygon",
"coordinates" : [
[
[
116.391913,
39.922199
],
[
116.40187,
39.922396
],
[
116.402127,
39.91318
],
[
116.392085,
39.913114
],
[
116.391913,
39.922199
]
]
]
}
}
},
{
"_index" : "es_location_002",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 1,
"location" : {
"type" : "Polygon",
"coordinates" : [
[
[
116.378009,
39.922199
],
[
116.385819,
39.92233
],
[
116.385819,
39.914958
],
[
116.378009,
39.914958
],
[
116.378009,
39.922199
]
]
]
}
}
}
]
}
}
2.4.4、查詢 contains 包含關(guān)系
如下圖,在 3 圖形內(nèi)畫了一個(gè)藍(lán)色的三角形,查詢所有包含三角形的文檔,看能否將 3 圖形查出來。
查詢包含語句:
GET es_location_002/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "Polygon",
"coordinates": [
[ [116.41071,39.921014], [116.41483,39.917064], [116.409766,39.915748], [116.41071,39.921014] ]
]
},
"relation": "contains"
}
}
}
}
}
}
輸出結(jié)果:3 圖形被查出來
{
"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" : "es_location_002",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"id" : 3,
"location" : {
"type" : "Polygon",
"coordinates" : [
[
[
116.407105,
39.923844
],
[
116.417405,
39.924437
],
[
116.417491,
39.912127
],
[
116.407277,
39.911863
],
[
116.407105,
39.923844
]
]
]
}
}
}
]
}
}
注意:包含關(guān)系也屬于相交關(guān)系,通過查詢相交的圖形,也能查出 3 圖形來。
查詢相交語句:
GET es_location_002/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "Polygon",
"coordinates": [
[ [116.41071,39.921014], [116.41483,39.917064], [116.409766,39.915748], [116.41071,39.921014] ]
]
},
"relation": "intersects"
}
}
}
}
}
}
查詢結(jié)果:同樣 3 圖形被查詢出來
{
"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" : "es_location_002",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"id" : 3,
"location" : {
"type" : "Polygon",
"coordinates" : [
[
[
116.407105,
39.923844
],
[
116.417405,
39.924437
],
[
116.417491,
39.912127
],
[
116.407277,
39.911863
],
[
116.407105,
39.923844
]
]
]
}
}
}
]
}
}
三、附錄
附錄一:es_location_001 索引
創(chuàng)建 es_location_001
索引:
PUT es_location_001
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"location": {
"type": "geo_point"
}
}
}
}
添加 6 個(gè)文檔:
POST es_location_001/_doc/1
{
"id": 1,
"location": [116.39775,39.92029]
}
POST es_location_001/_doc/2
{
"id": 2,
"location": [116.395947,39.916208]
}
POST es_location_001/_doc/3
{
"id": 3,
"location": [116.410624,39.91871]
}
POST es_location_001/_doc/4
{
"id": 4,
"location": [116.397235,39.909823]
}
POST es_location_001/_doc/5
{
"id": 5,
"location": [116.385304,39.917591]
}
POST es_location_001/_doc/6
{
"id": 6,
"location": [116.396548,39.92832]
}
附錄二:es_location_002 索引
創(chuàng)建 es_location_002
索引:
PUT es_location_002
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"location": {
"type": "geo_shape"
}
}
}
}
添加 3 個(gè)矩形文檔:
POST es_location_002/_doc/1
{
"id": 1,
"location": {
"type": "Polygon",
"coordinates": [
[ [116.378009,39.922199], [116.385819,39.92233], [116.385819,39.914958], [116.378009,39.914958], [116.378009,39.922199] ]
]
}
}
POST es_location_002/_doc/2
{
"id": 2,
"location": {
"type": "Polygon",
"coordinates": [
[ [116.391913,39.922199], [116.40187,39.922396], [116.402127,39.91318], [116.392085,39.913114], [116.391913,39.922199] ]
]
}
}
POST es_location_002/_doc/3
{
"id": 3,
"location": {
"type": "Polygon",
"coordinates": [
[ [116.407105,39.923844], [116.417405,39.924437], [116.417491,39.912127], [116.407277,39.911863], [116.407105,39.923844] ]
]
}
}
附錄三:地圖工具
-
高德坐標(biāo)拾取器
-
坐標(biāo)系轉(zhuǎn)換
系列文章
?? Elasticsearch 核心技術(shù)(一):Elasticsearch 安裝、配置、運(yùn)行(Windows 版)
?? Elasticsearch 核心技術(shù)(二):elasticsearch-head 插件安裝和使用
?? Elasticsearch 核心技術(shù)(三):Kibana 安裝、配置、運(yùn)行(Windows 版)
?? Elasticsearch 核心技術(shù)(四):索引管理、映射管理、文檔管理(REST API)
?? Elasticsearch 核心技術(shù)(五):常用數(shù)據(jù)類型詳解
?? Elasticsearch 核心技術(shù)(六):內(nèi)置的 8 種分詞器詳解 + 代碼示例
?? Elasticsearch 核心技術(shù)(七):IK 中文分詞器的安裝、使用、自定義字典
?? Elasticsearch 核心技術(shù)(八):常用 DSL 查詢(全文搜索、精確匹配、布爾查詢)
?? Elasticsearch 核心技術(shù)(九):搜索結(jié)果處理(分頁、排序、指定返回字段、去重、高亮顯示)文章來源:http://www.zghlxwxcb.cn/news/detail-448743.html
熱門專欄
?? 《Python入門核心技術(shù)》
?? 《IDEA 教程:從入門到精通》
?? 《Java 教程:從入門到精通》
?? 《MySQL 教程:從入門到精通》
?? 《大數(shù)據(jù)核心技術(shù)從入門到精通》文章來源地址http://www.zghlxwxcb.cn/news/detail-448743.html
到了這里,關(guān)于Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、geo_shape)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!