Geo queriesedit

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_polygon query
Find documents with geo-points within the specified polygon.

GeoShape Queryedit

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>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>

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;
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",                         
        ShapeBuilders.newMultiPoint(points)     
        .relation(ShapeRelation.WITHIN);        

field

shape

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

// Using pre-indexed shapes
QueryBuilder qb = geoShapeQuery(
        "pin.location",                  
        "DEU",                           
        "countries")                     
        .relation(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 Queryedit

See Geo Bounding Box Query

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

field

bounding box top left point

bounding box bottom right point

Geo Distance Queryedit

See Geo Distance Query

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

field

center point

distance from center point

Geo Polygon Queryedit

See Geo Polygon Query

List<GeoPoint> points = new ArrayList<>();             
points.add(new GeoPoint(40, -70));
points.add(new GeoPoint(30, -80));
points.add(new GeoPoint(20, -90));

QueryBuilder qb =
        geoPolygonQuery("pin.location", points);       

add your polygon of points a document should fall within

initialise the query with field and points