Geo queries

Elasticsearch supports two types of geo data: geo_point fields which support lat/lon pairs, and geo_shape fields, which support points, lines, circles, polygons, multi-polygons etc.

The queries in this group are:

geo_shape query
Find document with geo-shapes which either intersect, are contained by, or do not intersect with the specified geo-shape.
geo_bounding_box query
Finds documents with geo-points that fall into the specified rectangle.
geo_distance query
Finds document with geo-points within the specified distance of a central point.
geo_distance_range query
Like the geo_point query, but the range starts at a specified distance from the central point.
geo_polygon query
Find documents with geo-points within the specified polygon.
geohash_cell query
Find geo-points whose geohash intersects with the geohash of the specified point.

GeoShape Query

See Geo Shape Query

Note: the geo_shape type uses Spatial4J and JTS, both of which are optional dependencies. Consequently you must add Spatial4J and JTS to your classpath in order to use this type:

<dependency>
    <groupId>com.spatial4j</groupId>
    <artifactId>spatial4j</artifactId>
    <version>0.4.1</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>

check for updates in Maven Central

check for updates in Maven Central

// Import ShapeRelation and ShapeBuilder
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
QueryBuilder qb = geoShapeQuery(
    "pin.location",                     
    ShapeBuilder.newMultiPoint()        
        .point(0, 0)
        .point(0, 10)
        .point(10, 10)
        .point(10, 0)
        .point(0, 0),
    ShapeRelation.WITHIN);              

field

shape

relation can be ShapeRelation.WITHIN, ShapeRelation.INTERSECTS or ShapeRelation.DISJOINT

// Using pre-indexed shapes
QueryBuilder qb = geoShapeQuery(
        "pin.location",             
        "DEU",                      
        "countries",                
        ShapeRelation.WITHIN)       
    .indexedShapeIndex("shapes")    
    .indexedShapePath("location");  

field

The ID of the document that containing the pre-indexed shape.

Index type where the pre-indexed shape is.

relation

Name of the index where the pre-indexed shape is. Defaults to shapes.

The field specified as path containing the pre-indexed shape. Defaults to shape.

Geo Bounding Box Query

See Geo Bounding Box Query

QueryBuilder qb = geoBoundingBoxQuery("pin.location") 
    .topLeft(40.73, -74.1)                            
    .bottomRight(40.717, -73.99);                     

field

bounding box top left point

bounding box bottom right point

Geo Distance Query

See Geo Distance Query

QueryBuilder qb = geoDistanceQuery("pin.location")  
    .point(40, -70)                                 
    .distance(200, DistanceUnit.KILOMETERS)         
    .optimizeBbox("memory")                         
    .geoDistance(GeoDistance.ARC);                  

field

center point

distance from center point

optimize bounding box: memory, indexed or none

distance computation mode: GeoDistance.SLOPPY_ARC (default), GeoDistance.ARC (slightly more precise but significantly slower) or GeoDistance.PLANE (faster, but inaccurate on long distances and close to the poles)

Geo Distance Range Query

See Geo Distance Range Query

QueryBuilder qb = geoDistanceRangeQuery("pin.location")         
    .point(40, -70)                                             
    .from("200km")                                              
    .to("400km")                                                
    .includeLower(true)                                         
    .includeUpper(false)                                        
    .optimizeBbox("memory")                                     
    .geoDistance(GeoDistance.ARC);                              

field

center point

starting distance from center point

ending distance from center point

include lower value means that from is gt when false or gte when true

include upper value means that to is lt when false or lte when true

optimize bounding box: memory, indexed or none

distance computation mode: GeoDistance.SLOPPY_ARC (default), GeoDistance.ARC (slightly more precise but significantly slower) or GeoDistance.PLANE (faster, but inaccurate on long distances and close to the poles)

Geo Polygon Query

See Geo Polygon Query

QueryBuilder qb = geoPolygonQuery("pin.location")       
    .addPoint(40, -70)                                  
    .addPoint(30, -80)                                  
    .addPoint(20, -90);                                 

field

add your polygon of points a document should fall within

Geohash Cell Query

See Geohash Cell Query

QueryBuilder qb = geoHashCellQuery("pin.location",  
            new GeoPoint(13.4080, 52.5186))         
        .neighbors(true)                            
        .precision(3);                              

field

point. Can also be a hash like u30

The neighbors option of the filter offers the possibility to filter cells next to the given cell.

precision level