Hibernate.orgCommunity Documentation

Chapter 9. Spatial

9.1. Enable indexing of Spatial Coordinates
9.1.1. Indexing coordinates for range queries
9.1.2. Indexing coordinates in a grid with spatial hashes
9.1.3. Implementing the Coordinates interface
9.2. Performing Spatial Queries
9.2.1. Returning distance to query point in the search results
9.3. Multiple Coordinate pairs
9.4. Insight: implementation details of spatial hashes indexing
9.4.1. At indexing level
9.4.2. At search level

With the spatial extensions you can combine fulltext queries with distance restrictions, filter results based on distances or sort results on such a distance criteria.

The spatial support of Hibernate Search has the following goals:

For example, you might search for restaurants somewhere in a 2 km radius around your office.

In order to use the spatial extensions for an indexed entity, you need to add the @Spatial annotation (org.hibernate.search.annotations.Spatial) and specify one or more sets of coordinates.

There are different techniques to index point coordinates. Hibernate Search Spatial offers a choice between two strategies:

  • index as numbers
  • index as labeled spatial hashes

We will now describe both methods, so you can make a suitable choice. You can pick a different strategy for each set of coordinates. The strategy is selected by specifying the spatialMode attribute of the @Spatial annotation.

Instead of using the @Latitude and @Longitue annotations you can choose to implement the org.hibernate.search.spatial.Coordinates interface.


As we will see in the section Section 9.3, “Multiple Coordinate pairs”, an entity can have multiple @Spatial annotations; when having the entity implement Coordinates, the implemented methods refer to the default @Spatial annotation with the default pair of coordinates.

Tip

The default (field) name in case @Spatial is placed on the entity level is org.hibernate.search.annotations.Spatial.COORDINATES_DEFAULT_FIELD.

An alternative is to use properties implementing the Coordinates interface; this way you can have multiple Spatial instances:


When using this form the @Spatial.name automatically defaults to the property name. In the above case to location.

You can use the Hibernate Search query DSL to build a query to search around a pair of coordinates (latitude, longitude) or around a bean implementing the Coordinates interface.

As with any fulltext query, the spatial query creation flow looks like:

  1. retrieve a QueryBuilder from the SearchFactory
  2. use the DSL to build a spatial query, defining search center and radius
  3. optionally combine the resulting Query with other filters
  4. call the createFullTextQuery() and use the resulting query like any standard Hibernate or JPA query

A fully working example can be found in the test-suite of the source code. Refer to SpatialIndexingTest.testSpatialAnnotationOnClassLevel() and its corresponding Hotel test class.

Alternatively to passing separate latitude and longitude values, you can also pass an instance implementing the Coordinates interface:


To retrieve the actual distance values you need to use projection (see Section 5.1.3.5, “Projection”):


  • Use FullTextQuery.setProjection with FullTextQuery.SPATIAL_DISTANCE as one of the projected fields.
  • Call FullTextQuery.setSpatialParameters with the latitude, longitude and the name of the spatial field used to build the spatial query. Note that using coordinates different than the center used for the query will have unexpected results.

Tip

The default (field) name in case @Spatial is placed on the entity level is org.hibernate.search.annotations.Spatial.COORDINATES_DEFAULT_FIELD.

Distance projection and null values

Using distance projection on non @Spatial enabled entities and/or with a non spatial Query will have unexpected results as entities not spatially indexed and/or having null values for latitude or longitude will be considered to be at (0,0)/(lat,0)/(0,long).

Using distance projection with a spatial query on spatially indexed entities having, eventually, null values for latitude and/or longitude is safe as they will not be found by the spatial query and won’t have distance calculated.

To sort the results by distance to the center of the search you will have to build a Sort instance using a DistanceSortField:


The DistanceSortField must be constructed using the same coordinates on the same spatial field used to build the spatial query otherwise the sorting will occur with another center than the query. This repetition is needed to allow you to define Queries with any tool.

