概念
geo_shape則表示有多個(gè)點(diǎn)連成線組成的形狀,實(shí)際開發(fā)中,如果我們的地理坐標(biāo)是一個(gè)地理形狀,則可以使用地理形狀數(shù)據(jù)類型進(jìn)行插入、查詢文檔。比如說(shuō)學(xué)校、大商場(chǎng)這種面積比較大的地理坐標(biāo),都需要geo_shape來(lái)表示。
geo_shape支持存儲(chǔ)的常用形狀數(shù)據(jù)如下:
- 點(diǎn)(point)
- 圓形(circle)
- 矩形(envelope)
- 多邊形 (polygon)
geo_shape支持的圖形搜索類型:
- intersects - 查詢的形狀與索引的形狀有重疊(默認(rèn)), 即圖形有交集則匹配。
- disjoint - 查詢的形狀與索引的形狀完全不重疊。
- within - 查詢的形狀包含索引的形狀。
geo_shape支持GeoJSON及WKT中描述的大多數(shù)地理形狀:
GeoJson:
{
"type": "Point",
"coordinates": [125.6, 10.1]
}
WKT:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-427843.html
POINT (-77.03653 38.897676)
LINESTRING (-77.03653 38.897676,-77.009051 38.889939)
POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0))
MULTIPOINT (102.0 2.0, 103.0 2.0)
MULTILINESTRING ((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0),(100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8))
MULTIPOLYGON (((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)))
GEOMETRYCOLLECTION (POINT (100.0 0.0), LINESTRING (101.0 0.0, 102.0 1.0))
創(chuàng)建索引mapping
PUT /city
{
"mappings": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
添加數(shù)據(jù)
- 存儲(chǔ)一個(gè)點(diǎn):
POST city/_doc
{
"location": {
"type": "point",
"coordinates": [122.392496,31.245827]
}
}
POST /city/_doc
{
"location": "POINT (-77.03653 38.897676)"
}
- 存儲(chǔ)一個(gè)多邊形
POST /city/_doc
{
"location": {
"type": "polygon", // 存儲(chǔ)的圖形類型為: polygon,表示一個(gè)多邊形
"coordinates": [ // 支持多個(gè)多邊形
[ // 第一個(gè)多邊形,多邊形由下面的坐標(biāo)數(shù)組組成。
[100, 0], // 第一個(gè)坐標(biāo)點(diǎn),坐標(biāo)格式: [經(jīng)度, 緯度]
[101, 0],
[101, 1],
[100, 1],
[100, 0] // 最后一個(gè)坐標(biāo)點(diǎn),要跟第一個(gè)坐標(biāo)點(diǎn)相同,這樣多邊形才能形成閉合
]
]
}
}
POST /city/_doc
{
"location": "POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0))"
}
地理查詢
{
"query": {
"bool": {
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "circle",
"radius": "10km",
"coordinates": [
121.392496,
31.3
]
}
}
}
}
}
}
}
talk is cheap,show me the code:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-427843.html
代碼示例
點(diǎn):
PointBuilder pointBuilder = new PointBuilder(lng, lat);
GeoShapeQueryBuilder shapeQueryCirleBuilder = QueryBuilders.geoShapeQuery("location", pointBuilder).relation(ShapeRelation.CONTAINS);
圓:
CircleBuilder circleBuilder = new CircleBuilder();
circleBuilder.center(lng, lat);
DistanceUnit.Distance distance = new DistanceUnit.Distance(range, DistanceUnit.METERS);
circleBuilder.radius(distance);
GeoShapeQueryBuilder shapeQueryBuilder = QueryBuilders.geoShapeQuery("location", circleBuilder).relation(ShapeRelation.CONTAINS);
多邊形:
CoordinatesBuilder coordinatesBuilder = new CoordinatesBuilder();
List<Location> boundaryPoints = locationForm.getBoundaryPoints();
for (Location location : boundaryPoints) {
coordinatesBuilder.coordinate(location.getLng().doubleValue(), location.getLat().doubleValue());
}
PolygonBuilder polygonBuilder = new PolygonBuilder(coordinatesBuilder);
GeoShapeQueryBuilder shapeQueryBuilder = QueryBuilders.geoShapeQuery("location", polygonBuilder).relation(ShapeRelation.CONTAINS);
到了這里,關(guān)于elasticsearch之地理位置查詢geo_shape的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!