Issue Type: Bug Bug
Affects Versions: 4.2.0.Beta2
Assignee: Unassigned
Attachments: Testcase.zip
Created: 18/Oct/12 2:43 PM
Description:

The new DistanceSortField does not work when used with MaxResults set to something smaller than the number of documents stored.

The error is easy to reproduce, just add the following to the org.hibernate.search.test.jpa.SpatialQueringJPATest in module hibernate-search-orm

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 );
                // Set max results to 3 when 6 documents are stored:
		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();
	}

In hibernate-search-orm run it with mvn -Dtest=SpatialQueringJPATest test

Result is:

-------------------------------------------------------------------------------
Test set: org.hibernate.search.test.jpa.SpatialQueringJPATest
-------------------------------------------------------------------------------
Tests run: 3, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.895 sec <<< FAILURE!
testDistanceSortWithMaxResult(org.hibernate.search.test.jpa.SpatialQueringJPATest)  Time elapsed: 0.052 sec  <<< ERROR!
java.lang.ArrayIndexOutOfBoundsException: 3
	at org.hibernate.search.spatial.impl.DistanceComparator.setNextReader(DistanceComparator.java:77)
	at org.apache.lucene.search.TopFieldCollector$OneComparatorNonScoringCollector.setNextReader(TopFieldCollector.java:95)
	at org.hibernate.search.spatial.impl.DistanceCollector.setNextReader(DistanceCollector.java:69)
	at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:576)
	at org.hibernate.search.query.engine.impl.QueryHits.updateTopDocs(QueryHits.java:243)
	at org.hibernate.search.query.engine.impl.QueryHits.<init>(QueryHits.java:144)
	at org.hibernate.search.query.engine.impl.HSQueryImpl.getQueryHits(HSQueryImpl.java:457)
	at org.hibernate.search.query.engine.impl.HSQueryImpl.queryEntityInfos(HSQueryImpl.java:254)
	at org.hibernate.search.query.hibernate.impl.FullTextQueryImpl.list(FullTextQueryImpl.java:209)
	at org.hibernate.search.jpa.impl.FullTextQueryImpl.getResultList(FullTextQueryImpl.java:160)
	at org.hibernate.search.test.jpa.SpatialQueringJPATest.testDistanceSortWithMaxResult(SpatialQueringJPATest.java:197)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at junit.framework.TestCase.runTest(TestCase.java:168)
	at junit.framework.TestCase.runBare(TestCase.java:134)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:243)
	at junit.framework.TestSuite.run(TestSuite.java:238)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:236)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:134)
	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:113)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
	at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
	at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:103)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74)

Environment: Java 7, Hibernate 4.1.7, MySQL
Project: Hibernate Search
Priority: Major Major
Reporter: Michael Simons
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira