.within( 50, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); .within( 51, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); .within( 50, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); .within( 51, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery();
.within( 100, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); .within( 100, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery();
.within( 100.0d, Unit.KM ) .ofLatitude( 24.0d ) .andLongitude( 31.5d ) .createQuery(); .within( 100.0d, Unit.KM ) .ofLatitude( 24.0d ) .andLongitude( 31.5d ) .createQuery();
.within( 100, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery();
.within( 100, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery();
.within( 50, Unit.KM ) .ofLatitude( centerLatitude ) .andLongitude( centerLongitude ) .createQuery(); .within( 51, Unit.KM ) .ofLatitude( centerLatitude ) .andLongitude( centerLongitude ) .createQuery(); .within( 112, Unit.KM ) .ofLatitude( endOfTheWorldLatitude ) .andLongitude( endOfTheWorldLongitude ) .createQuery(); .within( 100000, Unit.KM ) .ofLatitude( endOfTheWorldLatitude ) .andLongitude( endOfTheWorldLongitude ) .createQuery();
.within( 1.8097233616663808, Unit.KM ) .ofLatitude( centerLatitude ) .andLongitude( centerLongitude ) .createQuery();
.andLongitude( centerLongitude ).createQuery();
.within( LARGE_DISTANCE_KM, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery();
.within( distance, Unit.KM ) .ofLatitude( startLat ) .andLongitude( startLon ) .createQuery();
.within( 5000, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); .within( 5000, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery();
.within( 100, 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 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; } }
@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" ) ); } }