- Dependency
- Client
- Client - Transport Client
- Client - XPack Transport Client
- Document APIs
- Document APIs - Index API
- Document APIs - Get API
- Document APIs - Delete API
- Document APIs - Delete By Query API
- Document APIs - Update API
- Document APIs - Multi Get API
- Document APIs - Bulk API
- Document APIs - Using Bulk Processor
- Search API
- Search API - Using scrolls in Java
- Search API - MultiSearch API
- Search API - Using Aggregations
- Search API - Terminate After
- Search API - Search Template
- Aggregations
- Aggregations - Structuring aggregations
- Aggregations - Metrics aggregations
- Aggregations - Bucket aggregations
- Query DSL
- Query DSL - Match All Query
- Query DSL - Full text queries
- Query DSL - Term level queries
- Query DSL - Compound queries
- Query DSL - Joining queries
- Query DSL - Geo queries
- Query DSL - Specialized queries
- Query DSL - Span queries
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
Query DSL - Geo queries
Geo queries 地理位置查询
Elasticsearch支持两种类型的地理数据:geo_point类型支持成对的纬度/经度,geo_shape类型支持点、线、圆、多边形、多个多边形等。
在这组的查询中:
- geo_shape查询
查找要么相交,包含的,要么指定形状不相交的地理形状的文档。
geo_shape
类型使用Spatial4J
和JTS
,这两者都是可选的依赖项。 因此,必须将Spatial4J
和JTS
添加到classpath
中才能使用此类型:
<dependency>
<groupId>org.locationtech.spatial4j</groupId>
<artifactId>spatial4j</artifactId>
<version>0.6</version>
</dependency>
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>1.13</version>
<exclusions>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
</exclusions>
</dependency>
// Import ShapeRelation and ShapeBuilder
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
List<Coordinate> points = new ArrayList<>();
points.add(new Coordinate(0, 0));
points.add(new Coordinate(0, 10));
points.add(new Coordinate(10, 10));
points.add(new Coordinate(10, 0));
points.add(new Coordinate(0, 0));
QueryBuilder qb = geoShapeQuery(
"pin.location", //field
ShapeBuilders.newMultiPoint(points) //shape
.relation(ShapeRelation.WITHIN); //relation 可以是 ShapeRelation.CONTAINS, ShapeRelation.WITHIN, ShapeRelation.INTERSECTS 或 ShapeRelation.DISJOINT
// Using pre-indexed shapes
QueryBuilder qb = geoShapeQuery(
"pin.location", //field
"DEU", //The ID of the document that containing the pre-indexed shape.
"countries") //Index type where the pre-indexed shape is.
.relation(ShapeRelation.WITHIN)) //relation
.indexedShapeIndex("shapes") //Name of the index where the pre-indexed shape is. Defaults to shapes.
.indexedShapePath("location"); //The field specified as path containing the pre-indexed shape. Defaults to shape.
- geo_bounding_box 查询
查找落入指定的矩形地理点的文档。
QueryBuilder qb = geoBoundingBoxQuery("pin.location") //field
.setCorners(40.73, -74.1, //bounding box top left point
40.717, -73.99); //bounding box bottom right point
- geo_distance 查询
查找在一个中心点指定范围内的地理点文档。
QueryBuilder qb = geoDistanceQuery("pin.location") //field
.point(40, -70) //center point
.distance(200, DistanceUnit.KILOMETERS); //distance from center point
- geo_polygon 查询
查找指定多边形内地理点的文档。
List<GeoPoint> points = new ArrayList<>(); //add your polygon of points a document should fall within
points.add(new GeoPoint(40, -70));
points.add(new GeoPoint(30, -80));
points.add(new GeoPoint(20, -90));
QueryBuilder qb =
geoPolygonQuery("pin.location", points); //initialise the query with field and points
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论