Apache Solr open-source search software
SOLR-18016: Fix spatial distance sorting with SRPT and filter cache (#3935)
David Smiley94e969c05336
diff --git a/changelog/unreleased/SOLR-18006-Fix reverse dist sorting on LatLonPointSpatialField.yml b/changelog/unreleased/SOLR-18006-Fix reverse dist sorting on LatLonPointSpatialField.ymlindex d3306164c38..3af69caac4a 100644--- a/changelog/unreleased/SOLR-18006-Fix reverse dist sorting on LatLonPointSpatialField.yml+++ b/changelog/unreleased/SOLR-18006-Fix reverse dist sorting on LatLonPointSpatialField.yml@@ -1,5 +1,4 @@-# See https://github.com/apache/solr/blob/main/dev-docs/changelog.adoc-title: Fix reverse distance sorting on LatLonPointSpatialField when combined with filter cache+title: Fix reverse distance sorting on LatLonPointSpatialField and "SRPT" fields when combined with the filter cache. This is a regression since Solr 9.9.type: fixed # added, changed, fixed, deprecated, removed, dependency_update, security, otherauthors:- name: Jan Høydahl@@ -10,3 +9,5 @@ authors:links:- name: SOLR-18006url: https://issues.apache.org/jira/browse/SOLR-18006+ - name: SOLR-18016+ url: https://issues.apache.org/jira/browse/SOLR-18016diff --git a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.javaindex 3604f84d0fc..7868d7688ca 100644--- a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java+++ b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java@@ -2381,7 +2381,7 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrIbase = leaf.docBase;end = base + leaf.reader().maxDoc();leafCollector = topCollector.getLeafCollector(leaf);- // we should never need to set the scorer given the settings for the collector+ leafCollector.setScorer(CONSTANT_SCORABLE);}leafCollector.collect(doc - base);}@@ -2703,4 +2703,12 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrIpublic long getWarmupTime() {return warmupTime;}++ private static final Scorable CONSTANT_SCORABLE =+ new Scorable() {+ @Override+ public float score() throws IOException {+ return 1f;+ }+ };}diff --git a/solr/core/src/test/org/apache/solr/search/TestSolr4Spatial.java b/solr/core/src/test/org/apache/solr/search/TestSolr4Spatial.javaindex 3aed5e46d5a..01e58f42e77 100644--- a/solr/core/src/test/org/apache/solr/search/TestSolr4Spatial.java+++ b/solr/core/src/test/org/apache/solr/search/TestSolr4Spatial.java@@ -68,7 +68,7 @@ public class TestSolr4Spatial extends SolrTestCaseJ4 {@BeforeClasspublic static void beforeClass() throws Exception {- initCore("solrconfig-basic.xml", "schema-spatial.xml");+ initCore("solrconfig-spatial.xml", "schema-spatial.xml");}@Override@@ -595,4 +595,46 @@ public class TestSolr4Spatial extends SolrTestCaseJ4 {assertFalse(strategy.getFieldType().stored());}}++ @Test+ public void testSOLR18006_GeodistDescWithFilterQuery() throws Exception { // AND SOLR-18016+ assumeTrue("Test requires distance calculations", canCalcDistance);+ assumeFalse("Test doesn't work with bbox", isBBoxField(fieldName));+ // SOLR-18006: geodist() desc sorting causes NPE when spatial query is in filter query+ // Reproduction from JIRA issue with exact coordinates and parameters++ // Index sample documents from JIRA issue+ assertU(adoc("id", "pt-001", fieldName, "48.106651,11.628476"));+ assertU(adoc("id", "pt-002", fieldName, "48.113089,11.622016"));+ assertU(adoc("id", "pt-003", fieldName, "48.137154,11.576124"));+ assertU(adoc("id", "pt-004", fieldName, "48.135125,11.581981"));+ assertU(adoc("id", "pt-005", fieldName, "48.121,11.612"));+ assertU(adoc("id", "pt-006", fieldName, "48.09,11.64"));+ assertU(commit());++ // Test descending sort with filter query - exact query from JIRA that triggers NPE+ // Expected order by distance DESC from pt=48.11308880280511,11.622015740056845:+ // pt-003 (48.137154,11.576124) - farthest+ // pt-004 (48.135125,11.581981)+ // pt-006 (48.09,11.64)+ // pt-005 (48.121,11.612)+ // pt-001 (48.106651,11.628476)+ // pt-002 (48.113089,11.622016) - closest+ assertJQ(+ req(+ "q", "*:*",+ "fq", "{!geofilt}",+ "sfield", fieldName,+ "pt", "48.11308880280511,11.622015740056845",+ "d", "10",+ "fl", "id",+ "sort", "geodist() desc"),+ "/response/numFound==6",+ "/response/docs/[0]/id=='pt-003'", // farthest+ "/response/docs/[1]/id=='pt-004'",+ "/response/docs/[2]/id=='pt-006'",+ "/response/docs/[3]/id=='pt-005'",+ "/response/docs/[4]/id=='pt-001'",+ "/response/docs/[5]/id=='pt-002'"); // closest+ }}diff --git a/solr/core/src/test/org/apache/solr/search/TestSolr4Spatial2.java b/solr/core/src/test/org/apache/solr/search/TestSolr4Spatial2.javaindex 2c96be7eb2a..166d17e8e94 100644--- a/solr/core/src/test/org/apache/solr/search/TestSolr4Spatial2.java+++ b/solr/core/src/test/org/apache/solr/search/TestSolr4Spatial2.java@@ -551,45 +551,4 @@ public class TestSolr4Spatial2 extends SolrTestCaseJ4 {"sort", "min(geodist(),geodist(55.4721936,-2.24703,llp)) asc"),"/response/docs/[0]/id=='2'");}-- @Test- public void testSOLR18006_GeodistDescWithFilterQuery() throws Exception {- // SOLR-18006: geodist() desc sorting causes NPE when spatial query is in filter query- // Reproduction from JIRA issue with exact coordinates and parameters- String fieldName = "llp_km";-- // Index sample documents from JIRA issue- assertU(adoc("id", "pt-001", fieldName, "48.106651,11.628476"));- assertU(adoc("id", "pt-002", fieldName, "48.113089,11.622016"));- assertU(adoc("id", "pt-003", fieldName, "48.137154,11.576124"));- assertU(adoc("id", "pt-004", fieldName, "48.135125,11.581981"));- assertU(adoc("id", "pt-005", fieldName, "48.121,11.612"));- assertU(adoc("id", "pt-006", fieldName, "48.09,11.64"));- assertU(commit());-- // Test descending sort with filter query - exact query from JIRA that triggers NPE- // Expected order by distance DESC from pt=48.11308880280511,11.622015740056845:- // pt-003 (48.137154,11.576124) - farthest- // pt-004 (48.135125,11.581981)- // pt-006 (48.09,11.64)- // pt-005 (48.121,11.612)- // pt-001 (48.106651,11.628476)- // pt-002 (48.113089,11.622016) - closest- assertJQ(- req(- "q", "*:*",- "fq", "{!geofilt}",- "sfield", fieldName,- "pt", "48.11308880280511,11.622015740056845",- "d", "10",- "fl", "id",- "sort", "geodist() desc"),- "/response/numFound==6",- "/response/docs/[0]/id=='pt-003'", // farthest- "/response/docs/[1]/id=='pt-004'",- "/response/docs/[2]/id=='pt-006'",- "/response/docs/[3]/id=='pt-005'",- "/response/docs/[4]/id=='pt-001'",- "/response/docs/[5]/id=='pt-002'"); // closest- }}