Apache Lucene open-source search software
SOLR-12979: fail fast when collapse field is non-docValued & non-uninvertible
Munendra S Ne0e5296abcef
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 14bdc71a109..e21132d502b 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -185,6 +185,9 @@ Bug Fixes* SOLR-13187: Fix NPE when invalid query parser is specified and return 400 error code.(Cesar Rodriguez, Marek, Charles Sanders, Munendra S N, Mikhail Khludnev)+* SOLR-12979: Improve error message and change error code to 400 when collapse field is non-docValued and+ non-uninvertible (hossman, Munendra S N)+Other Changes----------------------diff --git a/solr/core/src/java/org/apache/solr/search/CollapsingQParserPlugin.java b/solr/core/src/java/org/apache/solr/search/CollapsingQParserPlugin.javaindex c0e9223c7d3..c99fc85d587 100644--- a/solr/core/src/java/org/apache/solr/search/CollapsingQParserPlugin.java+++ b/solr/core/src/java/org/apache/solr/search/CollapsingQParserPlugin.java@@ -286,8 +286,12 @@ public class CollapsingQParserPlugin extends QParserPlugin {// if unknown field, this would fail fastSchemaField collapseFieldSf = request.getSchema().getField(this.collapseField);- // collapseFieldSf won't be null- if (collapseFieldSf.multiValued()) {+ if (!(collapseFieldSf.isUninvertible() || collapseFieldSf.hasDocValues())) {+ // uninvertible=false and docvalues=false+ // field can't be indexed=false and uninvertible=true+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Collapsing field '" + collapseField ++ "' should be either docValues enabled or indexed with uninvertible enabled");+ } else if (collapseFieldSf.multiValued()) {throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Collapsing not supported on multivalued fields");}diff --git a/solr/core/src/test/org/apache/solr/search/TestCollapseQParserPlugin.java b/solr/core/src/test/org/apache/solr/search/TestCollapseQParserPlugin.javaindex acdd59963df..16790c073a3 100644--- a/solr/core/src/test/org/apache/solr/search/TestCollapseQParserPlugin.java+++ b/solr/core/src/test/org/apache/solr/search/TestCollapseQParserPlugin.java@@ -814,8 +814,12 @@ public class TestCollapseQParserPlugin extends SolrTestCaseJ4 {// this is currently ok on fields that don't exist on any docs in the indexfor (String f : Arrays.asList("not_indexed_sS", "indexed_s_not_uninvert")) {for (String hint : Arrays.asList("", " hint=top_fc")) {- assertQ(req(params("q", "*:*", "fq", "{!collapse field="+f+hint+"}"))- , "*[count(//doc)=0]");+ SolrException e = expectThrows(SolrException.class,+ () -> h.query(req("q", "*:*", "fq", "{!collapse field="+f + hint +"}")));+ assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, e.code());+ assertTrue("unexpected Message: " + e.getMessage(),+ e.getMessage().contains("Collapsing field '" + f + "' " ++ "should be either docValues enabled or indexed with uninvertible enabled"));}}}@@ -941,18 +945,24 @@ public class TestCollapseQParserPlugin extends SolrTestCaseJ4 {req("q","*:*", "fq","{!collapse field=bleh}"), SolrException.ErrorCode.BAD_REQUEST);// if a field is uninvertible=false, it should behave the same as a field that is indexed=false ...+ // this also tests docValues=false along with indexed=false or univertible=falsefor (String f : Arrays.asList("not_indexed_sS", "indexed_s_not_uninvert")) {- { // this currently propogates up the low level DocValues error in the common case...- Exception e = expectThrows(RuntimeException.class, IllegalStateException.class,+ {+ SolrException e = expectThrows(SolrException.class,() -> h.query(req(params("q", "*:*","fq", "{!collapse field="+f+"}"))));+ assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, e.code());assertTrue("unexpected Message: " + e.getMessage(),- e.getMessage().contains("Re-index with correct docvalues type"));+ e.getMessage().contains("Collapsing field '" + f + "' " ++ "should be either docValues enabled or indexed with uninvertible enabled"));}- { // ... but in the case of hint=top_fc a bare NPE gets propogated up (SOLR-12979)...- expectThrows(RuntimeException.class, NullPointerException.class,- () -> h.query(req(params("q", "*:*",- "fq", "{!collapse field="+f+" hint=top_fc}"))));+ {+ SolrException e = expectThrows(SolrException.class,+ () -> h.query(req("q", "*:*", "fq", "{!collapse field="+f+" hint=top_fc}")));+ assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, e.code());+ assertTrue("unexpected Message: " + e.getMessage(),+ e.getMessage().contains("Collapsing field '" + f + "' " ++ "should be either docValues enabled or indexed with uninvertible enabled"));}}