Apache Lucene open-source search software
BooleanWeight.explain prints failing optional clauses (when required) (#16229)
Jakub Slowinski7c587afd540b
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex fd9092a1a7d..978f361159f 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -388,6 +388,8 @@ Improvements* GITHUB#16144: Clarify LeafCollector batch collection scoring contract. (Costin Leau)+* GITHUB#16229: BooleanWeight.explain prints failing optional clauses when query fails due to insufficient SHOULD matches. (Jakub Slowinski)+Optimizations---------------------* GITHUB#16222: MultiTermQuery constant-score wrapper now defers term collection to ScorerSupplier#get()diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java b/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.javaindex 08fc1296d10..f2be8ff12bd 100644--- a/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java+++ b/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java@@ -65,6 +65,7 @@ final class BooleanWeight extends Weight {public Explanation explain(LeafReaderContext context, int doc) throws IOException {final int minShouldMatch = query.getMinimumNumberShouldMatch();List<Explanation> subs = new ArrayList<>();+ List<Explanation> failingOptionals = new ArrayList<>();boolean fail = false;int matchCount = 0;int shouldMatchCount = 0;@@ -97,16 +98,25 @@ final class BooleanWeight extends Weight {subs.add(Explanation.noMatch("no match on required clause (" + c.query().toString() + ")", e));fail = true;+ } else if (c.occur() == Occur.SHOULD) {+ failingOptionals.add(+ Explanation.noMatch("no match on optional clause (" + c.query().toString() + ")", e));}}if (fail) {return Explanation.noMatch("Failure to meet condition(s) of required/prohibited clause(s)", subs);} else if (matchCount == 0) {+ subs.addAll(failingOptionals);return Explanation.noMatch("No matching clauses", subs);} else if (shouldMatchCount < minShouldMatch) {+ subs.addAll(failingOptionals);return Explanation.noMatch(- "Failure to match minimum number of optional clauses: " + minShouldMatch, subs);+ "Failure to match minimum number of optional clauses: "+ + minShouldMatch+ + ", matched: "+ + shouldMatchCount,+ subs);} else {// Replicating the same floating-point errors as the scorer does is quite// complex (essentially because of how ReqOptSumScorer casts intermediatediff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.javaindex b0ed0b89a5f..37993a32e5f 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java@@ -1395,7 +1395,7 @@ public class TestBooleanQuery extends LuceneTestCase {bqBuilder.add(new TermQuery(d), Occur.MUST_NOT);bqBuilder.add(new TermQuery(d), Occur.MUST_NOT);BooleanQuery bq = bqBuilder.build();- // should and must are not dedupliacated+ // should and must are not deduplicatedassertEquals(2, bq.getClauses(Occur.SHOULD).size());assertEquals(2, bq.getClauses(Occur.MUST).size());// filter and must not are deduplicateddiff --git a/lucene/core/src/test/org/apache/lucene/search/TestComplexExplanations.java b/lucene/core/src/test/org/apache/lucene/search/TestComplexExplanations.javaindex b0aa9f663ff..ee2d07e6087 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestComplexExplanations.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestComplexExplanations.java@@ -145,4 +145,39 @@ public class TestComplexExplanations extends BaseExplanationTestCase {bqtest(new BoostQuery(query, 0), new int[] {0, 1, 2, 3});}++ public void testExplainFailingOptionalClauses() throws Exception {+ // minShouldMatch not satisfied, show failing SHOULDs.+ BooleanQuery msm =+ new BooleanQuery.Builder()+ .add(new TermQuery(new Term(FIELD, "w1")), Occur.SHOULD)+ .add(new TermQuery(new Term(FIELD, "xx")), Occur.SHOULD)+ .add(new TermQuery(new Term(FIELD, "zz")), Occur.SHOULD)+ .setMinimumNumberShouldMatch(2)+ .build();+ String msmExpl = searcher.explain(msm, 0).toString();+ assertTrue(+ msmExpl.contains("Failure to match minimum number of optional clauses: 2, matched: 1"));+ assertTrue(msmExpl.contains("no match on optional clause (field:xx)"));+ assertTrue(msmExpl.contains("no match on optional clause (field:zz)"));+ assertFalse(msmExpl.contains("no match on optional clause (field:w1)"));++ // nothing matches in disjunction, show failing SHOULDs.+ BooleanQuery disj =+ new BooleanQuery.Builder()+ .add(new TermQuery(new Term(FIELD, "xx")), Occur.SHOULD)+ .add(new TermQuery(new Term(FIELD, "zz")), Occur.SHOULD)+ .build();+ String disjExpl = searcher.explain(disj, 0).toString();+ assertTrue(disjExpl.contains("no match on optional clause (field:xx)"));+ assertTrue(disjExpl.contains("no match on optional clause (field:zz)"));++ // MUST clause fails, failing SHOULDs are not shown.+ BooleanQuery mustFail =+ new BooleanQuery.Builder()+ .add(new TermQuery(new Term(FIELD, "missing")), Occur.MUST)+ .add(new TermQuery(new Term(FIELD, "xx")), Occur.SHOULD)+ .build();+ assertFalse(searcher.explain(mustFail, 0).toString().contains("no match on optional clause"));+ }}