This is an automated email from the ASF dual-hosted git repository.
dsmiley pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/main by this push:
new e22443ea8d5 SOLR-18625: eDisMax: don't emit empty pf or bq clauses
(#5303)
e22443ea8d5 is described below
commit e22443ea8d56e3733bc9dfef85f15e2dec7f020b
Author: Ian Springer <[email protected]>
AuthorDate: Sun Nov 15 00:48:58 2026 -0500
SOLR-18625: eDisMax: don't emit empty pf or bq clauses (#5303)
Fix the eDisMax parser adding an empty phrase field (pf) boost clause when
no phrase qualifies for the boost, such as for single-term queries or q=*:*,
and an empty boost query (bq) clause when the bq consists only of stopwords.
The empty SHOULD clauses did not affect scoring but defeated the single-clause
MatchAllDocsQuery optimization; they are now omitted, so q=*:* reduces to a
MatchAllDocsQuery even when pf or bq is set.
Co-authored-by: Ian Springer <[email protected]>
Co-authored-by: Claude <[email protected]>
Co-authored-by: Nick Shanin <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>
---
.../SOLR-18625-empty-phrase-boost-matchalldocs.yml | 12 +++++
.../apache/solr/search/ExtendedDismaxQParser.java | 14 +++--
.../solr/search/TestExtendedDismaxParser.java | 62 +++++++++++++++++++++-
3 files changed, 83 insertions(+), 5 deletions(-)
diff --git
a/changelog/unreleased/SOLR-18625-empty-phrase-boost-matchalldocs.yml
b/changelog/unreleased/SOLR-18625-empty-phrase-boost-matchalldocs.yml
new file mode 100644
index 00000000000..003259c474a
--- /dev/null
+++ b/changelog/unreleased/SOLR-18625-empty-phrase-boost-matchalldocs.yml
@@ -0,0 +1,12 @@
+title: Fix the eDisMax parser adding an empty phrase field (pf) boost clause
when no phrase qualifies
+ for the boost, such as for single-term queries or q=*:*, and an empty boost
query (bq) clause when
+ the bq consists only of stopwords. The empty SHOULD clauses did not affect
scoring but defeated the
+ single-clause MatchAllDocsQuery optimization; they are now omitted, so q=*:*
reduces to a
+ MatchAllDocsQuery even when pf or bq is set.
+type: changed
+authors:
+ - name: Ian Springer
+ nick: ianspringer
+links:
+ - name: SOLR-18625
+ url: https://issues.apache.org/jira/browse/SOLR-18625
diff --git
a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java
b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java
index 809c0305be3..a4ab3902cd5 100644
--- a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java
+++ b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java
@@ -584,7 +584,9 @@ public class ExtendedDismaxQParser extends QParser {
for (String qs : config.boostParams) {
if (qs.trim().length() == 0) continue;
Query q = subQuery(qs, null).getQuery();
- boostQueries.add(q);
+ if (!isEmpty(q)) {
+ boostQueries.add(q);
+ }
}
}
return boostQueries;
@@ -625,8 +627,11 @@ public class ExtendedDismaxQParser extends QParser {
final int slop)
throws SyntaxError {
- if (null == fields || fields.isEmpty() || null == clauses ||
clauses.size() < shingleSize)
- return;
+ if (null == fields
+ || fields.isEmpty()
+ || null == clauses
+ || clauses.isEmpty()
+ || clauses.size() < shingleSize) return;
if (0 == shingleSize) shingleSize = clauses.size();
@@ -676,7 +681,8 @@ public class ExtendedDismaxQParser extends QParser {
// TODO: perhaps we shouldn't use synonyms either...
Query phrase = pp.parse(userPhraseQuery.toString());
- if (phrase != null) {
+ // parse returns an empty BooleanQuery when minClauseSize discards every
phrase
+ if (!isEmpty(phrase)) {
mainQuery.add(phrase, BooleanClause.Occur.SHOULD);
}
}
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 f1568b25795..f6c6cad12f8 100644
--- a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java
+++ b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java
@@ -26,6 +26,7 @@ import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.isA;
+import static org.hamcrest.Matchers.not;
import java.util.Arrays;
import java.util.HashSet;
@@ -54,6 +55,7 @@ import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.util.BaseTestHarness;
import org.apache.solr.util.ErrorLogMuter;
import org.apache.solr.util.SolrPluginUtils;
+import org.hamcrest.Matcher;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -151,7 +153,15 @@ public class TestExtendedDismaxParser extends
SolrTestCaseJ4 {
public void testMatchAllDocs() throws Exception {
for (String sow : Arrays.asList("true", "false")) {
for (String q : Arrays.asList("*:*", "*")) {
- try (SolrQueryRequest req = req("sow", sow, "qf", "id")) {
+ // pf, pf2, pf3, and an all-stopword bq must not add empty clauses
+ try (SolrQueryRequest req =
+ req(
+ "sow", sow,
+ "qf", "id",
+ "pf", "id^10 text^5",
+ "pf2", "id^10",
+ "pf3", "id^10",
+ "bq", "text:a")) {
QParser qParser = QParser.getParser(q, "edismax", req);
assertThat(qParser.getQuery(), isA(MatchAllDocsQuery.class));
}
@@ -1193,6 +1203,56 @@ public class TestExtendedDismaxParser extends
SolrTestCaseJ4 {
"//str[@name='id'][.='146']");
}
+ // When no phrase qualifies for the pf boost, no empty phrase clause should
be added
+ public void testNoEmptyPhraseBoostClause() throws Exception {
+ // Single-token query: minClauseSize discards the one-position phrase
+ assertPhraseBoostParsedQuery(
+ "wireless", "subject", allOf(not(containsString("^10.0")),
not(containsString("()"))));
+ }
+
+ public void testNoEmptyPhraseBoostClauseForFieldedQuery() throws Exception {
+ assertPhraseBoostParsedQuery(
+ "subject:wireless",
+ "subject",
+ allOf(not(containsString("^10.0")), not(containsString("()"))));
+ }
+
+ // Whitespace-free query that analyzes into multiple tokens keeps its phrase
boost
+ public void testAnalyzedSingleTermKeepsPhraseBoost() throws Exception {
+ assertPhraseBoostParsedQuery("wi-fi", "subject",
containsString("(subject:\"wi fi\")^10.0"));
+ }
+
+ // The unanalyzed qf field keeps this phrase exclusive to the pf clause.
+ public void testSingleTermSynonymKeepsPhraseBoost() throws Exception {
+ assertPhraseBoostParsedQuery(
+ "grackle",
+ "id",
+ allOf(containsString("subject:\"crow blackbird\""),
containsString("^10.0")));
+ }
+
+ /** Parses {@code q} with {@code pf=subject^10} in both {@code sow} modes
and checks each. */
+ private void assertPhraseBoostParsedQuery(String q, String qf,
Matcher<String> matcher)
+ throws Exception {
+ for (String sow : Arrays.asList("true", "false")) {
+ String parsed =
+ getParsedQuery(
+ req(
+ "q",
+ q,
+ "qf",
+ qf,
+ "pf",
+ "subject^10",
+ "sow",
+ sow,
+ "defType",
+ "edismax",
+ "debugQuery",
+ "true"));
+ assertThat("sow=" + sow, parsed, matcher);
+ }
+ }
+
// test phrase fields including pf2 pf3 and phrase slop
public void testPfPs() throws Exception {
assertU(adoc("id", "s0", "phrase_sw", "foo bar a b c", "boost_d", "1.0"));