.within( 100, Unit.KM ) .ofLatitude( centerLatitude ) .andLongitude( centerLongitude ) .createQuery();
.within( distance, Unit.KM ) .ofLatitude( startLat ) .andLongitude( startLon ) .createQuery();
@Test public void testDistanceSort() throws Exception { double centerLatitude = 24.0d; double centerLongitude = 32.0d; final QueryBuilder builder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity( POI.class ).get(); org.apache.lucene.search.Query luceneQuery = builder.spatial().onField( "location" ) .within( 100, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); FullTextQuery hibQuery = fullTextSession.createFullTextQuery( luceneQuery, POI.class ); Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" ) ); hibQuery.setSort( distanceSort ); hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE ); hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" ); List<Object[]> results = hibQuery.list(); Double previousDistance = (Double) results.get( 0 )[1]; for ( int i = 1; i < results.size(); i++ ) { Object[] projectionEntry = results.get( i ); Double currentDistance = (Double) projectionEntry[1]; assertTrue( previousDistance + " should be < " + currentDistance, previousDistance < currentDistance ); previousDistance = currentDistance; } }
.andLongitude( centerLongitude ).createQuery();
.within( LARGE_DISTANCE_KM, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery();
.within( 5000, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); .within( 5000, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery();
@Test public void testSpatialAnnotationWithSubAnnotationsLevelRangeMode() throws Exception { //Point center = Point.fromDegrees( 24, 31.5 ); // 50.79 km fromBoundingCircle 24.32 double centerLatitude = 24; double centerLongitude = 31.5; final QueryBuilder builder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity( UserRange.class ).get(); org.apache.lucene.search.Query luceneQuery = builder.spatial() .within( 50, Unit.KM ) .ofLatitude( centerLatitude ) .andLongitude( centerLongitude ) .createQuery(); org.hibernate.query.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, UserRange.class ); List results = hibQuery.list(); Assert.assertEquals( 0, results.size() ); org.apache.lucene.search.Query luceneQuery2 = builder.spatial() .within( 51, Unit.KM ) .ofLatitude( centerLatitude ) .andLongitude( centerLongitude ) .createQuery(); org.hibernate.query.Query hibQuery2 = fullTextSession.createFullTextQuery( luceneQuery2, UserRange.class ); List results2 = hibQuery2.list(); Assert.assertEquals( 1, results2.size() ); }
@Test public void testSpatialHashQueries() { final QueryBuilder builder = helper.queryBuilder( POIHash.class ); Coordinates coordinates = Point.fromDegrees( 24d, 31.5d ); Query query = builder .spatial() .onField( "location" ) .within( 51, Unit.KM ) .ofCoordinates( coordinates ) .createQuery(); helper.assertThat( query ).from( POIHash.class ) .matchesExactlyIds( 2 ); query = builder .spatial() .onField( "location" ) .within( 500, Unit.KM ) .ofLatitude( 48.858333d ).andLongitude( 2.294444d ) .createQuery(); helper.assertThat( query ).from( POIHash.class ) .matchesExactlyIds( 1 ); }
@Test public void testSpatialRangeQueries() { final QueryBuilder builder = helper.queryBuilder( POI.class ); Coordinates coordinates = Point.fromDegrees( 24d, 31.5d ); Query query = builder .spatial() .onField( "location" ) .within( 51, Unit.KM ) .ofCoordinates( coordinates ) .createQuery(); helper.assertThat( query ).from( POI.class ) .matchesExactlyIds( 2 ); query = builder .spatial() .onField( "location" ) .within( 500, Unit.KM ) .ofLatitude( 48.858333d ).andLongitude( 2.294444d ) .createQuery(); helper.assertThat( query ).from( POI.class ) .matchesExactlyIds( 1 ); }
@Test public void testSpatialLatLongOnGetters() throws Exception { //Point center = Point.fromDegrees( 24, 31.5 ); // 50.79 km fromBoundingCircle 24.32 double centerLatitude = 24; double centerLongitude = 31.5; final QueryBuilder builder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity( GetterUser.class ).get(); org.apache.lucene.search.Query luceneQuery = builder.spatial().onField( "home" ) .within( 50, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, GetterUser.class ); List results = hibQuery.list(); Assert.assertEquals( 0, results.size() ); org.apache.lucene.search.Query luceneQuery2 = builder.spatial().onField( "home" ) .within( 51, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery2 = fullTextSession.createFullTextQuery( luceneQuery2, GetterUser.class ); List results2 = hibQuery2.list(); Assert.assertEquals( 1, results2.size() ); }
@Test public void testSpatialAnnotationOnClassLevel() throws Exception { //Point center = Point.fromDegrees( 24, 31.5 ); // 50.79 km fromBoundingCircle 24.32 double centerLatitude = 24; double centerLongitude = 31.5; final QueryBuilder builder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity( Hotel.class ).get(); org.apache.lucene.search.Query luceneQuery = builder.spatial().onField( "hotel_location" ) .within( 50, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, Hotel.class ); List results = hibQuery.list(); Assert.assertEquals( 0, results.size() ); org.apache.lucene.search.Query luceneQuery2 = builder.spatial().onField( "hotel_location" ) .within( 51, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery2 = fullTextSession.createFullTextQuery( luceneQuery2, Hotel.class ); List results2 = hibQuery2.list(); Assert.assertEquals( 1, results2.size() ); }
@Test public void testSpatialsAnnotation() throws Exception { final QueryBuilder builder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity( UserEx.class ).get(); org.apache.lucene.search.Query luceneQuery = builder.spatial() .within( 100.0d, Unit.KM ) .ofLatitude( 24.0d ) .andLongitude( 31.5d ) .createQuery(); org.hibernate.query.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, UserEx.class ); List results = hibQuery.list(); Assert.assertEquals( 1, results.size() ); org.apache.lucene.search.Query luceneQuery2 = builder.spatial().onField( "work" ) .within( 100.0d, Unit.KM ).ofLatitude( 12.0d ).andLongitude( 27.5d ).createQuery(); org.hibernate.query.Query hibQuery2 = fullTextSession.createFullTextQuery( luceneQuery2, UserEx.class ); List results2 = hibQuery2.list(); Assert.assertEquals( 1, results2.size() ); }
@Test @TestForIssue(jiraKey = "HSEARCH-1470") public void testSpatialQueryOnWrongFieldThrowsException() throws Exception { final QueryBuilder builder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity( POI.class ).get(); try { builder.spatial() .onField( "foo" ) .within( 1, Unit.KM ) .ofLatitude( 0d ) .andLongitude( 0d ) .createQuery(); fail( "Building an invalid spatial query should fail" ); } catch (SearchException e) { assertTrue( "Wrong error message " + e.getMessage(), e.getMessage().startsWith( "HSEARCH000131" ) ); } }
public void testSpatialQueries() { loadData(); double centerLatitude = 24; double centerLongitude = 31.5; Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(CitySpatial.class).get().spatial() .onField("city_location") .within(50, Unit.KM).ofLatitude(centerLatitude).andLongitude(centerLongitude).createQuery(); CacheQuery<?> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<?> found = cacheQuery.list(); assertEquals(0, found.size()); query = Search.getSearchManager(cache).buildQueryBuilderForClass(CitySpatial.class).get().spatial() .onField("city_location") .within(51, Unit.KM).ofLatitude(centerLatitude).andLongitude(centerLongitude).createQuery(); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assertEquals(1, found.size()); }
@Test @TestForIssue(jiraKey = "HSEARCH-1470") public void testSpatialQueryOnNonSpatialConfiguredEntityThrowsException() throws Exception { final QueryBuilder builder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity( MissingSpatialPOI.class ).get(); try { builder.spatial() .within( 1, Unit.KM ) .ofLatitude( 0d ) .andLongitude( 0d ) .createQuery(); fail( "Building an invalid spatial query should fail" ); } catch (SearchException e) { assertTrue( "Wrong error message: " + e.getMessage(), e.getMessage().startsWith( "HSEARCH000131" ) ); } }
@Test public void testSpatialAnnotationOnEmbeddableFieldLevel() throws Exception { final QueryBuilder builder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity( Restaurant.class ).get(); double centerLatitude = 24; double centerLongitude = 31.5; org.apache.lucene.search.Query luceneQuery = builder.spatial().onField( "position.location" ) .within( 50, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, Restaurant.class ); List results = hibQuery.list(); Assert.assertEquals( 0, results.size() ); org.apache.lucene.search.Query luceneQuery2 = builder.spatial().onField( "position.location" ) .within( 51, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery2 = fullTextSession.createFullTextQuery( luceneQuery2, Restaurant.class ); List results2 = hibQuery2.list(); Assert.assertEquals( 1, results2.size() ); }
@Test public void testSpatialAnnotationOnFieldLevel() throws Exception { //Point center = Point.fromDegrees( 24, 31.5 ); // 50.79 km fromBoundingCircle 24.32 double centerLatitude = 24; double centerLongitude = 31.5; final QueryBuilder builder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity( Event.class ).get(); org.apache.lucene.search.Query luceneQuery = builder.spatial().onField( "location" ) .within( 50, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, Event.class ); List results = hibQuery.list(); Assert.assertEquals( 0, results.size() ); org.apache.lucene.search.Query luceneQuery2 = builder.spatial().onField( "location" ) .within( 51, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery2 = fullTextSession.createFullTextQuery( luceneQuery2, Event.class ); List results2 = hibQuery2.list(); Assert.assertEquals( 1, results2.size() ); }
@Test public void testSpatialAnnotationWithSubAnnotationsLevel() throws Exception { //Point center = Point.fromDegrees( 24, 31.5 ); // 50.79 km fromBoundingCircle 24.32 double centerLatitude = 24; double centerLongitude = 31.5; final QueryBuilder builder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity( User.class ).get(); org.apache.lucene.search.Query luceneQuery = builder.spatial().onField( "home" ) .within( 50, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, User.class ); List results = hibQuery.list(); Assert.assertEquals( 0, results.size() ); org.apache.lucene.search.Query luceneQuery2 = builder.spatial().onField( "home" ) .within( 51, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery2 = fullTextSession.createFullTextQuery( luceneQuery2, User.class ); List results2 = hibQuery2.list(); Assert.assertEquals( 1, results2.size() ); }
@Test public void testSpatialAnnotationOnFieldLevelRangeMode() throws Exception { final QueryBuilder builder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity( RangeEvent.class ).get(); double centerLatitude = 24; double centerLongitude = 31.5; org.apache.lucene.search.Query luceneQuery = builder.spatial().onField( "location" ) .within( 50, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, RangeEvent.class ); List results = hibQuery.list(); Assert.assertEquals( 0, results.size() ); org.apache.lucene.search.Query luceneQuery2 = builder.spatial().onField( "location" ) .within( 51, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery2 = fullTextSession.createFullTextQuery( luceneQuery2, RangeEvent.class ); List results2 = hibQuery2.list(); Assert.assertEquals( 1, results2.size() ); }
private void assertNumberOfPointsOfInterestWithinRadius(double centerLatitude, double centerLongitude, double radius, int expectedPoiCount) { final QueryBuilder builder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity( POI.class ).get(); org.apache.lucene.search.Query luceneQuery = builder.spatial().onField( "location" ) .within( radius, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); org.hibernate.query.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, POI.class ); List results = hibQuery.list(); Assert.assertEquals( "Unexpected number of POIs within radius", expectedPoiCount, results.size() ); } }