Skip to content

Commit

Permalink
HSEARCH-1470 Improving error message for HSEARCH000131 and adding exp…
Browse files Browse the repository at this point in the history
…licit tests
  • Loading branch information
hferentschik committed Nov 7, 2014
1 parent 40c9e6d commit 938d389
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 14 deletions.
Expand Up @@ -420,7 +420,7 @@ public interface Log extends BasicLogger {
@Message(id = 128, value = "Interceptor enforces update of index data instead of index operation %2$s on instance of class %1$s")
void forceUpdateOnIndexOperationViaInterception(Class<?> entityClass, WorkType type);

@Message(id = 131, value = "The field used for the spatial query is not using SpatialFieldBridge: %1$s.%2$s")
@Message(id = 131, value = "The field '%1$s#%2$s' used for the spatial query is not configured as spatial field. Check the proper use of @Spatial respectively SpatialFieldBridge")
SearchException targetedFieldNotSpatial(String className, String fieldName);

@Message(id = 133, value = "@ClassBridge implementation '%1$s' should implement either org.hibernate.search.bridge.FieldBridge, org.hibernate.search.bridge.TwoWayStringBridge or org.hibernate.search.bridge.StringBridge")
Expand Down
@@ -0,0 +1,71 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.test.spatial;

import javax.persistence.Entity;
import javax.persistence.Id;

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Latitude;
import org.hibernate.search.annotations.Longitude;
import org.hibernate.search.annotations.Store;

/**
* @author Hardy Ferentschik
*/
@Entity
@Indexed
public class MissingSpatialPOI {
@Id
Integer id;

@Field(store = Store.YES)
String name;

@Field(store = Store.YES, index = Index.YES)
String type;

@Latitude
double latitude;

@Longitude
double longitude;

public MissingSpatialPOI(Integer id, String name, double latitude, double longitude, String type) {
this.id = id;
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
this.type = type;
}

public MissingSpatialPOI() {
}

public Integer getId() {
return id;
}

public String getName() {
return name;
}

public double getLatitude() {
return latitude;
}

public double getLongitude() {
return longitude;
}

public String getType() {
return type;
}

}
Expand Up @@ -20,13 +20,15 @@
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.exception.SearchException;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.hibernate.search.query.dsl.Unit;
import org.hibernate.search.spatial.DistanceSortField;
import org.hibernate.search.test.SearchTestBase;
import org.hibernate.search.testsupport.TestForIssue;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* Hibernate Search spatial : unit tests on indexing POIs in with Grid and Grid+Distance
Expand Down Expand Up @@ -59,6 +61,9 @@ public void createAndIndexTestData() throws Exception {
fullTextSession.save( new NonGeoPOI( 5, "Distance to 24,32 : 11.12", 23.9d, 32.0d, "" ) );
fullTextSession.save( new NonGeoPOI( 6, "Distance to 24,32 : 22.24", 24.2d, 32.0d, "" ) );

// MissingSpatialPOI
fullTextSession.save( new MissingSpatialPOI( 1, "Distance to 24,32 : 0", 24.0d, 32.0d, "" ) );

// Event
SimpleDateFormat dateFormat = new SimpleDateFormat( "d M yyyy" );
Date date = dateFormat.parse( "10 9 1976" );
Expand Down Expand Up @@ -129,12 +134,12 @@ public void testDistanceProjection() throws Exception {
Object[] fourthResult = (Object[]) results.get( 3 );
Object[] fifthResult = (Object[]) results.get( 4 );
Object[] sixthResult = (Object[]) results.get( 5 );
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 );
Assert.assertEquals( ((Double)fourthResult[1]), 15.0636, 0.0001 );
Assert.assertEquals( ((Double)fifthResult[1]), 22.239, 0.001 );
Assert.assertEquals( ((Double)sixthResult[1]), 24.446, 0.001 );
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 );
Assert.assertEquals( ( (Double) fourthResult[1] ), 15.0636, 0.0001 );
Assert.assertEquals( ( (Double) fifthResult[1] ), 22.239, 0.001 );
Assert.assertEquals( ( (Double) sixthResult[1] ), 24.446, 0.001 );
}

