mirror of https://github.com/apache/lucene.git
LUCENE-3638: add sugar to load only specific fields for document from IR/IS.document
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1214449 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
60929a5adb
commit
69ed5533dd
|
@ -612,6 +612,10 @@ New features
|
|||
(Mike McCandless, Uwe Schindler, Robert Muir, Chris Male, Yonik Seeley,
|
||||
Jason Rutherglen, Paul Elschot)
|
||||
|
||||
* LUCENE-3638: Added sugar methods to IndexReader and IndexSearcher to
|
||||
load only certain fields when loading a document. (Peter Chang via
|
||||
Mike McCandless)
|
||||
|
||||
Optimizations
|
||||
|
||||
* LUCENE-2588: Don't store unnecessary suffixes when writing the terms
|
||||
|
|
|
@ -35,8 +35,8 @@ import org.apache.lucene.store.*;
|
|||
import org.apache.lucene.util.ArrayUtil;
|
||||
import org.apache.lucene.util.Bits;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.MapBackedSet;
|
||||
import org.apache.lucene.util.CommandLineUtil;
|
||||
import org.apache.lucene.util.MapBackedSet;
|
||||
import org.apache.lucene.util.ReaderUtil; // for javadocs
|
||||
|
||||
/** IndexReader is an abstract class, providing an interface for accessing an
|
||||
|
@ -716,6 +716,17 @@ public abstract class IndexReader implements Cloneable,Closeable {
|
|||
return visitor.getDocument();
|
||||
}
|
||||
|
||||
/**
|
||||
* Like {@link #document(int)} but only loads the specified
|
||||
* fields. Note that this is simply sugar for {@link
|
||||
* DocumentStoredFieldVisitor#DocumentStoredFieldVisitor(Set)}.
|
||||
*/
|
||||
public final Document document(int docID, Set<String> fieldsToLoad) throws CorruptIndexException, IOException {
|
||||
final DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor(fieldsToLoad);
|
||||
document(docID, visitor);
|
||||
return visitor.getDocument();
|
||||
}
|
||||
|
||||
/** Returns true if any documents have been deleted */
|
||||
public abstract boolean hasDeletions();
|
||||
|
||||
|
|
|
@ -17,10 +17,10 @@ package org.apache.lucene.search;
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import java.io.Closeable;
|
||||
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;
|
||||
|
@ -176,16 +176,21 @@ public class IndexSearcher {
|
|||
return reader;
|
||||
}
|
||||
|
||||
/* Sugar for <code>.getIndexReader().document(docID)</code> */
|
||||
/** Sugar for <code>.getIndexReader().document(docID)</code> */
|
||||
public Document doc(int docID) throws CorruptIndexException, IOException {
|
||||
return reader.document(docID);
|
||||
}
|
||||
|
||||
/* Sugar for <code>.getIndexReader().document(docID, fieldVisitor)</code> */
|
||||
/** Sugar for <code>.getIndexReader().document(docID, fieldVisitor)</code> */
|
||||
public void doc(int docID, StoredFieldVisitor fieldVisitor) throws CorruptIndexException, IOException {
|
||||
reader.document(docID, fieldVisitor);
|
||||
}
|
||||
|
||||
/** Sugar for <code>.getIndexReader().document(docID, fieldsToLoad)</code> */
|
||||
public final Document document(int docID, Set<String> fieldsToLoad) throws CorruptIndexException, IOException {
|
||||
return reader.document(docID, fieldsToLoad);
|
||||
}
|
||||
|
||||
/** Expert: Set the SimilarityProvider implementation used by this Searcher.
|
||||
*
|
||||
*/
|
||||
|
|
|
@ -26,7 +26,8 @@ import java.util.HashSet;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import org.junit.Assume;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.lucene.analysis.MockAnalyzer;
|
||||
import org.apache.lucene.document.BinaryField;
|
||||
import org.apache.lucene.document.Document;
|
||||
|
@ -35,17 +36,18 @@ import org.apache.lucene.document.FieldType;
|
|||
import org.apache.lucene.document.StringField;
|
||||
import org.apache.lucene.document.TextField;
|
||||
import org.apache.lucene.index.IndexReader.FieldOption;
|
||||
import org.apache.lucene.index.codecs.lucene40.Lucene40PostingsFormat;
|
||||
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
|
||||
import org.apache.lucene.index.codecs.lucene40.Lucene40PostingsFormat;
|
||||
import org.apache.lucene.search.DocIdSetIterator;
|
||||
import org.apache.lucene.search.FieldCache;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.store.LockObtainFailedException;
|
||||
import org.apache.lucene.store.NoSuchDirectoryException;
|
||||
import org.apache.lucene.util.Bits;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
import org.apache.lucene.util._TestUtil;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.Bits;
|
||||
import org.junit.Assume;
|
||||
|
||||
public class TestIndexReader extends LuceneTestCase {
|
||||
|
||||
|
@ -1058,4 +1060,23 @@ public class TestIndexReader extends LuceneTestCase {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testLoadCertainFields() throws Exception {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random, dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newField("field1", "foobar", StringField.TYPE_STORED));
|
||||
doc.add(newField("field2", "foobaz", StringField.TYPE_STORED));
|
||||
writer.addDocument(doc);
|
||||
IndexReader r = writer.getReader();
|
||||
writer.close();
|
||||
Set<String> fieldsToLoad = new HashSet<String>();
|
||||
assertEquals(0, r.document(0, fieldsToLoad).getFields().size());
|
||||
fieldsToLoad.add("field1");
|
||||
Document doc2 = r.document(0, fieldsToLoad);
|
||||
assertEquals(1, doc2.getFields().size());
|
||||
assertEquals("foobar", doc2.get("field1"));
|
||||
r.close();
|
||||
dir.close();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue