Index: src/java/org/apache/lucene/search/BooleanQuery.java
===================================================================
--- src/java/org/apache/lucene/search/BooleanQuery.java	(revision 800994)
+++ src/java/org/apache/lucene/search/BooleanQuery.java	(working copy)
@@ -216,7 +216,7 @@
       }
     }
 
-    public Explanation explain(IndexReader reader, int doc)
+    public Explanation explain(IndexSearcher searcher, IndexReader reader, int doc)
       throws IOException {
       final int minShouldMatch =
         BooleanQuery.this.getMinimumNumberShouldMatch();
@@ -233,7 +233,7 @@
         if (w.scorer(reader, true, true) == null) {
           continue;
         }
-        Explanation e = w.explain(reader, doc);
+        Explanation e = w.explain(searcher, reader, doc);
         if (!c.isProhibited()) maxCoord++;
         if (e.isMatch()) {
           if (!c.isProhibited()) {
Index: src/java/org/apache/lucene/search/ConstantScoreQuery.java
===================================================================
--- src/java/org/apache/lucene/search/ConstantScoreQuery.java	(revision 800929)
+++ src/java/org/apache/lucene/search/ConstantScoreQuery.java	(working copy)
@@ -18,6 +18,7 @@
  */
 
 import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.util.ReaderUtil;
 
 import java.io.IOException;
 import java.util.Set;
@@ -81,9 +82,9 @@
       return new ConstantScorer(similarity, reader, this);
     }
 
-    public Explanation explain(IndexReader reader, int doc) throws IOException {
-
-      ConstantScorer cs = (ConstantScorer) scorer(reader, true, false);
+    public Explanation explain(IndexSearcher searcher, IndexReader reader, int doc) throws IOException {
+      
+      ConstantScorer cs = new ConstantScorer(similarity, ReaderUtil.subReader(doc, reader), this);
       boolean exists = cs.docIdSetIterator.advance(doc) == doc;
 
       ComplexExplanation result = new ComplexExplanation();
Index: src/java/org/apache/lucene/search/DisjunctionMaxQuery.java
===================================================================
--- src/java/org/apache/lucene/search/DisjunctionMaxQuery.java	(revision 800929)
+++ src/java/org/apache/lucene/search/DisjunctionMaxQuery.java	(working copy)
@@ -144,13 +144,13 @@
     }
 
     /* Explain the score we computed for doc */
-    public Explanation explain(IndexReader reader, int doc) throws IOException {
-      if (disjuncts.size() == 1) return ((QueryWeight) weights.get(0)).explain(reader,doc);
+    public Explanation explain(IndexSearcher searcher, IndexReader reader, int doc) throws IOException {
+      if (disjuncts.size() == 1) return ((QueryWeight) weights.get(0)).explain(searcher, reader,doc);
       ComplexExplanation result = new ComplexExplanation();
       float max = 0.0f, sum = 0.0f;
       result.setDescription(tieBreakerMultiplier == 0.0f ? "max of:" : "max plus " + tieBreakerMultiplier + " times others of:");
       for (Iterator iter = weights.iterator(); iter.hasNext();) {
-        Explanation e = ((QueryWeight) iter.next()).explain(reader, doc);
+        Explanation e = ((QueryWeight) iter.next()).explain(searcher, reader, doc);
         if (e.isMatch()) {
           result.setMatch(Boolean.TRUE);
           result.addDetail(e);
Index: src/java/org/apache/lucene/search/FilteredQuery.java
===================================================================
--- src/java/org/apache/lucene/search/FilteredQuery.java	(revision 800929)
+++ src/java/org/apache/lucene/search/FilteredQuery.java	(working copy)
@@ -73,8 +73,8 @@
         weight.normalize(v);
         value = weight.getValue() * getBoost();
       }
-      public Explanation explain (IndexReader ir, int i) throws IOException {
-        Explanation inner = weight.explain (ir, i);
+      public Explanation explain (IndexSearcher searcher, IndexReader ir, int i) throws IOException {
+        Explanation inner = weight.explain (searcher, ir, i);
         if (getBoost()!=1) {
           Explanation preBoost = inner;
           inner = new Explanation(inner.getValue()*getBoost(),"product of:");
Index: src/java/org/apache/lucene/search/IndexSearcher.java
===================================================================
--- src/java/org/apache/lucene/search/IndexSearcher.java	(revision 800929)
+++ src/java/org/apache/lucene/search/IndexSearcher.java	(working copy)
@@ -27,6 +27,7 @@
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.ReaderUtil;
 
 /** Implements search over a single IndexReader.
  *
@@ -121,15 +122,7 @@
   }
 
   protected void gatherSubReaders(List allSubReaders, IndexReader r) {
-    IndexReader[] subReaders = r.getSequentialSubReaders();
-    if (subReaders == null) {
-      // Add the reader itself, and do not recurse
-      allSubReaders.add(r);
-    } else {
-      for (int i = 0; i < subReaders.length; i++) {
-        gatherSubReaders(allSubReaders, subReaders[i]);
-      }
-    }
+    ReaderUtil.gatherSubReaders(allSubReaders, r);
   }
 
   /** Return the {@link IndexReader} this searches. */
@@ -317,7 +310,7 @@
   }
 
   public Explanation explain(QueryWeight weight, int doc) throws IOException {
-    return weight.explain(reader, doc);
+    return weight.explain(this, ReaderUtil.subReader(doc, reader), doc);
   }
 
   private boolean fieldSortDoTrackScores;
Index: src/java/org/apache/lucene/search/MatchAllDocsQuery.java
===================================================================
--- src/java/org/apache/lucene/search/MatchAllDocsQuery.java	(revision 800929)
+++ src/java/org/apache/lucene/search/MatchAllDocsQuery.java	(working copy)
@@ -129,7 +129,7 @@
           normsField != null ? reader.norms(normsField) : null);
     }
 
-    public Explanation explain(IndexReader reader, int doc) {
+    public Explanation explain(IndexSearcher searcher, IndexReader reader, int doc) {
       // explain query weight
       Explanation queryExpl = new ComplexExplanation
         (true, getValue(), "MatchAllDocsQuery, product of:");
Index: src/java/org/apache/lucene/search/MultiPhraseQuery.java
===================================================================
--- src/java/org/apache/lucene/search/MultiPhraseQuery.java	(revision 800929)
+++ src/java/org/apache/lucene/search/MultiPhraseQuery.java	(working copy)
@@ -186,7 +186,7 @@
                                       slop, reader.norms(field));
     }
 
-    public Explanation explain(IndexReader reader, int doc)
+    public Explanation explain(IndexSearcher searcher, IndexReader reader, int doc)
       throws IOException {
       ComplexExplanation result = new ComplexExplanation();
       result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");
Index: src/java/org/apache/lucene/search/MultiSearcher.java
===================================================================
--- src/java/org/apache/lucene/search/MultiSearcher.java	(revision 800929)
+++ src/java/org/apache/lucene/search/MultiSearcher.java	(working copy)
@@ -22,6 +22,7 @@
 import org.apache.lucene.index.CorruptIndexException;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.util.ReaderUtil;
 
 import java.io.IOException;
 import java.util.HashMap;
@@ -164,25 +165,7 @@
   /** Returns index of the searcher for document <code>n</code> in the array
    * used to construct this searcher. */
   public int subSearcher(int n) {                 // find searcher for doc n:
-    // replace w/ call to Arrays.binarySearch in Java 1.2
-    int lo = 0;					  // search starts array
-    int hi = searchables.length - 1;		  // for first element less
-						  // than n, return its index
-    while (hi >= lo) {
-      int mid = (lo + hi) >>> 1;
-      int midValue = starts[mid];
-      if (n < midValue)
-	hi = mid - 1;
-      else if (n > midValue)
-	lo = mid + 1;
-      else {                                      // found a match
-        while (mid+1 < searchables.length && starts[mid+1] == midValue) {
-          mid++;                                  // scan to last match
-        }
-	return mid;
-      }
-    }
-    return hi;
+    return ReaderUtil.subIndex(n, searchables.length, starts);
   }
 
   /** Returns the document number of document <code>n</code> within its
Index: src/java/org/apache/lucene/search/PhraseQuery.java
===================================================================
--- src/java/org/apache/lucene/search/PhraseQuery.java	(revision 800929)
+++ src/java/org/apache/lucene/search/PhraseQuery.java	(working copy)
@@ -158,7 +158,7 @@
 
     }
 
-    public Explanation explain(IndexReader reader, int doc)
+    public Explanation explain(IndexSearcher searcher, IndexReader reader, int doc)
       throws IOException {
 
       Explanation result = new Explanation();
Index: src/java/org/apache/lucene/search/QueryWeight.java
===================================================================
--- src/java/org/apache/lucene/search/QueryWeight.java	(revision 800929)
+++ src/java/org/apache/lucene/search/QueryWeight.java	(working copy)
@@ -48,9 +48,22 @@
  */
 public abstract class QueryWeight implements Weight, Serializable {
 
-  /** An explanation of the score computation for the named document. */
-  public abstract Explanation explain(IndexReader reader, int doc) throws IOException;
+  /**
+   * An explanation of the score computation for the named document. 
+   * 
+   * @param searcher
+   * @param reader sub-reader containing the give doc
+   * @param doc
+   * @return an Explanation for the score
+   * @throws IOException
+   */
+  public abstract Explanation explain(IndexSearcher searcher, IndexReader reader, int doc) throws IOException;
 
+  public Explanation explain(IndexReader reader, int doc) throws IOException {
+    return explain(null, reader, doc);
+  }
+
+  
   /** The query that this concerns. */
   public abstract Query getQuery();
 
Index: src/java/org/apache/lucene/search/QueryWeightWrapper.java
===================================================================
--- src/java/org/apache/lucene/search/QueryWeightWrapper.java	(revision 800929)
+++ src/java/org/apache/lucene/search/QueryWeightWrapper.java	(working copy)
@@ -36,7 +36,7 @@
     this.weight = weight;
   }
   
-  public Explanation explain(IndexReader reader, int doc) throws IOException {
+  public Explanation explain(IndexSearcher searcher, IndexReader reader, int doc) throws IOException {
     return weight.explain(reader, doc);
   }
 
Index: src/java/org/apache/lucene/search/TermQuery.java
===================================================================
--- src/java/org/apache/lucene/search/TermQuery.java	(revision 800929)
+++ src/java/org/apache/lucene/search/TermQuery.java	(working copy)
@@ -69,15 +69,19 @@
       return new TermScorer(this, termDocs, similarity, reader.norms(term.field()));
     }
 
-    public Explanation explain(IndexReader reader, int doc)
+    public Explanation explain(IndexSearcher searcher, IndexReader reader, int doc)
       throws IOException {
 
       ComplexExplanation result = new ComplexExplanation();
       result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");
 
-      Explanation idfExpl =
-        new Explanation(idf, "idf(docFreq=" + reader.docFreq(term) +
-            ", numDocs=" + reader.numDocs() + ")");
+      Explanation expl;
+      if(searcher == null) {
+        expl = new Explanation(idf, "idf(" + idf + ")");
+      } else {
+        expl = new Explanation(idf, "idf(docFreq=" + searcher.docFreq(term) +
+            ", numDocs=" + searcher.getIndexReader().numDocs() + ")");
+      }
 
       // explain query weight
       Explanation queryExpl = new Explanation();
@@ -86,13 +90,13 @@
       Explanation boostExpl = new Explanation(getBoost(), "boost");
       if (getBoost() != 1.0f)
         queryExpl.addDetail(boostExpl);
-      queryExpl.addDetail(idfExpl);
+      queryExpl.addDetail(expl);
 
       Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");
       queryExpl.addDetail(queryNormExpl);
 
       queryExpl.setValue(boostExpl.getValue() *
-                         idfExpl.getValue() *
+                         expl.getValue() *
                          queryNormExpl.getValue());
 
       result.addDetail(queryExpl);
@@ -105,7 +109,7 @@
 
       Explanation tfExpl = scorer(reader, true, false).explain(doc);
       fieldExpl.addDetail(tfExpl);
-      fieldExpl.addDetail(idfExpl);
+      fieldExpl.addDetail(expl);
 
       Explanation fieldNormExpl = new Explanation();
       byte[] fieldNorms = reader.norms(field);
@@ -117,7 +121,7 @@
       
       fieldExpl.setMatch(Boolean.valueOf(tfExpl.isMatch()));
       fieldExpl.setValue(tfExpl.getValue() *
-                         idfExpl.getValue() *
+                         expl.getValue() *
                          fieldNormExpl.getValue());
 
       result.addDetail(fieldExpl);
Index: src/java/org/apache/lucene/search/function/CustomScoreQuery.java
===================================================================
--- src/java/org/apache/lucene/search/function/CustomScoreQuery.java	(revision 800929)
+++ src/java/org/apache/lucene/search/function/CustomScoreQuery.java	(working copy)
@@ -23,11 +23,13 @@
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.search.ComplexExplanation;
 import org.apache.lucene.search.Explanation;
+import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.QueryWeight;
 import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.Searcher;
 import org.apache.lucene.search.Similarity;
+import org.apache.lucene.util.ReaderUtil;
 import org.apache.lucene.util.ToStringUtils;
 
 /**
@@ -336,16 +338,39 @@
       }
       Scorer[] valSrcScorers = new Scorer[valSrcWeights.length];
       for(int i = 0; i < valSrcScorers.length; i++) {
-         valSrcScorers[i] = valSrcWeights[i].scorer(reader, true, false);
+         valSrcScorers[i] = valSrcWeights[i].scorer(reader, true, topScorer);
       }
       return new CustomScorer(similarity, reader, this, subQueryScorer, valSrcScorers);
     }
 
-    public Explanation explain(IndexReader reader, int doc) throws IOException {
+    public Explanation explain(IndexSearcher searcher, IndexReader reader, int doc) throws IOException {
       Scorer scorer = scorer(reader, true, false);
-      return scorer == null ? new Explanation(0.0f, "no matching docs") : scorer.explain(doc);
+      return scorer == null ? new Explanation(0.0f, "no matching docs") : doExplain(searcher, reader, doc);
     }
     
+    private Explanation doExplain(IndexSearcher searcher, IndexReader reader, int doc) throws IOException {
+      Scorer[] valSrcScorers = new Scorer[valSrcWeights.length];
+      for(int i = 0; i < valSrcScorers.length; i++) {
+         valSrcScorers[i] = valSrcWeights[i].scorer(reader, true, false);
+      }
+      Explanation subQueryExpl = subQueryWeight.explain(searcher, reader, doc);
+      if (!subQueryExpl.isMatch()) {
+        return subQueryExpl;
+      }
+      // match
+      Explanation[] valSrcExpls = new Explanation[valSrcScorers.length];
+      for(int i = 0; i < valSrcScorers.length; i++) {
+        valSrcExpls[i] = valSrcScorers[i].explain(doc);
+      }
+      Explanation customExp = customExplain(doc,subQueryExpl,valSrcExpls);
+      float sc = getValue() * customExp.getValue();
+      Explanation res = new ComplexExplanation(
+        true, sc, CustomScoreQuery.this.toString() + ", product of:");
+      res.addDetail(customExp);
+      res.addDetail(new Explanation(getValue(), "queryBoost")); // actually using the q boost as q weight (== weight value)
+      return res;
+    }
+
     public boolean scoresDocsOutOfOrder() {
       return false;
     }
@@ -427,7 +452,7 @@
     
     /*(non-Javadoc) @see org.apache.lucene.search.Scorer#explain(int) */
     public Explanation explain(int doc) throws IOException {
-      Explanation subQueryExpl = weight.subQueryWeight.explain(reader,doc);
+      Explanation subQueryExpl = weight.subQueryWeight.explain(null, reader,doc); // nocommit: needs resolution
       if (!subQueryExpl.isMatch()) {
         return subQueryExpl;
       }
Index: src/java/org/apache/lucene/search/function/ValueSourceQuery.java
===================================================================
--- src/java/org/apache/lucene/search/function/ValueSourceQuery.java	(revision 800929)
+++ src/java/org/apache/lucene/search/function/ValueSourceQuery.java	(working copy)
@@ -20,6 +20,7 @@
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.TermDocs;
 import org.apache.lucene.search.*;
+import org.apache.lucene.util.ReaderUtil;
 import org.apache.lucene.util.ToStringUtils;
 
 import java.io.IOException;
@@ -62,7 +63,7 @@
     // no terms involved here
   }
 
-  private class ValueSourceWeight extends QueryWeight {
+  class ValueSourceWeight extends QueryWeight {
     Similarity similarity;
     float queryNorm;
     float queryWeight;
@@ -92,14 +93,14 @@
       this.queryNorm = norm;
       queryWeight *= this.queryNorm;
     }
-
+    
     public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
       return new ValueSourceScorer(similarity, reader, this);
     }
 
     /*(non-Javadoc) @see org.apache.lucene.search.Weight#explain(org.apache.lucene.index.IndexReader, int) */
-    public Explanation explain(IndexReader reader, int doc) throws IOException {
-      return scorer(reader, true, false).explain(doc);
+    public Explanation explain(IndexSearcher searcher, IndexReader reader, int doc) throws IOException {
+      return new ValueSourceScorer(similarity, reader, this).explain(doc);
     }
   }
 
Index: src/java/org/apache/lucene/search/spans/SpanWeight.java
===================================================================
--- src/java/org/apache/lucene/search/spans/SpanWeight.java	(revision 800929)
+++ src/java/org/apache/lucene/search/spans/SpanWeight.java	(working copy)
@@ -68,7 +68,7 @@
         .norms(query.getField()));
   }
 
-  public Explanation explain(IndexReader reader, int doc)
+  public Explanation explain(IndexSearcher searcher, IndexReader reader, int doc)
     throws IOException {
 
     ComplexExplanation result = new ComplexExplanation();
Index: src/java/org/apache/lucene/util/ReaderUtil.java
===================================================================
--- src/java/org/apache/lucene/util/ReaderUtil.java	(revision 0)
+++ src/java/org/apache/lucene/util/ReaderUtil.java	(revision 0)
@@ -0,0 +1,87 @@
+package org.apache.lucene.util;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.lucene.index.IndexReader;
+
+public class ReaderUtil {
+
+  /**
+   * Gathers sub-readers from reader into a List.
+   * 
+   * @param allSubReaders
+   * @param reader
+   */
+  public static void gatherSubReaders(List allSubReaders, IndexReader reader) {
+    IndexReader[] subReaders = reader.getSequentialSubReaders();
+    if (subReaders == null) {
+      // Add the reader itself, and do not recurse
+      allSubReaders.add(reader);
+    } else {
+      for (int i = 0; i < subReaders.length; i++) {
+        gatherSubReaders(allSubReaders, subReaders[i]);
+      }
+    }
+  }
+
+  /**
+   * Returns sub IndexReader that contains the given document id.
+   */
+  public static IndexReader subReader(int doc, IndexReader reader) {
+    List subReadersList = new ArrayList();
+    ReaderUtil.gatherSubReaders(subReadersList, reader);
+    IndexReader[] subReaders = (IndexReader[]) subReadersList
+        .toArray(new IndexReader[subReadersList.size()]);
+    int[] docStarts = new int[subReaders.length];
+    int maxDoc = 0;
+    for (int i = 0; i < subReaders.length; i++) {
+      docStarts[i] = maxDoc;
+      maxDoc += subReaders[i].maxDoc();
+    }
+    return subReaders[ReaderUtil.subIndex(doc, subReaders.length, docStarts)];
+  }
+
+  /**
+   * Returns index of the searcher/reader for document <code>n</code> in the
+   * array used to construct this searcher/reader.
+   */
+  public static int subIndex(int n, int size, int[] docStarts) { // find
+    // searcher/reader for doc n:
+
+    int lo = 0; // search starts array
+    int hi = size - 1; // for first element less than n, return its index
+    while (hi >= lo) {
+      int mid = (lo + hi) >>> 1;
+      int midValue = docStarts[mid];
+      if (n < midValue)
+        hi = mid - 1;
+      else if (n > midValue)
+        lo = mid + 1;
+      else { // found a match
+        while (mid + 1 < size && docStarts[mid + 1] == midValue) {
+          mid++; // scan to last match
+        }
+        return mid;
+      }
+    }
+    return hi;
+  }
+}
Index: src/test/org/apache/lucene/search/JustCompileSearch.java
===================================================================
--- src/test/org/apache/lucene/search/JustCompileSearch.java	(revision 800929)
+++ src/test/org/apache/lucene/search/JustCompileSearch.java	(working copy)
@@ -429,7 +429,7 @@
 
   static final class JustCompileWeight extends QueryWeight {
 
-    public Explanation explain(IndexReader reader, int doc) throws IOException {
+    public Explanation explain(IndexSearcher searcher, IndexReader reader, int doc) throws IOException {
       throw new UnsupportedOperationException(UNSUPPORTED_MSG);
     }
 

