Apache Lucene open-source search software
SOLR-9409: improve error message on unsupported types in collapsing
Munendra S N9228cefbc817
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 26aa716bf49..5a5091c4ad4 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -195,6 +195,9 @@ Bug Fixes* SOLR-13280: Strengthen ScheduledTrigger's preferredOperation parameter validation. (Christine Poerschke)+* SOLR-9409: Improve error message for unsupported field types and return 400 error code on+ unsupported values in collapsing (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 c99fc85d587..b63c119ee7d 100644--- a/solr/core/src/java/org/apache/solr/search/CollapsingQParserPlugin.java+++ b/solr/core/src/java/org/apache/solr/search/CollapsingQParserPlugin.java@@ -389,10 +389,7 @@ public class CollapsingQParserPlugin extends QParserPlugin {boostDocsMap,searcher);- } catch (SolrException e) {- // handle SolrException separately- throw e;- } catch (Exception e) {+ } catch (IOException e) {throw new RuntimeException(e);}}@@ -973,7 +970,7 @@ public class CollapsingQParserPlugin extends QParserPlugin {} else {NumberType numType = fieldType.getNumberType();if (null == numType) {- throw new IOException("min/max must be either Int/Long/Float based field types");+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "min/max must be either Int/Long/Float based field types");}switch (numType) {case INTEGER: {@@ -989,7 +986,7 @@ public class CollapsingQParserPlugin extends QParserPlugin {break;}default: {- throw new IOException("min/max must be either Int/Long/Float field types");+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "min/max must be either Int/Long/Float field types");}}}@@ -1170,7 +1167,8 @@ public class CollapsingQParserPlugin extends QParserPlugin {break;}default: {- throw new IOException("min/max must be Int or Float field types when collapsing on numeric fields");+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,+ "min/max must be Int or Float field types when collapsing on numeric fields");}}}@@ -1314,7 +1312,8 @@ public class CollapsingQParserPlugin extends QParserPlugin {}} else {if(HINT_TOP_FC.equals(hint)) {- throw new IOException("top_fc hint is only supported when collapsing on String Fields");+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,+ "top_fc hint is only supported when collapsing on String Fields");}}@@ -1324,16 +1323,12 @@ public class CollapsingQParserPlugin extends QParserPlugin {if (text.indexOf("(") == -1) {minMaxFieldType = searcher.getSchema().getField(text).getType();} else {- LocalSolrQueryRequest request = null;- try {- SolrParams params = new ModifiableSolrParams();- request = new LocalSolrQueryRequest(searcher.getCore(), params);+ SolrParams params = new ModifiableSolrParams();+ try (SolrQueryRequest request = new LocalSolrQueryRequest(searcher.getCore(), params)) {FunctionQParser functionQParser = new FunctionQParser(text, null, null,request);funcQuery = (FunctionQuery)functionQParser.parse();- } catch (Exception e) {- throw new IOException(e);- } finally {- request.close();+ } catch (SyntaxError e) {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);}}}@@ -1367,7 +1362,8 @@ public class CollapsingQParserPlugin extends QParserPlugin {return new IntScoreCollector(maxDoc, leafCount, nullValue, nullPolicy, size, collapseField, boostDocs);} else {- throw new IOException("64 bit numeric collapse fields are not supported");+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,+ "Collapsing field should be of either String, Int or Float type");}} else { // min, max, sort, etc.. something other then just "score"@@ -1419,7 +1415,8 @@ public class CollapsingQParserPlugin extends QParserPlugin {funcQuery,searcher);} else {- throw new IOException("64 bit numeric collapse fields are not supported");+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,+ "Collapsing field should be of either String, Int or Float type");}}diff --git a/solr/core/src/test/org/apache/solr/search/TestCollapseQParserPlugin.java b/solr/core/src/test/org/apache/solr/search/TestCollapseQParserPlugin.javaindex 16790c073a3..2753542ce17 100644--- a/solr/core/src/test/org/apache/solr/search/TestCollapseQParserPlugin.java+++ b/solr/core/src/test/org/apache/solr/search/TestCollapseQParserPlugin.java@@ -16,7 +16,6 @@*/package org.apache.solr.search;-import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.HashSet;@@ -970,19 +969,13 @@ public class TestCollapseQParserPlugin extends SolrTestCaseJ4 {@Testpublic void test64BitCollapseFieldException() {- ModifiableSolrParams doubleParams = new ModifiableSolrParams();- doubleParams.add("q", "*:*");- doubleParams.add("fq", "{!collapse field=group_d}");- expectThrows(RuntimeException.class, IOException.class, () -> h.query(req(doubleParams)));-- ModifiableSolrParams dateParams = new ModifiableSolrParams();- dateParams.add("q", "*:*");- dateParams.add("fq", "{!collapse field=group_dt}");- expectThrows(RuntimeException.class, IOException.class, () -> h.query(req(dateParams)));-- ModifiableSolrParams longParams = new ModifiableSolrParams();- longParams.add("q", "*:*");- longParams.add("fq", "{!collapse field=group_l}");- expectThrows(RuntimeException.class, IOException.class, () -> h.query(req(longParams)));+ assertQEx("Should Fail For collapsing on Long fields", "Collapsing field should be of either String, Int or Float type",+ req("q", "*:*", "fq", "{!collapse field=group_l}"), SolrException.ErrorCode.BAD_REQUEST);++ assertQEx("Should Fail For collapsing on Double fields", "Collapsing field should be of either String, Int or Float type",+ req("q", "*:*", "fq", "{!collapse field=group_d}"), SolrException.ErrorCode.BAD_REQUEST);++ assertQEx("Should Fail For collapsing on Date fields", "Collapsing field should be of either String, Int or Float type",+ req("q", "*:*", "fq", "{!collapse field=group_dt}"), SolrException.ErrorCode.BAD_REQUEST);}}