From f5c80265e0d1a3e396c2ae7e5cc9a512519b55ea Mon Sep 17 00:00:00 2001 From: Michael Busch Date: Wed, 30 May 2007 23:09:25 +0000 Subject: [PATCH] LUCENE-698: FilteredQuery now takes the query boost into account for scoring. git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@542968 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 3 ++ .../apache/lucene/search/FilteredQuery.java | 19 +++++--- .../lucene/search/TestFilteredQuery.java | 45 ++++++++++++++++++- .../lucene/search/TestSimpleExplanations.java | 6 --- 4 files changed, 61 insertions(+), 12 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 260641050e9..fe8a5ab7213 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -147,6 +147,9 @@ Bug fixes anymore 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 features 1. 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.java index 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 filtered public 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()); else diff --git a/src/test/org/apache/lucene/search/TestFilteredQuery.java b/src/test/org/apache/lucene/search/TestFilteredQuery.java index 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.java index 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 */