SOLR-15031 Prevent null being wrapped in a QueryValueSource

closes #2118
This commit is contained in:
Pieter van Boxtel 2020-11-27 12:41:36 +01:00 committed by Mike Drob
parent 1b67ed9516
commit 98f12f4aeb
No known key found for this signature in database
GPG Key ID: 3E48C0C6EF362B9E
4 changed files with 80 additions and 58 deletions

View File

@ -42,6 +42,10 @@ public class QueryValueSource extends ValueSource {
final float defVal;
public QueryValueSource(Query q, float defVal) {
super();
if (q == null) {
throw new IllegalArgumentException("query cannot be null");
}
this.q = q;
this.defVal = defVal;
}

View File

@ -268,6 +268,9 @@ Bug Fixes
* SOLR-14939: JSON range faceting to support cache=false parameter. (Christine Poerschke, Mike Drob)
* SOLR-15031: Fix preventing null being wrapped in a QueryValueSource subQuery. Such null queries can be caused by query text
resulting in an empty token stream. (Pieter van Boxtel via Mike Drob)
Other Changes
---------------------

View File

@ -361,7 +361,9 @@ public class FunctionQParser extends QParser {
((FunctionQParser)subParser).setParseMultipleSources(true);
}
Query subQuery = subParser.getQuery();
if (subQuery instanceof FunctionQuery) {
if (subQuery == null) {
valueSource = new ConstValueSource(0.0f);
} else if (subQuery instanceof FunctionQuery) {
valueSource = ((FunctionQuery) subQuery).getValueSource();
} else {
valueSource = new QueryValueSource(subQuery, 0.0f);

View File

@ -1148,4 +1148,17 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {
/*id*/1, /*score*/2,
/*id*/2, /*score*/2);
}
/**
* Tests a specific (edge) case where a subQuery is null, because no terms are
* found in the query. Below such subQuery is created from a field query on a
* query text containing only stopwords. Feeding the resulting null-subQuery
* into a QueryValueSource (and then using it in for example an if function)
* may not produce NullPointerExceptions.
*/
@Test
public void testNullSubQuery() throws Exception {
clearIndex();
assertJQ(req("q", "{!func}if($subQuery,1,0)", "subQuery", "{!field f=text v='stopworda'}"));
}
}