Apache Lucene open-source search software
LUCENE-698: FilteredQuery now takes the query boost into account for scoring.
Michael Buschf5c80265e0d1
diff --git a/CHANGES.txt b/CHANGES.txtindex 260641050e9..fe8a5ab7213 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -147,6 +147,9 @@ Bug fixesanymore and ignored, but re-thrown. Some javadoc improvements.(Daniel Naber)+19. LUCENE-698: FilteredQuery now takes the query boost into account for+ scoring. (Michael Busch)+New features1. LUCENE-759: Added two n-gram-producing TokenFilters.diff --git a/src/java/org/apache/lucene/search/FilteredQuery.java b/src/java/org/apache/lucene/search/FilteredQuery.javaindex 8bebdc10801..d48c4b89b8c 100644--- a/src/java/org/apache/lucene/search/FilteredQuery.java+++ b/src/java/org/apache/lucene/search/FilteredQuery.java@@ -66,13 +66,20 @@ extends Query {final Weight weight = query.createWeight (searcher);final Similarity similarity = query.getSimilarity(searcher);return new Weight() {-+ private float value;+// pass these methods through to enclosed query's weight- public float getValue() { return weight.getValue(); }- public float sumOfSquaredWeights() throws IOException { return weight.sumOfSquaredWeights(); }- public void normalize (float v) { weight.normalize(v); }+ public float getValue() { return value; }+ public float sumOfSquaredWeights() throws IOException {+ return weight.sumOfSquaredWeights() * getBoost() * getBoost();+ }+ public void normalize (float v) {+ weight.normalize(v);+ value = weight.getValue() * getBoost();+ }public Explanation explain (IndexReader ir, int i) throws IOException {Explanation inner = weight.explain (ir, i);+ inner.setValue(getBoost() * inner.getValue());Filter f = FilteredQuery.this.filter;BitSet matches = f.bits(ir);if (matches.get(i))@@ -121,11 +128,13 @@ extends Query {return true;}- public float score() throws IOException { return scorer.score(); }+ public float score() throws IOException { return getBoost() * scorer.score(); }// add an explanation about whether the document was filteredpublic Explanation explain (int i) throws IOException {Explanation exp = scorer.explain (i);+ exp.setValue(getBoost() * exp.getValue());+if (bitset.get(i))exp.setDescription ("allowed by filter: "+exp.getDescription());elsediff --git a/src/test/org/apache/lucene/search/TestFilteredQuery.java b/src/test/org/apache/lucene/search/TestFilteredQuery.javaindex 01733e3a1ac..7ace0f0addd 100644--- a/src/test/org/apache/lucene/search/TestFilteredQuery.java+++ b/src/test/org/apache/lucene/search/TestFilteredQuery.java@@ -24,6 +24,7 @@ import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.Term;+import org.apache.lucene.search.BooleanClause.Occur;import org.apache.lucene.store.RAMDirectory;import java.util.BitSet;@@ -116,7 +117,48 @@ extends TestCase {filteredquery = new FilteredQuery (new TermQuery (new Term ("field", "y")), filter);hits = searcher.search (filteredquery);assertEquals (0, hits.length());- QueryUtils.check(filteredquery,searcher);+ QueryUtils.check(filteredquery,searcher);++ // test boost+ Filter f = new Filter() {+ public BitSet bits (IndexReader reader) {+ BitSet bitset = new BitSet(5);+ bitset.set(0, 5);+ return bitset;+ }+ };++ float boost = 2.5f;+ BooleanQuery bq1 = new BooleanQuery();+ TermQuery tq = new TermQuery (new Term ("field", "one"));+ tq.setBoost(boost);+ bq1.add(tq, Occur.MUST);+ bq1.add(new TermQuery (new Term ("field", "five")), Occur.MUST);++ BooleanQuery bq2 = new BooleanQuery();+ tq = new TermQuery (new Term ("field", "one"));+ filteredquery = new FilteredQuery(tq, f);+ filteredquery.setBoost(boost);+ bq2.add(filteredquery, Occur.MUST);+ bq2.add(new TermQuery (new Term ("field", "five")), Occur.MUST);+ assertScoreEquals(bq1, bq2);++ assertEquals(boost, filteredquery.getBoost(), 0);+ assertEquals(1.0f, tq.getBoost(), 0); // the boost value of the underlying query shouldn't have changed+ }++ /**+ * Tests whether the scores of the two queries are the same.+ */+ public void assertScoreEquals(Query q1, Query q2) throws Exception {+ Hits hits1 = searcher.search (q1);+ Hits hits2 = searcher.search (q2);++ assertEquals(hits1.length(), hits2.length());++ for (int i = 0; i < hits1.length(); i++) {+ assertEquals(hits1.score(i), hits2.score(i), 0.0000001f);+ }}/**@@ -146,3 +188,4 @@ extends TestCase {}}+diff --git a/src/test/org/apache/lucene/search/TestSimpleExplanations.java b/src/test/org/apache/lucene/search/TestSimpleExplanations.javaindex 152135d4f71..ad8c9508e11 100644--- a/src/test/org/apache/lucene/search/TestSimpleExplanations.java+++ b/src/test/org/apache/lucene/search/TestSimpleExplanations.java@@ -119,12 +119,6 @@ public class TestSimpleExplanations extends TestExplanations {q.setBoost(1000);qtest(q, new int[] {3});}- public void testFQ7() throws Exception {- Query q = new FilteredQuery(qp.parse("xx"),- new ItemizedFilter(new int[] {1,3}));- q.setBoost(0);- qtest(q, new int[] {3});- }/* ConstantScoreQueries */