public void testDistanceSortWithMaxResult() throws Exception {
POI poi = new POI( 1, "Distance to 24,32 : 0", 24.0d, 32.0d, "" );
POI poi2 = new POI( 2, "Distance to 24,32 : 10.16", 24.0d, 31.9d, "" );
POI poi3 = new POI( 3, "Distance to 24,32 : 11.12", 23.9d, 32.0d, "" );
POI poi4 = new POI( 4, "Distance to 24,32 : 15.06", 23.9d, 32.1d, "" );
POI poi5 = new POI( 5, "Distance to 24,32 : 22.24", 24.2d, 32.0d, "" );
POI poi6 = new POI( 6, "Distance to 24,32 : 24.45", 24.2d, 31.9d, "" );
FullTextEntityManager em = Search.getFullTextEntityManager( factory.createEntityManager() );
em.getTransaction().begin();
em.persist( poi );
em.persist( poi2 );
em.persist( poi3 );
em.getTransaction().commit();
em.getTransaction().begin();
em.persist( poi4 );
em.persist( poi5 );
em.persist( poi6 );
em.getTransaction().commit();
em.getTransaction().begin();
double centerLatitude = 24.0d;
double centerLongitude = 32.0d;
final QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity( POI.class ).get();
org.apache.lucene.search.Query luceneQuery = builder.spatial().onCoordinates( "location" )
.within( 100, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery();
FullTextQuery hibQuery = em.createFullTextQuery( luceneQuery, POI.class );
Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" ) );
hibQuery.setSort( distanceSort );
hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE );
hibQuery.setMaxResults(3);
hibQuery.setSpatialParameters( Point.fromDegrees( centerLatitude, centerLongitude ), "location" );
List results = hibQuery.getResultList();
Object[] firstResult = (Object[]) results.get( 0 );
Object[] secondResult = (Object[]) results.get( 1 );
Object[] thirdResult = (Object[]) results.get( 2 );
Assert.assertEquals( (Double) firstResult[1], 0.0, 0.0001 );
Assert.assertEquals( (Double) secondResult[1], 10.1582, 0.0001 );
Assert.assertEquals( (Double) thirdResult[1], 11.1195, 0.0001 );
List<?> pois = em.createQuery( "from " + POI.class.getName() ).getResultList();
for ( Object entity : pois ) {
em.remove( entity );
}
em.getTransaction().commit();
em.close();
}