概念
geo_shape则表示有多个点连成线组成的形状,实际开发中,如果我们的地理坐标是一个地理形状,则可以使用地理形状数据类型进行插入、查询文档。比如说学校、大商场这种面积比较大的地理坐标,都需要geo_shape来表示。
geo_shape支持存储的常用形状数据如下:
- 点(point)
- 圆形(circle)
- 矩形(envelope)
- 多边形 (polygon)
geo_shape支持的图形搜索类型:
- intersects - 查询的形状与索引的形状有重叠(默认), 即图形有交集则匹配。
- disjoint - 查询的形状与索引的形状完全不重叠。
- within - 查询的形状包含索引的形状。
geo_shape支持GeoJSON及WKT中描述的大多数地理形状:
GeoJson:
{
"type": "Point",
"coordinates": [125.6, 10.1]
}
WKT:文章来源:https://www.toymoban.com/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))
创建索引mapping
PUT /city
{
"mappings": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
添加数据
- 存储一个点:
POST city/_doc
{
"location": {
"type": "point",
"coordinates": [122.392496,31.245827]
}
}
POST /city/_doc
{
"location": "POINT (-77.03653 38.897676)"
}
- 存储一个多边形
POST /city/_doc
{
"location": {
"type": "polygon", // 存储的图形类型为: polygon,表示一个多边形
"coordinates": [ // 支持多个多边形
[ // 第一个多边形,多边形由下面的坐标数组组成。
[100, 0], // 第一个坐标点,坐标格式: [经度, 纬度]
[101, 0],
[101, 1],
[100, 1],
[100, 0] // 最后一个坐标点,要跟第一个坐标点相同,这样多边形才能形成闭合
]
]
}
}
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:文章来源地址https://www.toymoban.com/news/detail-427843.html
代码示例
点:
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);
到了这里,关于elasticsearch之地理位置查询geo_shape的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!