Index: src/java/org/apache/lucene/search/IndexSearcher.java =================================================================== --- src/java/org/apache/lucene/search/IndexSearcher.java (版本 1211892) +++ src/java/org/apache/lucene/search/IndexSearcher.java (工作副本) @@ -21,6 +21,7 @@ import java.io.IOException; import java.util.Iterator; import java.util.NoSuchElementException; +import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutionException; @@ -180,9 +181,14 @@ /* Sugar for .getIndexReader().document(docID) */ public Document doc(int docID) throws CorruptIndexException, IOException { - return reader.document(docID); + return reader.document(docID, null, null); } + /** see {@link IndexReader#document(int, Set, Set)} for detail*/ + public Document doc(int docID, Set fieldsToAdd, Set fieldsToFilter) throws CorruptIndexException, IOException { + return reader.document(docID, fieldsToAdd, fieldsToFilter); + } + /* Sugar for .getIndexReader().document(docID, fieldVisitor) */ public void doc(int docID, StoredFieldVisitor fieldVisitor) throws CorruptIndexException, IOException { reader.document(docID, fieldVisitor); Index: src/java/org/apache/lucene/index/IndexReader.java =================================================================== --- src/java/org/apache/lucene/index/IndexReader.java (版本 1211892) +++ src/java/org/apache/lucene/index/IndexReader.java (工作副本) @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import org.apache.lucene.document.Document; @@ -882,11 +883,22 @@ // Document returned here contains that class not // IndexableField public Document document(int docID) throws CorruptIndexException, IOException { + return document(docID, null, null); + } + + /** + * Manipulate the specific fields to be loaded with two sets. + *

The fieldsToFilter is provided because there is usually less fields carry huge data. + * And in this scenario it needs not to check other fields

+ * @param fieldsToAdd fields to be loaded + * @param fieldsToFilter fields to be filtered + */ + public Document document(int docID, final Set fieldsToAdd, final Set fieldsToFilter) throws CorruptIndexException, IOException { ensureOpen(); if (docID < 0 || docID >= maxDoc()) { throw new IllegalArgumentException("docID must be >= 0 and < maxDoc=" + maxDoc() + " (got docID=" + docID + ")"); } - final DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor(); + final DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor(fieldsToAdd, fieldsToFilter); document(docID, visitor); return visitor.getDocument(); } Index: src/java/org/apache/lucene/document/DocumentStoredFieldVisitor.java =================================================================== --- src/java/org/apache/lucene/document/DocumentStoredFieldVisitor.java (版本 1211892) +++ src/java/org/apache/lucene/document/DocumentStoredFieldVisitor.java (工作副本) @@ -35,11 +35,15 @@ public class DocumentStoredFieldVisitor extends StoredFieldVisitor { private final Document doc = new Document(); + /*TODO: should it remove fieldsToAdd ? */ private final Set fieldsToAdd; + private final Set fieldsToFilter; - /** Load only fields named in the provided Set<String>. */ - public DocumentStoredFieldVisitor(Set fieldsToAdd) { + /** Load only fields named in the provided Set<String>. + * And do not load fields in provided List<String> */ + public DocumentStoredFieldVisitor(Set fieldsToAdd, Set fieldsToFilter) { this.fieldsToAdd = fieldsToAdd; + this.fieldsToFilter = fieldsToFilter; } /** Load only fields named in the provided Set<String>. */ @@ -48,11 +52,13 @@ for(String field : fields) { fieldsToAdd.add(field); } + this.fieldsToFilter = null; } /** Load all stored fields. */ public DocumentStoredFieldVisitor() { this.fieldsToAdd = null; + this.fieldsToFilter = null; } @Override @@ -103,7 +109,9 @@ @Override public Status needsField(FieldInfo fieldInfo) throws IOException { - return fieldsToAdd == null || fieldsToAdd.contains(fieldInfo.name) ? Status.YES : Status.NO; + return (fieldsToFilter == null || !fieldsToFilter.contains(fieldInfo.name)) && + (fieldsToAdd == null || fieldsToAdd.contains(fieldInfo.name)) ? + Status.YES : Status.NO; } public Document getDocument() {