Skip to content

Apache Lucene open-source search software

SOLR-9616 Solr throws exception when expand=true on empty index

Ishan Chattopadhyayae64bcb37ffe9
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 1584647e787..d09ae3be13b 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -218,6 +218,8 @@ Bug Fixes
* SOLR-9768: RecordingJsonParser produces incomplete json (Wojciech Stryszyk via ab)
+* SOLR-9616: Solr throws exception when expand=true on empty index (Timo Hund via Ishan Chattopadhyaya)
+
Other Changes
----------------------
diff --git a/solr/core/src/java/org/apache/solr/handler/component/ExpandComponent.java b/solr/core/src/java/org/apache/solr/handler/component/ExpandComponent.java
index 8274d68aebd..366c4a9b7a8 100644
--- a/solr/core/src/java/org/apache/solr/handler/component/ExpandComponent.java
+++ b/solr/core/src/java/org/apache/solr/handler/component/ExpandComponent.java
@@ -265,6 +265,12 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
* This code gathers the group information for the current page.
*/
List<LeafReaderContext> contexts = searcher.getTopReaderContext().leaves();
+
+ if(contexts.size() == 0) {
+ //When no context is available we can skip the expanding
+ return;
+ }
+
int currentContext = 0;
int currentDocBase = contexts.get(currentContext).docBase;
int nextDocBase = (currentContext+1)<contexts.size() ? contexts.get(currentContext+1).docBase : Integer.MAX_VALUE;
diff --git a/solr/core/src/test/org/apache/solr/handler/component/TestExpandComponent.java b/solr/core/src/test/org/apache/solr/handler/component/TestExpandComponent.java
index 7d38d625bad..f7fc13a00b8 100644
--- a/solr/core/src/test/org/apache/solr/handler/component/TestExpandComponent.java
+++ b/solr/core/src/test/org/apache/solr/handler/component/TestExpandComponent.java
@@ -314,4 +314,19 @@ public class TestExpandComponent extends SolrTestCaseJ4 {
);
}
+ @Test
+ public void testExpandWithEmptyIndexReturnsZeroResults() {
+ //We make sure the index is cleared
+
+ clearIndex();
+ assertU(commit());
+
+ ModifiableSolrParams params = new ModifiableSolrParams();
+ params.add("q", "*:*");
+ params.add("fq", "{!collapse field=group_s}");
+ params.add("expand" ,"true");
+ params.add("expand.rows", "10");
+
+ assertQ(req(params), "*[count(//doc)=0]");
+ }
}