<style>
/* Changing the layout to use less space for mobiles */
@media screen and (max-device-width: 480px), screen and (-webkit-min-device-pixel-ratio: 2) {
#email-body { min-width: 30em !important; }
#email-page { padding: 8px !important; }
#email-banner { padding: 8px 8px 0 8px !important; }
#email-avatar { margin: 1px 8px 8px 0 !important; padding: 0 !important; }
#email-fields { padding: 0 8px 8px 8px !important; }
#email-gutter { width: 0 !important; }
}
</style>
<div id="email-body">
<table id="email-wrap" align="center" border="0" cellpadding="0" cellspacing="0" style="background-color:#f0f0f0;color:#000000;width:100%;">
<tr valign="top">
<td id="email-page" style="padding:16px !important;">
<table align="center" border="0" cellpadding="0" cellspacing="0" style="background-color:#ffffff;border:1px solid #bbbbbb;color:#000000;width:100%;">
<tr valign="top">
<td bgcolor="#3e4c4e" style="background-color:#3e4c4e;color:#ffffff;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;line-height:1;"><img src="https://www.jboss.org/dms/hibernate/images/jira/jiraheader_hibernate.png" alt="" style="vertical-align:top;" /></td>
</tr><tr valign="top">
<td id="email-banner" style="padding:32px 32px 0 32px;">
<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="width:100%;">
<tr valign="top">
<td style="color:#505050;font-family:Arial,FreeSans,Helvetica,sans-serif;padding:0;">
<img id="email-avatar" src="https://hibernate.onjira.com/secure/useravatar?avatarId=10162" alt="" height="48" width="48" border="0" align="left" style="padding:0;margin: 0 16px 16px 0;" />
<div id="email-action" style="padding: 0 0 8px 0;font-size:12px;line-height:18px;">
<a class="user-hover" rel="michael.simons" id="email_michael.simons" href="https://hibernate.onjira.com/secure/ViewProfile.jspa?name=michael.simons" style="color:#6c797f;">Michael Simons</a>
created <img src="https://hibernate.onjira.com/images/icons/bug.gif" height="16" width="16" border="0" align="absmiddle" alt="Bug"> <a style='color:#6c797f;text-decoration:none;' href='https://hibernate.onjira.com/browse/HSEARCH-1219'>HSEARCH-1219</a>
</div>
<div id="email-summary" style="font-size:16px;line-height:20px;padding:2px 0 16px 0;">
<a style='color:#6c797f;text-decoration:none;' href='https://hibernate.onjira.com/browse/HSEARCH-1219'><strong>new DistanceSortField doesn't work with FullTextQuery#setMaxResults</strong></a>
</div>
</td>
</tr>
</table>
</td>
</tr>
<tr valign="top">
<td id="email-fields" style="padding:0 32px 32px 32px;">
<table border="0" cellpadding="0" cellspacing="0" style="padding:0;text-align:left;width:100%;" width="100%">
<tr valign="top">
<td id="email-gutter" style="width:64px;white-space:nowrap;"></td>
<td>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr valign="top">
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 10px 10px 0;white-space:nowrap;">
<strong style="font-weight:normal;color:#505050;">Issue Type:</strong>
</td>
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 0 10px 0;width:100%;">
<img src="https://hibernate.onjira.com/images/icons/bug.gif" height="16" width="16" border="0" align="absmiddle" alt="Bug"> Bug
</td>
</tr> <tr valign="top">
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 10px 10px 0;white-space:nowrap;">
<strong style="font-weight:normal;color:#505050;">Affects Versions:</strong>
</td>
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 0 10px 0;width:100%;">
4.2.0.Beta2 </td>
</tr>
<tr valign="top">
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 10px 10px 0;white-space:nowrap;">
<strong style="font-weight:normal;color:#505050;">Assignee:</strong>
</td>
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 0 10px 0;width:100%;">
Unassigned </td>
</tr> <tr valign="top">
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 10px 10px 0;white-space:nowrap;">
<strong style="font-weight:normal;color:#505050;">Attachments:</strong>
</td>
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 0 10px 0;width:100%;">
Testcase.zip </td>
</tr>
<tr valign="top">
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 10px 10px 0;white-space:nowrap;">
<strong style="font-weight:normal;color:#505050;">Created:</strong>
</td>
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 0 10px 0;width:100%;">
18/Oct/12 2:43 PM
</td>
</tr> <tr valign="top">
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 10px 10px 0;white-space:nowrap;">
<strong style="font-weight:normal;color:#505050;">Description:</strong>
</td>
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 0 10px 0;width:100%;">
<p style='margin-top:0;margin-bottom:10px;'>The new DistanceSortField does not work when used with MaxResults set to something smaller than the number of documents stored.</p>
<p style='margin-top:0;margin-bottom:10px;'>The error is easy to reproduce, just add the following to the org.hibernate.search.test.jpa.SpatialQueringJPATest in module hibernate-search-orm</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> void testDistanceSortWithMaxResult() <span class="code-keyword">throws</span> Exception {
                POI poi = <span class="code-keyword">new</span> POI( 1, <span class="code-quote">"Distance to 24,32 : 0"</span>, 24.0d, 32.0d, "" );
                POI poi2 = <span class="code-keyword">new</span> POI( 2, <span class="code-quote">"Distance to 24,32 : 10.16"</span>, 24.0d, 31.9d, "" );
                POI poi3 = <span class="code-keyword">new</span> POI( 3, <span class="code-quote">"Distance to 24,32 : 11.12"</span>, 23.9d, 32.0d, "" );
                POI poi4 = <span class="code-keyword">new</span> POI( 4, <span class="code-quote">"Distance to 24,32 : 15.06"</span>, 23.9d, 32.1d, "" );
                POI poi5 = <span class="code-keyword">new</span> POI( 5, <span class="code-quote">"Distance to 24,32 : 22.24"</span>, 24.2d, 32.0d, "" );
                POI poi6 = <span class="code-keyword">new</span> POI( 6, <span class="code-quote">"Distance to 24,32 : 24.45"</span>, 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();
                <span class="code-object">double</span> centerLatitude = 24.0d;
                <span class="code-object">double</span> centerLongitude = 32.0d;
                <span class="code-keyword">final</span> QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity( POI.class ).get();
                org.apache.lucene.search.Query luceneQuery = builder.spatial().onCoordinates( <span class="code-quote">"location"</span> )
                                .within( 100, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery();
                FullTextQuery hibQuery = em.createFullTextQuery( luceneQuery, POI.class );
                Sort distanceSort = <span class="code-keyword">new</span> Sort( <span class="code-keyword">new</span> DistanceSortField( centerLatitude, centerLongitude, <span class="code-quote">"location"</span> ) );
                hibQuery.setSort( distanceSort );
                hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE );
<span class="code-comment">// Set max results to 3 when 6 documents are stored:
</span>                hibQuery.setMaxResults(3);
                hibQuery.setSpatialParameters( Point.fromDegrees( centerLatitude, centerLongitude ), <span class="code-quote">"location"</span> );
                List results = hibQuery.getResultList();
                <span class="code-object">Object</span>[] firstResult = (<span class="code-object">Object</span>[]) results.get( 0 );
                <span class="code-object">Object</span>[] secondResult = (<span class="code-object">Object</span>[]) results.get( 1 );
                <span class="code-object">Object</span>[] thirdResult = (<span class="code-object">Object</span>[]) results.get( 2 );
                Assert.assertEquals( (<span class="code-object">Double</span>) firstResult[1], 0.0, 0.0001 );
                Assert.assertEquals( (<span class="code-object">Double</span>) secondResult[1], 10.1582, 0.0001 );
                Assert.assertEquals( (<span class="code-object">Double</span>) thirdResult[1], 11.1195, 0.0001 );
                List<?> pois = em.createQuery( <span class="code-quote">"from "</span> + POI.class.getName() ).getResultList();
                <span class="code-keyword">for</span> ( <span class="code-object">Object</span> entity : pois ) {
                        em.remove( entity );
                }
                em.getTransaction().commit();
                em.close();
        }</pre>
</div></div>
<p style='margin-top:0;margin-bottom:10px;'>In hibernate-search-orm run it with mvn -Dtest=SpatialQueringJPATest test</p>
<p style='margin-top:0;margin-bottom:10px;'>Result is:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>
-------------------------------------------------------------------------------
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)
</pre>
</div></div>
</td>
</tr>
<tr valign="top">
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 10px 10px 0;white-space:nowrap;">
<strong style="font-weight:normal;color:#505050;">Environment:</strong>
</td>
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 0 10px 0;width:100%;">
Java 7, Hibernate 4.1.7, MySQL
</td>
</tr>
<tr valign="top">
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 10px 10px 0;white-space:nowrap;">
<strong style="font-weight:normal;color:#505050;">Project:</strong>
</td>
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 0 10px 0;width:100%;">
<a style="color:#6c797f;" href="https://hibernate.onjira.com/browse/HSEARCH">Hibernate Search</a>
</td>
</tr> <tr valign="top">
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 10px 10px 0;white-space:nowrap;">
<strong style="font-weight:normal;color:#505050;">Priority:</strong>
</td>
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 0 10px 0;width:100%;">
<img src="https://hibernate.onjira.com/images/icons/priority_major.gif" height="16" width="16" border="0" align="absmiddle" alt="Major"> Major
</td>
</tr> <tr valign="top">
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 10px 10px 0;white-space:nowrap;">
<strong style="font-weight:normal;color:#505050;">Reporter:</strong>
</td>
<td style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 0 10px 0;width:100%;">
<a class="user-hover" rel="michael.simons" id="email_michael.simons" href="https://hibernate.onjira.com/secure/ViewProfile.jspa?name=michael.simons" style="color:#6c797f;">Michael Simons</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td><!-- End #email-page -->
</tr>
<tr valign="top">
<td style="color:#505050;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:10px;line-height:14px;padding: 0 16px 16px 16px;text-align:center;">
This message is automatically generated by JIRA.<br />
If you think it was sent incorrectly, please contact your JIRA administrators<br />
For more information on JIRA, see: <a style='color:#6c797f;' href='http://www.atlassian.com/software/jira'>http://www.atlassian.com/software/jira</a>
</td>
</tr>
</table><!-- End #email-wrap -->
</div><!-- End #email-body -->