国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、geo_shape)

這篇具有很好參考價(jià)值的文章主要介紹了Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、geo_shape)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、geo_shape)

?? 博客主頁:水滴技術(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_pointgeo_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_pointgeo_shape 的查詢,查詢的方法包括下面四種:geo_bounding_box、geo_distance、geo_polygongeo_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)(所屬文檔)。

Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、geo_shape)

上圖中,故宮的左上角坐標(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)的人,如下圖:

Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、geo_shape)

查詢時(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)排序的。

Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、geo_shape)

按距離排序,需要在上面查詢基礎(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。

Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、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)見附錄二。
Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、geo_shape)

2.4.1、查詢 intersects 相交關(guān)系

如下圖,查詢與藍(lán)色三角形“相交”的圖形有哪些,通過肉眼可以看出,1 和 2 與之相交。

Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、geo_shape)
查詢相交的語句:

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 與之不相交。

Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、geo_shape)
查詢不相交的語句:

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 圖形能否被查出。

Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、geo_shape)
查詢?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 圖形查出來。

Elasticsearch 核心技術(shù)(十):GEO 地理查詢(geo_bounding_box、geo_distance、geo_shape)
查詢包含語句:

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é)果處理(分頁、排序、指定返回字段、去重、高亮顯示)

熱門專欄

?? 《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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Redis Geo:掌握地理空間數(shù)據(jù)的藝術(shù)

    Redis Geo:掌握地理空間數(shù)據(jù)的藝術(shù)

    歡迎來到我的博客,代碼的世界里,每一行都是一個(gè)故事 在移動(dòng)互聯(lián)網(wǎng)和物聯(lián)網(wǎng)的時(shí)代,地理位置數(shù)據(jù)無處不在。從導(dǎo)航和配送到社交網(wǎng)絡(luò)和廣告,地理位置信息正在重新定義我們與世界的互動(dòng)方式。但是,處理和分析這些大量的地理空間數(shù)據(jù)絕非易事。這時(shí),Redis Geo應(yīng)運(yùn)而

    2024年02月02日
    瀏覽(21)
  • Redis GEO地理位置信息的應(yīng)用

    Redis GEO地理位置信息的應(yīng)用

    Redis的GEO操作是一種基于地理位置信息進(jìn)行操作的功能。它使用經(jīng)度和緯度坐標(biāo)來表示地理位置,支持存儲(chǔ)地理位置信息用來實(shí)現(xiàn)諸如附近位置、搖一搖這類依賴于地理位置信息的功能。 1.GEOADD添加位置信息 將一個(gè)或多個(gè)指定的地理位置(經(jīng)度、緯度、名稱)添加到指定的鍵

    2024年02月08日
    瀏覽(20)
  • 【Redis】特殊數(shù)據(jù)類型 Geo (地理位置)

    【Redis】特殊數(shù)據(jù)類型 Geo (地理位置)

    除了五中基本的數(shù)據(jù)類型外,Redis還支持兩種特殊的數(shù)據(jù)類型,第一種 Geo (地理位置):用于存儲(chǔ)地理位置相關(guān)的數(shù)據(jù),例如經(jīng)緯度、距離等。第二種 Stream (流):是一個(gè)高級(jí)的列表類型,支持對(duì)列表的批量操作,如添加多個(gè)元素、獲取多個(gè)元素等。 Redis GEO(Geo Redis)是一個(gè)用

    2024年02月15日
    瀏覽(16)
  • IP詳細(xì)地理位置查詢:技術(shù)原理與應(yīng)用實(shí)踐

    IP詳細(xì)地理位置查詢:技術(shù)原理與應(yīng)用實(shí)踐

    IP地址是互聯(lián)網(wǎng)上設(shè)備的唯一標(biāo)識(shí),在網(wǎng)絡(luò)安全、個(gè)性化服務(wù)等領(lǐng)域具有重要意義。通過IP詳細(xì)地理位置查詢,可以獲取到IP地址所在地的具體信息,為網(wǎng)絡(luò)管理、定位服務(wù)等提供支持。IP數(shù)據(jù)云將深入探討IP詳細(xì)地理位置查詢的技術(shù)原理、應(yīng)用實(shí)踐以及相關(guān)的隱私安全考慮,以

    2024年02月21日
    瀏覽(15)
  • elasticsearch[二]-DSL查詢語法:全文檢索、精準(zhǔn)查詢(term/range)、地理坐標(biāo)查詢(矩陣、范圍)、復(fù)合查詢(相關(guān)性算法)、布爾查詢

    elasticsearch[二]-DSL查詢語法:全文檢索、精準(zhǔn)查詢(term/range)、地理坐標(biāo)查詢(矩陣、范圍)、復(fù)合查詢(相關(guān)性算法)、布爾查詢

    elasticsearch 的查詢依然是基于 JSON 風(fēng)格的 DSL 來實(shí)現(xiàn)的。 Elasticsearch 提供了基于 JSON 的 DSL(Domain Specific Language)來定義查詢。常見的查詢類型包括: 查詢所有 :查詢出所有數(shù)據(jù),一般測(cè)試用。例如:match_all 全文檢索(full text)查詢 :利用分詞器對(duì)用戶輸入內(nèi)容分詞,然后去

    2024年01月18日
    瀏覽(22)
  • Redis GEO 類型與 API 結(jié)合,地理位置優(yōu)化的絕佳實(shí)踐

    Redis GEO 類型與 API 結(jié)合,地理位置優(yōu)化的絕佳實(shí)踐

    ?? 嗨,您好 ?? 我是 vnjohn,在互聯(lián)網(wǎng)企業(yè)擔(dān)任 Java 開發(fā),CSDN 優(yōu)質(zhì)創(chuàng)作者 ?? 推薦專欄:Spring、MySQL、Nacos、Java,后續(xù)其他專欄會(huì)持續(xù)優(yōu)化更新迭代 ??文章所在專欄:MySQL、Redis、業(yè)務(wù)設(shè)計(jì) ?? 我當(dāng)前正在學(xué)習(xí)微服務(wù)領(lǐng)域、云原生領(lǐng)域、消息中間件等架構(gòu)、原理知識(shí) ?? 向我

    2024年02月08日
    瀏覽(19)
  • ElasticSearch核心詳解、文檔、查詢響應(yīng)、分頁、映射、結(jié)構(gòu)化查詢

    ElasticSearch核心詳解、文檔、查詢響應(yīng)、分頁、映射、結(jié)構(gòu)化查詢

    在Elasticsearch中,文檔以JSON格式進(jìn)行存儲(chǔ),可以是復(fù)雜的結(jié)構(gòu),如: 其中,card是一個(gè)復(fù)雜對(duì)象,嵌套的Card對(duì)象 元數(shù)據(jù)(metadata) ???????一個(gè)文檔不只有數(shù)據(jù)。它還包含了元數(shù)據(jù)(metadata)——關(guān)于文檔的信息。 三個(gè)必須的元數(shù)據(jù)節(jié)點(diǎn)是: index ???????索引(index)類似于關(guān)

    2024年02月12日
    瀏覽(21)
  • R語言【paleobioDB】——pbdb_collections_geo():從PBDB獲取根據(jù)地理位置信息篩選的采集號(hào)的基本信息

    Package? paleobioDB ?version 0.7.0 paleobioDB 包在2020年已經(jīng)停止更新,該包依賴PBDB v1 API。 可以選擇在Index of /src/contrib/Archive/paleobioDB (r-project.org)下載安裝包后,執(zhí)行本地安裝。 參數(shù)【...】 :可通過 API 調(diào)用的參數(shù)。所有可用參數(shù)詳見?PBDB Data Service: Single fossil collection。 根據(jù)參數(shù)發(fā)

    2024年01月22日
    瀏覽(20)
  • 【大數(shù)據(jù)入門核心技術(shù)-ElasticSearch】(二)ElasticSearch整體架構(gòu)和重要工作原理

    目錄 一、整體架構(gòu)圖 二、重要工作原理 1、文檔寫入原理 2、文檔檢索原理

    2024年02月05日
    瀏覽(29)
  • 《Hadoop核心技術(shù)》Hbase集群部署,創(chuàng)建表,刪除表,插入數(shù)據(jù),查詢數(shù)據(jù)

    《Hadoop核心技術(shù)》Hbase集群部署,創(chuàng)建表,刪除表,插入數(shù)據(jù),查詢數(shù)據(jù)

    額前言: ????????我是一名正在學(xué)習(xí)《Hadoop核心技術(shù)》的學(xué)生,今天跟大家分享一下在虛擬機(jī)上在Hadoop集群中用Hbase進(jìn)行簡(jiǎn)單的增刪查 可以進(jìn)行隨機(jī)訪問的存取和檢索數(shù)據(jù)的存儲(chǔ)平臺(tái) ????????HBase 是一個(gè)開源的、分布式的、版本化的 NoSQL 數(shù)據(jù)庫(也即非關(guān)系型數(shù)據(jù)庫

    2024年02月03日
    瀏覽(55)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包