Sorting and null values

Using distance sort on non @Spatial enabled entities and/or with a non spatial Query will have also unexpected results as entities non spatially indexed and/or with null values for latitude or longitude will be considered to be at (0,0)/(lat,0)/(0,long)

Using distance sort with a spatial query on spatially indexed entities having, potentially, null values for latitude and/or longitude is safe as they will not be found by the spatial query and so won’t be sorted

You can associate multiple pairs of coordinates to the same entity, as long as each pair is uniquely identified by using a different name. This is achieved by stacking multiple @Spatial annotations within a single @Spatials annotation and specifying the name attribute on the individual @Spatial annotations.


To target an alternative pair of coordinates at query time, we need to specify the pair by name using onField(String):


The following chapter is meant to provide a technical insight in spatial hash (grid) indexing. It discusses how coordinates are mapped to the index and how queries are implemented.

When Hibernate Search indexes an entity annotated with @Spatial, it instantiates a SpatialFieldBridge to transform the latitude and longitude fields accessed via the Coordinates interface to the multiple index fields stored in the Lucene index.

Principle of the spatial index: the spatial index used in Hibernate Search is a grid based spatial index where grid ids are hashes derived from latitude and longitude.

To make computations easier the latitude and longitude field values will be projected into a flat coordinate system with the help of a sinusoidal projection. Origin value space is :

[-90 → +90],]-180 →; 180]

for latitude,longitude coordinates and projected space is:

]-pi → +pi],[-pi/2 → +pi/2]

for Cartesian x,y coordinates (beware of fields order inversion: x is longitude and y is latitude).

The index is divided into n levels labeled from 0 to n-1.

At the level 0 the projected space is the whole Earth. At the level 1 the projected space is divided into 4 rectangles (called boxes as in bounding box):

[-pi,-pi/2]→[0,0], [-pi,0]→[0,+pi/2], [0,-pi/2]→[+pi,0] and [0,0]→[+pi,+pi/2]

At level n+1 each box of level n is divided into 4 new boxes and so on. The numbers of boxes at a given level is 4^n.

Each box is given an id, in this format: [Box index on the X axis]|[Box index on the Y axis]. To calculate the index of a box on an axis we divide the axis range in 2^n slots and find the slot the box belongs to. At the n level the indexes on an axis are from -(2^n)/2 to (2^n)/2. For instance, the 5th level has 4^5 = 1024 boxes with 32 indexes on each axis (32x32 is 1024) and the box of Id "0|8" is covering the [0,8/32*pi/2]→[1/32*pi,9/32*pi/2] rectangle is projected space.

Beware! The boxes are rectangles in projected space but the related area on Earth is not rectangular!

Now that we have all these boxes at all these levels, we index points "into" them.

For a point (lat,long) we calculate its projection (x,y) and then we calculate for each level of the spatial index, the ids of the boxes it belongs to.

At each level the point is in one and only one box. For points on the edges the box are considered exclusive n the left side and inclusive on the right i-e ]start,end] (the points are normalized before projection to [-90,+90],]-180,+180]).

We store in the Lucene document corresponding to the entity to index one field for each level of the spatial hash grid. The field is named: HSSI[n]. [spatial index fields name] is given either by the parameter at class level annotation or derived from the name of the spatial annotated method of the entity, HSSI stands for Hibernate Search Spatial Index and n is the level of the spatial hashes grid.

We also store the latitude and longitude as a numeric field under [spatial index fields name]_HSSI_Latitude and [spatial index fields name]_HSSI_Longitude fields. They will be used to filter precisely results by distance in the second stage of the search.

Now that we have all these fields, what are they used for?

When you ask for a spatial search by providing a search discus (center+radius) we will calculate the box ids that do cover the search discus in the projected space, fetch all the documents that belong to these boxes (thus narrowing the number of documents for which we will have to calculate distance to the center) and then filter this subset with a real distance calculation. This is called two level spatial filtering.