@Test
Expand All @@ -149,7 +154,7 @@ public void testDistanceSort() throws Exception {
.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" ));
Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" ) );
hibQuery.setSort( distanceSort );
hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE );
hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" );
Expand Down Expand Up @@ -177,7 +182,7 @@ public void testNonGeoDistanceSort() throws Exception {
org.apache.lucene.search.Query luceneQuery = builder.all().createQuery();

FullTextQuery hibQuery = fullTextSession.createFullTextQuery( luceneQuery, NonGeoPOI.class );
Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" ));
Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" ) );
hibQuery.setSort( distanceSort );
hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE );
hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" );
Expand All @@ -186,6 +191,46 @@ public void testNonGeoDistanceSort() throws Exception {
hibQuery.list();
}

@Test
@TestForIssue(jiraKey = "HSEARCH-1470")
public void testSpatialQueryOnNonSpatialConfiguredEntityThrowsException() throws Exception {
final QueryBuilder builder = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity( MissingSpatialPOI.class ).get();

try {
builder.spatial()
.onDefaultCoordinates()
.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
@TestForIssue(jiraKey = "HSEARCH-1470")
public void testSpatialQueryOnWrongFieldThrowsException() throws Exception {
final QueryBuilder builder = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity( POI.class ).get();

try {
builder.spatial()
.onCoordinates( "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" ) );
}
}

@Test
public void testSpatialAnnotationOnFieldLevel() throws Exception {
//Point center = Point.fromDegrees( 24, 31.5 ); // 50.79 km fromBoundingCircle 24.32
Expand Down Expand Up @@ -354,15 +399,23 @@ public void testSpatialAnnotationOnClassLevelRangeMode() throws Exception {
double endOfTheWorldLatitude = 0.0d;
double endOfTheWorldLongitude = 180.0d;

org.apache.lucene.search.Query luceneQuery3 = builder.spatial().onCoordinates( "location" )
.within( 112, Unit.KM ).ofLatitude( endOfTheWorldLatitude ).andLongitude( endOfTheWorldLongitude ).createQuery();
org.apache.lucene.search.Query luceneQuery3 = builder.spatial()
.onCoordinates( "location" )
.within( 112, Unit.KM )
.ofLatitude( endOfTheWorldLatitude )
.andLongitude( endOfTheWorldLongitude )
.createQuery();

org.hibernate.Query hibQuery3 = fullTextSession.createFullTextQuery( luceneQuery3, RangeHotel.class );
List results3 = hibQuery3.list();
Assert.assertEquals( 2, results3.size() );

org.apache.lucene.search.Query luceneQuery4 = builder.spatial().onCoordinates( "location" )
.within( 100000, Unit.KM ).ofLatitude( endOfTheWorldLatitude ).andLongitude( endOfTheWorldLongitude ).createQuery();
org.apache.lucene.search.Query luceneQuery4 = builder.spatial()
.onCoordinates( "location" )
.within( 100000, Unit.KM )
.ofLatitude( endOfTheWorldLatitude )
.andLongitude( endOfTheWorldLongitude )
.createQuery();

org.hibernate.Query hibQuery4 = fullTextSession.createFullTextQuery( luceneQuery4, RangeHotel.class );
List results4 = hibQuery4.list();
Expand Down Expand Up @@ -429,7 +482,8 @@ protected Class<?>[] getAnnotatedClasses() {
RangeEvent.class,
Restaurant.class,
NonGeoPOI.class,
GetterUser.class
GetterUser.class,
MissingSpatialPOI.class
};
}

Expand Down

0 comments on commit 938d389

Please sign in to comment.