地理查询编辑
Elasticsearch支持两种类型的地理数据: geo_point
纬度/经度对字段的支持,和 geo_shape
领域,支持点、线、圆、多边形、多等。
这组查询:
- 发现文档与几何图型相交,包含,或与指定的geo-shape不相交。
- 发现文档与geo-points落入指定的矩形。
- 发现文档geo-points内指定的中心点的距离。
-
就像
geo_point
查询,但是范围是从一个指定的中心点的距离。 - 发现文档geo-points内指定的多边形。
- 找到的geo-points geohash相交的geohash指定点。
geo_shape
查询
geo_bounding_box
查询
geo_distance
查询
geo_distance_range
查询
geo_polygon
查询
geohash_cell
查询
GeoShape查询编辑
看到Geo形状查询
注意: geo_shape
类型使用 Spatial4J
和 JTS
,这两个都是可选的依赖性。因此您必须添加 Spatial4J
和JTS
到类路径中为了使用这种类型:
<dependency>
<groupId>com.spatial4j</groupId>
<artifactId>spatial4j</artifactId>
<version>0.4.1</version> <!--1-->
</dependency>
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>1.13</version> <!--2-->
<exclusions>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
</exclusions>
</dependency>
检查更新Maven中央 | |
检查更新Maven中央 |
// Import ShapeRelation and ShapeBuilder
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
QueryBuilder qb = geoShapeQuery(
"pin.location", //1
ShapeBuilder.newMultiPoint() //2
.point(0, 0)
.point(0, 10)
.point(10, 10)
.point(10, 0)
.point(0, 0),
ShapeRelation.WITHIN); //3
场 | |
形状 | |
关系可以 |
// Using pre-indexed shapes
QueryBuilder qb = geoShapeQuery(
"pin.location", //1
"DEU", //2
"countries", //3
ShapeRelation.WITHIN) //4
.indexedShapeIndex("shapes") //5
.indexedShapePath("location"); //6
场 | |
文档的ID包含预先索引的形状。 | |
索引类型预先索引形状在哪里。 | |
关系 | |
预先索引的索引的名称,形状。默认为 形状. | |
包含预先索引的字段指定为路径的形状。默认为 形状. |
地理边界框查询编辑
看到地理边界框查询
QueryBuilder qb = geoBoundingBoxQuery("pin.location") //1
.topLeft(40.73, -74.1) //2
.bottomRight(40.717, -73.99); //3
QueryBuilder qb = geoDistanceQuery("pin.location") //1
.point(40, -70) //2
.distance(200, DistanceUnit.KILOMETERS) //3
.optimizeBbox("memory") //4
.geoDistance(GeoDistance.ARC); //5
QueryBuilder qb = geoDistanceRangeQuery("pin.location") //1
.point(40, -70) //2
.from("200km") //3
.to("400km") //4
.includeLower(true) //5
.includeUpper(false) //6
.optimizeBbox("memory") //7
.geoDistance(GeoDistance.ARC); //8
QueryBuilder qb = geoPolygonQuery("pin.location") //1
.addPoint(40, -70) //2
.addPoint(30, -80) //3
.addPoint(20, -90); //4
Geohash细胞查询编辑
QueryBuilder qb = geoHashCellQuery("pin.location", //1
new GeoPoint(13.4080, 52.5186)) //2
.neighbors(true) //3
.precision(3); //4
场 | |
点。也可以是一个散列 | |
的 | |
精度水平 |