Index: lucene/src/java/org/apache/lucene/search/TermQuery.java =================================================================== --- lucene/src/java/org/apache/lucene/search/TermQuery.java (revision 1051632) +++ lucene/src/java/org/apache/lucene/search/TermQuery.java (working copy) @@ -18,12 +18,14 @@ */ import java.io.IOException; +import java.util.HashSet; import java.util.Set; import org.apache.lucene.index.DocsEnum; import org.apache.lucene.index.Term; import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.Explanation.IDFExplanation; +import org.apache.lucene.util.ReaderUtil; import org.apache.lucene.util.ToStringUtils; /** A Query that matches documents containing a term. @@ -40,12 +42,27 @@ private float queryNorm; private float queryWeight; private IDFExplanation idfExp; - + private Set hash; + public TermWeight(Searcher searcher) throws IOException { this.similarity = getSimilarity(searcher); if (docFreq != -1) { idfExp = similarity.idfExplain(term, searcher, docFreq); + } else if (searcher instanceof IndexSearcher) { + hash = new HashSet(); + IndexReader ir = ((IndexSearcher)searcher).getIndexReader(); + final int dfSum[] = new int[1]; + new ReaderUtil.Gather(ir) { + @Override + protected void add(int base, IndexReader r) throws IOException { + int df = r.docFreq(term); + dfSum[0] += df; + if (df > 0) + hash.add(r.hashCode()); + } + }.run(); + idfExp = similarity.idfExplain(term, searcher, dfSum[0]); } else { idfExp = similarity.idfExplain(term, searcher); } @@ -76,6 +93,9 @@ @Override public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException { + if (hash != null && !hash.contains(reader.hashCode())) + return null; + DocsEnum docs = reader.termDocsEnum(reader.getDeletedDocs(), term.field(), term.bytes());