This is an automated email from the ASF dual-hosted git repository. dsmiley pushed a commit to branch branch_10x in repository https://gitbox.apache.org/repos/asf/solr.git
commit a8bc5486572d6bb18a30a2501d222532f95d07ca Author: Eric Pugh <[email protected]> AuthorDate: Wed Sep 9 06:43:08 2026 -0400 SOLR-504: a missing or blank "pf" must not add an empty/no-op boolean clause to the query (#4891) (cherry picked from commit a0714c8d01d070d1bf8fea2efb1d3bdece82c280) --- .../SOLR-504-dismax-blank-pf-empty-clause.yml | 9 +++++++ .../java/org/apache/solr/search/DisMaxQParser.java | 7 +++++- .../solr/search/TestExtendedDismaxParser.java | 29 ++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/changelog/unreleased/SOLR-504-dismax-blank-pf-empty-clause.yml b/changelog/unreleased/SOLR-504-dismax-blank-pf-empty-clause.yml new file mode 100644 index 00000000000..ebab4b53904 --- /dev/null +++ b/changelog/unreleased/SOLR-504-dismax-blank-pf-empty-clause.yml @@ -0,0 +1,9 @@ +title: > + The DisMax query parser no longer adds an empty, no-op boolean clause to the query when the + pf parameter is blank or missing. +type: fixed +authors: + - name: Eric Pugh +links: + - name: SOLR-504 + url: https://issues.apache.org/jira/browse/SOLR-504 diff --git a/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java b/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java index 25e93592d35..91eec6acabd 100644 --- a/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java +++ b/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java @@ -227,7 +227,12 @@ public class DisMaxQParser extends QParser { * matched those phrases but do match looser phrases. */ String userPhraseQuery = userQuery.replace("\"", ""); - return pp.parse("\"" + userPhraseQuery + "\""); + Query phrase = pp.parse("\"" + userPhraseQuery + "\""); + // blank/missing pf yields an empty BooleanQuery, not null; normalize it + if (ExtendedDismaxQParser.isEmpty(phrase)) { + return null; + } + return phrase; } protected Query getUserQuery( diff --git a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java index cf80ef4eb24..f1568b25795 100644 --- a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java +++ b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java @@ -3377,4 +3377,33 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 { "org.apache.solr.search.SyntaxError: Query Field 'nosuchfield' is not a valid field name", exception.getMessage()); } + + /** SOLR-504: a missing or blank "pf" must not add an empty/no-op boolean clause to the query */ + @Test + public void testPfMissingOrBlankAddsNoEmptyClause() throws Exception { + final String expectedNoPf = "+((subject:hello | title:hello) (subject:world | title:world))"; + final String expectedRealPf = expectedNoPf + " (subject:\"hello world\")"; + + for (String defType : List.of("dismax", "edismax")) { + try (SolrQueryRequest req = req("qf", "subject title", "defType", defType)) { + assertEquals( + defType, + expectedNoPf, + QParser.getParser("hello world", defType, req).getQuery().toString()); + } + try (SolrQueryRequest req = req("qf", "subject title", "pf", "", "defType", defType)) { + assertEquals( + defType, + expectedNoPf, + QParser.getParser("hello world", defType, req).getQuery().toString()); + } + // sanity check: a real pf *does* add a (non-empty) phrase clause + try (SolrQueryRequest req = req("qf", "subject title", "pf", "subject", "defType", defType)) { + assertEquals( + defType, + expectedRealPf, + QParser.getParser("hello world", defType, req).getQuery().toString()); + } + } + } }
