From dfd94b518ac4bb933760987ad74f4ab3b63912e7 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Wed, 25 Feb 2015 14:10:38 +0000 Subject: [PATCH] LUCENE-6227: Fix explanations of FILTER clauses. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1662218 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/lucene/search/BooleanWeight.java | 14 ++++++---- .../lucene/search/TestSimpleExplanations.java | 28 +++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java b/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java index 6ad7397fced..c5b99320698 100644 --- a/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java +++ b/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java @@ -110,6 +110,7 @@ public class BooleanWeight extends Weight { int coord = 0; float sum = 0.0f; boolean fail = false; + int matchCount = 0; int shouldMatchCount = 0; Iterator cIter = query.clauses().iterator(); for (Iterator wIter = weights.iterator(); wIter.hasNext();) { @@ -130,10 +131,10 @@ public class BooleanWeight extends Weight { sum += e.getValue(); coord++; } else if (c.isRequired()) { - Explanation r = - new Explanation(0.0f, "match on required clause (" + c.getQuery().toString() + ")"); - r.addDetail(e); - sumExpl.addDetail(r); + Explanation r = new Explanation(0f, "match on required clause, product of:"); + r.addDetail(new Explanation(0f, Occur.FILTER + " clause")); + r.addDetail(e); + sumExpl.addDetail(r); } else if (c.isProhibited()) { Explanation r = new Explanation(0.0f, "match on prohibited clause (" + c.getQuery().toString() + ")"); @@ -141,6 +142,9 @@ public class BooleanWeight extends Weight { sumExpl.addDetail(r); fail = true; } + if (!c.isProhibited()) { + matchCount++; + } if (c.getOccur() == Occur.SHOULD) { shouldMatchCount++; } @@ -165,7 +169,7 @@ public class BooleanWeight extends Weight { return sumExpl; } - sumExpl.setMatch(0 < coord ? Boolean.TRUE : Boolean.FALSE); + sumExpl.setMatch(0 < matchCount); sumExpl.setValue(sum); final float coordFactor = disableCoord ? 1.0f : coord(coord, maxCoord); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanations.java b/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanations.java index afbabc2d242..2f0cb26392c 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanations.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanations.java @@ -483,6 +483,34 @@ public class TestSimpleExplanations extends BaseExplanationTestCase { } + public void testBQ23() throws Exception { + BooleanQuery query = new BooleanQuery(); + query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.FILTER); + query.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.FILTER); + qtest(query, new int[] { 0,1,2,3 }); + } + + public void testBQ24() throws Exception { + BooleanQuery query = new BooleanQuery(); + query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.FILTER); + query.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD); + qtest(query, new int[] { 0,1,2,3 }); + } + + public void testBQ25() throws Exception { + BooleanQuery query = new BooleanQuery(); + query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.FILTER); + query.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.MUST); + qtest(query, new int[] { 0,1,2,3 }); + } + + public void testBQ26() throws Exception { + BooleanQuery query = new BooleanQuery(); + query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.FILTER); + query.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT); + qtest(query, new int[] { 0,1 }); + } + /* BQ of TQ: using alt so some fields have zero boost and some don't */ public void testMultiFieldBQ1() throws Exception {