Skip to content

Commit

Permalink
HSEARCH-1220 Fix inconsistent docId vs scoreDocId param in distance p…
Browse files Browse the repository at this point in the history
…rojection
  • Loading branch information
Nicolas Helleringer authored and Sanne committed Oct 19, 2012
1 parent 2ace203 commit 1116f74
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Expand Up @@ -183,7 +183,7 @@ public Double spatialDistance(int index) throws IOException {
if ( spatialSearchCenter == null ) {
return null;
}
return Double.valueOf( distanceCollector.getDistance( index ) );
return Double.valueOf( distanceCollector.getDistance( docId( index ) ) );
}

public Explanation explain(int index) throws IOException {
Expand Down
Expand Up @@ -36,6 +36,8 @@
import org.hibernate.search.test.spatial.POI;
import org.junit.Assert;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
/**
* Hibernate Search spatial : unit tests on quering POIs
*
Expand Down Expand Up @@ -157,6 +159,66 @@ public void testDistanceSort() throws Exception {
em.close();
}

public void testDistanceSort2() throws Exception {
FullTextEntityManager em = Search.getFullTextEntityManager( factory.createEntityManager() );

em.getTransaction().begin();
int cnt =0;
for ( double[] c : new double[][] {
{41.04389845, -74.06328534},
{40.64383333, -73.75050000},
{40.75666667, -73.98650000},
{40.69416667, -73.78166667},
{40.75802992, -73.98532391},
{40.75802992, -73.98532391},
{50.77687257, 6.08431213},
{50.78361600, 6.07003500},
{50.76066667, 6.08866667},
{50.77683333, 6.08466667},
{50.77650000, 6.08416667},
} ) {
em.persist( new POI( cnt, "Test_" + cnt, c[0], c[1], "" ) );
++cnt;
}
em.getTransaction().commit();


em.getTransaction().begin();
double centerLatitude = 50.7753455;
double centerLongitude = 6.083886799999959;

final QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity( POI.class ).get();

org.apache.lucene.search.Query luceneQuery = builder.spatial().onCoordinates( "location" )
.within( 1.8097233616663808, 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.setMaxResults( 1000 );
hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE );
hibQuery.setSpatialParameters( Point.fromDegrees( centerLatitude, centerLongitude ), "location" );
List<Object[]> results = hibQuery.getResultList();

for ( Object[] result : results ) {
POI poi = (POI)result[0];
String message = poi.getName() + " (" + poi.getLatitude() + ", " + poi.getLongitude() + ") is not at "
+ centerLatitude + ", " + centerLongitude;

Assert.assertThat( message, ( (Double) result[1] ).doubleValue(), is( not( 0.0 ) ) );
}

List<?> pois = em.createQuery( "from " + POI.class.getName() ).getResultList();
for ( Object entity : pois ) {
em.remove( entity );
}
em.getTransaction().commit();
em.close();
}

@Override
public Class[] getAnnotatedClasses() {
return new Class<?>[] { POI.class };
Expand Down

0 comments on commit 1116f74

Please sign in to comment.