diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index aa37779994f..6ea7f94764b 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -102,6 +102,8 @@ Improvements * SOLR-13663: Introduce into XML Query Parser (Alessandro Benedetti via Mikhail Khludnev) +* LUCENE-8952: Use a sort key instead of true distance in NearestNeighbor (Julie Tibshirani). + Optimizations * LUCENE-8922: DisjunctionMaxQuery more efficiently leverages impacts to skip diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/LatLonPointPrototypeQueries.java b/lucene/sandbox/src/java/org/apache/lucene/search/LatLonPointPrototypeQueries.java index 73cbbb87fe4..3c0d7ff3164 100644 --- a/lucene/sandbox/src/java/org/apache/lucene/search/LatLonPointPrototypeQueries.java +++ b/lucene/sandbox/src/java/org/apache/lucene/search/LatLonPointPrototypeQueries.java @@ -27,6 +27,7 @@ import org.apache.lucene.geo.GeoUtils; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.PointValues; import org.apache.lucene.util.Bits; +import org.apache.lucene.util.SloppyMath; import org.apache.lucene.util.bkd.BKDReader; /** @@ -104,7 +105,8 @@ public class LatLonPointPrototypeQueries { ScoreDoc[] scoreDocs = new ScoreDoc[hits.length]; for(int i=0;i hitQueue = new PriorityQueue<>(n, new Comparator() { @Override public int compare(NearestHit a, NearestHit b) { - // sort by opposite distanceMeters natural order - int cmp = Double.compare(a.distanceMeters, b.distanceMeters); + // sort by opposite distanceSortKey natural order + int cmp = Double.compare(a.distanceSortKey, b.distanceSortKey); if (cmp != 0) { return -cmp; } @@ -319,10 +327,10 @@ class NearestNeighbor { return 0.0; } - double d1 = SloppyMath.haversinMeters(pointLat, pointLon, minLat, minLon); - double d2 = SloppyMath.haversinMeters(pointLat, pointLon, minLat, maxLon); - double d3 = SloppyMath.haversinMeters(pointLat, pointLon, maxLat, maxLon); - double d4 = SloppyMath.haversinMeters(pointLat, pointLon, maxLat, minLon); + double d1 = SloppyMath.haversinSortKey(pointLat, pointLon, minLat, minLon); + double d2 = SloppyMath.haversinSortKey(pointLat, pointLon, minLat, maxLon); + double d3 = SloppyMath.haversinSortKey(pointLat, pointLon, maxLat, maxLon); + double d4 = SloppyMath.haversinSortKey(pointLat, pointLon, maxLat, minLon); return Math.min(Math.min(d1, d2), Math.min(d3, d4)); } diff --git a/lucene/sandbox/src/test/org/apache/lucene/search/TestNearest.java b/lucene/sandbox/src/test/org/apache/lucene/search/TestNearest.java index 40c521e43c1..627b7245881 100644 --- a/lucene/sandbox/src/test/org/apache/lucene/search/TestNearest.java +++ b/lucene/sandbox/src/test/org/apache/lucene/search/TestNearest.java @@ -26,7 +26,6 @@ import org.apache.lucene.document.LatLonDocValuesField; import org.apache.lucene.document.LatLonPoint; import org.apache.lucene.document.StoredField; import org.apache.lucene.document.StringField; -import org.apache.lucene.search.NearestNeighbor.NearestHit; import org.apache.lucene.geo.GeoEncodingUtils; import org.apache.lucene.geo.GeoTestUtil; import org.apache.lucene.index.DirectoryReader; @@ -190,23 +189,22 @@ public class TestNearest extends LuceneTestCase { double pointLon = GeoTestUtil.nextLongitude(); // dumb brute force search to get the expected result: - NearestHit[] expectedHits = new NearestHit[lats.length]; + FieldDoc[] expectedHits = new FieldDoc[lats.length]; for(int id=0;id() { + Arrays.sort(expectedHits, new Comparator() { @Override - public int compare(NearestHit a, NearestHit b) { - int cmp = Double.compare(a.distanceMeters, b.distanceMeters); + public int compare(FieldDoc a, FieldDoc b) { + int cmp = Double.compare(((Double) a.fields[0]).doubleValue(), ((Double) b.fields[0]).doubleValue()); if (cmp != 0) { return cmp; } // tie break by smaller docID: - return a.docID - b.docID; + return a.doc - b.doc; } }); @@ -221,22 +219,23 @@ public class TestNearest extends LuceneTestCase { ScoreDoc[] hits = LatLonPointPrototypeQueries.nearest(s, "point", pointLat, pointLon, topN).scoreDocs; for(int i=0;i