Here's a diff to ConstantScoreQuery that optionally folds in norms
(minus explain() functionality right now).
Should it be added, or do the differences warrant a new Query class,
or if kept together, should ConstantScoreQuery be renamed since it's
not quite so constant?

-Yonik
Now hiring -- http://forms.cnet.com/slink?231706


--- src/java/org/apache/lucene/search/ConstantScoreQuery.java   (revision 
344312)
+++ src/java/org/apache/lucene/search/ConstantScoreQuery.java   (working copy)
@@ -23,16 +23,29 @@

 /**
  * A query that wraps a filter and simply returns a constant score equal to the
- * query boost for every document in the filter.
+ * query boost for every document in the filter.  Field boosts and field length
+ * normalization factors may be enabled by providing a field to use.
  *
  * @author yonik
  * @version $Id$
  */
 public class ConstantScoreQuery extends Query {
-  protected final Filter filter;
+  protected Filter filter;
+  protected String field;

-  public ConstantScoreQuery(Filter filter) {
+  /**
+   * @param filter the set of documents to produce a score for
+   * @param field If non-null, this field's norms are
+   * multiplied into the score, effectively enabling index time field boosts
+   * and field length normalization.
+   */
+  public ConstantScoreQuery(Filter filter, String field) {
     this.filter=filter;
+    this.field = field;
+  }
+
+  public ConstantScoreQuery(Filter filter) {
+    this(filter, null);
   }

   public Query rewrite(IndexReader reader) throws IOException {
@@ -67,7 +80,9 @@
     }

     public Scorer scorer(IndexReader reader) throws IOException {
-      return new ConstantScorer(getSimilarity(searcher), reader, this);
+      return new ConstantScorer(getSimilarity(searcher), reader, this,
+              (field!=null && reader.hasNorms(field)) ?
reader.norms(field) : null
+              );
     }

     public Explanation explain(IndexReader reader, int doc) throws
IOException {
@@ -95,12 +110,16 @@
   protected class ConstantScorer extends Scorer {
     final BitSet bits;
     final float theScore;
+    final byte[] norms;
+    final float[] normDecoder = Similarity.getNormDecoder();
+
     int doc=-1;

-    public ConstantScorer(Similarity similarity, IndexReader reader,
Weight w) throws IOException {
+    public ConstantScorer(Similarity similarity, IndexReader reader,
Weight w, byte[] norms) throws IOException {
       super(similarity);
       theScore = w.getValue();
       bits = filter.bits(reader);
+      this.norms = norms;
     }

     public boolean next() throws IOException {
@@ -113,7 +132,7 @@
     }

     public float score() throws IOException {
-      return theScore;
+      return norms==null ? theScore : theScore*normDecoder[norms[doc]&0xFF];
     }

     public boolean skipTo(int target) throws IOException {

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to