lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From busc...@apache.org
Subject svn commit: r542968 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/search/FilteredQuery.java src/test/org/apache/lucene/search/TestFilteredQuery.java src/test/org/apache/lucene/search/TestSimpleExplanations.java
Date Wed, 30 May 2007 23:09:26 GMT
Author: buschmi
Date: Wed May 30 16:09:25 2007
New Revision: 542968

URL: http://svn.apache.org/viewvc?view=rev&rev=542968
Log:
LUCENE-698: FilteredQuery now takes the query boost into account for scoring.

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestFilteredQuery.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanations.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?view=diff&rev=542968&r1=542967&r2=542968
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Wed May 30 16:09:25 2007
@@ -147,6 +147,9 @@
     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.

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java?view=diff&rev=542968&r1=542967&r2=542968
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java Wed May 30 16:09:25
2007
@@ -66,13 +66,20 @@
     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 @@
             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

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestFilteredQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestFilteredQuery.java?view=diff&rev=542968&r1=542967&r2=542968
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestFilteredQuery.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestFilteredQuery.java Wed May 30
16:09:25 2007
@@ -24,6 +24,7 @@
 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 @@
     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);
+    }
   }
 
   /**
@@ -145,4 +187,5 @@
     QueryUtils.check(query,searcher);    
   }
 }
+
 

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanations.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanations.java?view=diff&rev=542968&r1=542967&r2=542968
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanations.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanations.java Wed May
30 16:09:25 2007
@@ -119,12 +119,6 @@
     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 */
   



Mime
View raw message