lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r756029 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/search/MatchAllDocsQuery.java src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java
Date Thu, 19 Mar 2009 14:40:56 GMT
Author: mikemccand
Date: Thu Mar 19 14:40:55 2009
New Revision: 756029

URL: http://svn.apache.org/viewvc?rev=756029&view=rev
Log:
LUCENE-1543: allow MatchAllDocsQuery to specify a field whose norms are used to set document
scores

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=756029&r1=756028&r2=756029&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Thu Mar 19 14:40:55 2009
@@ -183,6 +183,10 @@
     maintain sort order of the original byte[] when the bytes are
     interpreted as unsigned.  (Steven Rowe via Mike McCandless)
 
+18. LUCENE-1543: Allow MatchAllDocsQuery to optionally use norms from
+    a specific fields to set the score for a document.  (Karl Wettin
+    via Mike McCandless)
+
 Optimizations
 
  1. LUCENE-1427: Fixed QueryWrapperFilter to not waste time computing

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java?rev=756029&r1=756028&r2=756029&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java Thu Mar 19
14:40:55 2009
@@ -31,17 +31,29 @@
 public class MatchAllDocsQuery extends Query {
 
   public MatchAllDocsQuery() {
+    this(null);
+  }
+
+  private final String normsField;
+
+  /**
+   * @param normsField Field used for normalization factor (document boost). Null if nothing.
+   */
+  public MatchAllDocsQuery(String normsField) {
+    this.normsField = normsField;
   }
 
   private class MatchAllScorer extends Scorer {
     final TermDocs termDocs;
     final float score;
+    final byte[] norms;
 
-    MatchAllScorer(IndexReader reader, Similarity similarity, Weight w) throws IOException
+    MatchAllScorer(IndexReader reader, Similarity similarity, Weight w, byte[] norms) throws
IOException
     {
       super(similarity);
       this.termDocs = reader.termDocs(null);
       score = w.getValue();
+      this.norms = norms;
     }
 
     public Explanation explain(int doc) {
@@ -57,7 +69,11 @@
     }
 
     public float score() {
-      return score;
+      if (norms == null) {
+        return score;
+      } else {
+        return score * Similarity.decodeNorm(norms[doc()]); // normalize for field
+      }
     }
 
     public boolean skipTo(int target) throws IOException {
@@ -98,7 +114,8 @@
     }
 
     public Scorer scorer(IndexReader reader) throws IOException {
-      return new MatchAllScorer(reader, similarity, this);
+      return new MatchAllScorer(reader, similarity, this,
+          normsField != null ? reader.norms(normsField) : null);
     }
 
     public Explanation explain(IndexReader reader, int doc) {

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java?rev=756029&r1=756028&r2=756029&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java Thu Mar
19 14:40:55 2009
@@ -23,6 +23,7 @@
 import org.apache.lucene.document.Field;
 import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.index.Term;
+import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.store.RAMDirectory;
 
 import org.apache.lucene.util.LuceneTestCase;
@@ -34,18 +35,47 @@
 public class TestMatchAllDocsQuery extends LuceneTestCase {
 
   public void testQuery() throws IOException {
+
     RAMDirectory dir = new RAMDirectory();
     IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
     iw.setMaxBufferedDocs(2);  // force multi-segment
-    addDoc("one", iw);
-    addDoc("two", iw);
-    addDoc("three four", iw);
+    addDoc("one", iw, 1f);
+    addDoc("two", iw, 20f);
+    addDoc("three four", iw, 300f);
     iw.close();
-    
-    IndexSearcher is = new IndexSearcher(dir);
-    ScoreDoc[] hits = is.search(new MatchAllDocsQuery(), null, 1000).scoreDocs;
+
+    IndexReader ir = IndexReader.open(dir);
+    IndexSearcher is = new IndexSearcher(ir);
+    ScoreDoc[] hits;
+
+    // assert with norms scoring turned off
+
+    hits = is.search(new MatchAllDocsQuery(), null, 1000).scoreDocs;
     assertEquals(3, hits.length);
+    assertEquals("one", ir.document(hits[0].doc).get("key"));
+    assertEquals("two", ir.document(hits[1].doc).get("key"));
+    assertEquals("three four", ir.document(hits[2].doc).get("key"));
 
+    // assert with norms scoring turned on
+
+    MatchAllDocsQuery normsQuery = new MatchAllDocsQuery("key");
+    hits = is.search(normsQuery, null, 1000).scoreDocs;
+    assertEquals(3, hits.length);
+
+    assertEquals("three four", ir.document(hits[0].doc).get("key"));    
+    assertEquals("two", ir.document(hits[1].doc).get("key"));
+    assertEquals("one", ir.document(hits[2].doc).get("key"));
+
+    // change norm & retest
+    ir.setNorm(0, "key", 400f);
+    normsQuery = new MatchAllDocsQuery("key");
+    hits = is.search(normsQuery, null, 1000).scoreDocs;
+    assertEquals(3, hits.length);
+
+    assertEquals("one", ir.document(hits[0].doc).get("key"));
+    assertEquals("three four", ir.document(hits[1].doc).get("key"));    
+    assertEquals("two", ir.document(hits[2].doc).get("key"));
+    
     // some artificial queries to trigger the use of skipTo():
     
     BooleanQuery bq = new BooleanQuery();
@@ -66,6 +96,8 @@
     assertEquals(2, hits.length);
     
     is.close();
+    ir.close();
+    dir.close();
   }
 
   public void testEquals() {
@@ -76,9 +108,11 @@
     assertFalse(q1.equals(q2));
   }
   
-  private void addDoc(String text, IndexWriter iw) throws IOException {
+  private void addDoc(String text, IndexWriter iw, float boost) throws IOException {
     Document doc = new Document();
-    doc.add(new Field("key", text, Field.Store.YES, Field.Index.ANALYZED));
+    Field f = new Field("key", text, Field.Store.YES, Field.Index.ANALYZED);
+    f.setBoost(boost);
+    doc.add(f);
     iw.addDocument(doc);
   }
 



Mime
View raw message