Class IndexReader
- java.lang.Object
-
- org.apache.lucene.index.IndexReader
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
CompositeReader
,LeafReader
public abstract class IndexReader extends Object implements Closeable
IndexReader is an abstract class, providing an interface for accessing a point-in-time view of an index. Any changes made to the index viaIndexWriter
will not be visible until a newIndexReader
is opened. It's best to useDirectoryReader.open(IndexWriter)
to obtain anIndexReader
, if yourIndexWriter
is in-process. When you need to re-open to see changes to the index, it's best to useDirectoryReader.openIfChanged(DirectoryReader)
since the new reader will share resources with the previous one when possible. Search of an index is done entirely through this abstract interface, so that any subclass which implements it is searchable.There are two different types of IndexReaders:
LeafReader
: These indexes do not consist of several sub-readers, they are atomic. They support retrieval of stored fields, doc values, terms, and postings.CompositeReader
: Instances (likeDirectoryReader
) of this reader can only be used to get stored fields from the underlying LeafReaders, but it is not possible to directly retrieve postings. To do that, get the sub-readers viaCompositeReader.getSequentialSubReaders()
.
IndexReader instances for indexes on disk are usually constructed with a call to one of the static
DirectoryReader.open()
methods, e.g.DirectoryReader.open(org.apache.lucene.store.Directory)
.DirectoryReader
implements theCompositeReader
interface, it is not possible to directly get postings.For efficiency, in this API documents are often referred to via document numbers, non-negative integers which each name a unique document in the index. These document numbers are ephemeral -- they may change as documents are added to and deleted from an index. Clients should thus not rely on a given document having the same number between sessions.
NOTE:
IndexReader
instances are completely thread safe, meaning multiple threads can call any of its methods, concurrently. If your application requires external synchronization, you should not synchronize on theIndexReader
instance; use your own (non-Lucene) objects instead.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
IndexReader.CacheHelper
A utility class that gives hooks in order to help build a cache based on the data that is contained in this index.static class
IndexReader.CacheKey
A cache key identifying a resource that is being cached on.static interface
IndexReader.ClosedListener
A listener that is called when a resource gets closed.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
close()
Closes files associated with this index.void
decRef()
Expert: decreases the refCount of this IndexReader instance.abstract int
docFreq(Term term)
Returns the number of documents containing theterm
.protected abstract void
doClose()
Implements close.Document
document(int docID)
Deprecated.usestoredFields()
to retrieve one or more documentsDocument
document(int docID, Set<String> fieldsToLoad)
Deprecated.usestoredFields()
to retrieve one or more documentsabstract void
document(int docID, StoredFieldVisitor visitor)
Deprecated.usestoredFields()
to retrieve one or more documentsprotected void
ensureOpen()
Throws AlreadyClosedException if this IndexReader or any of its child readers is closed, otherwise returns.boolean
equals(Object obj)
abstract IndexReaderContext
getContext()
Expert: Returns the rootIndexReaderContext
for thisIndexReader
's sub-reader tree.abstract int
getDocCount(String field)
Returns the number of documents that have at least one term for this field.abstract IndexReader.CacheHelper
getReaderCacheHelper()
Optional method: Return aIndexReader.CacheHelper
that can be used to cache based on the content of this reader.int
getRefCount()
Expert: returns the current refCount for this readerabstract long
getSumDocFreq(String field)
Returns the sum ofTermsEnum.docFreq()
for all terms in this field.abstract long
getSumTotalTermFreq(String field)
Returns the sum ofTermsEnum.totalTermFreq()
for all terms in this field.Terms
getTermVector(int docID, String field)
Deprecated.usetermVectors()
to retrieve one or more documentsabstract Fields
getTermVectors(int docID)
Deprecated.usetermVectors()
to retrieve one or more documentsboolean
hasDeletions()
Returns true if any documents have been deleted.int
hashCode()
void
incRef()
Expert: increments the refCount of this IndexReader instance.List<LeafReaderContext>
leaves()
Returns the reader's leaves, or itself if this reader is atomic.abstract int
maxDoc()
Returns one greater than the largest possible document number.protected void
notifyReaderClosedListeners()
For test framework use only.int
numDeletedDocs()
Returns the number of deleted documents.abstract int
numDocs()
Returns the number of documents in this index.void
registerParentReader(IndexReader reader)
Expert: This method is called byIndexReader
s which wrap other readers (e.g.abstract StoredFields
storedFields()
Returns aStoredFields
reader for the stored fields of this index.abstract TermVectors
termVectors()
Returns aTermVectors
reader for the term vectors of this index.abstract long
totalTermFreq(Term term)
Returns the total number of occurrences ofterm
across all documents (the sum of the freq() for each doc that has this term).boolean
tryIncRef()
Expert: increments the refCount of this IndexReader instance only if the IndexReader has not been closed yet and returnstrue
iff the refCount was successfully incremented, otherwisefalse
.
-
-
-
Method Detail
-
registerParentReader
public final void registerParentReader(IndexReader reader)
Expert: This method is called byIndexReader
s which wrap other readers (e.g.CompositeReader
orFilterLeafReader
) to register the parent at the child (this reader) on construction of the parent. When this reader is closed, it will mark all registered parents as closed, too. The references to parent readers are weak only, so they can be GCed once they are no longer in use.- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
notifyReaderClosedListeners
protected void notifyReaderClosedListeners() throws IOException
For test framework use only.- Throws:
IOException
- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
getRefCount
public final int getRefCount()
Expert: returns the current refCount for this reader
-
incRef
public final void incRef()
Expert: increments the refCount of this IndexReader instance. RefCounts are used to determine when a reader can be closed safely, i.e. as soon as there are no more references. Be sure to always call a correspondingdecRef()
, in a finally clause; otherwise the reader may never be closed. Note thatclose()
simply calls decRef(), which means that the IndexReader will not really be closed untildecRef()
has been called for all outstanding references.- See Also:
decRef()
,tryIncRef()
-
tryIncRef
public final boolean tryIncRef()
Expert: increments the refCount of this IndexReader instance only if the IndexReader has not been closed yet and returnstrue
iff the refCount was successfully incremented, otherwisefalse
. If this method returnsfalse
the reader is either already closed or is currently being closed. Either way this reader instance shouldn't be used by an application unlesstrue
is returned.RefCounts are used to determine when a reader can be closed safely, i.e. as soon as there are no more references. Be sure to always call a corresponding
decRef()
, in a finally clause; otherwise the reader may never be closed. Note thatclose()
simply calls decRef(), which means that the IndexReader will not really be closed untildecRef()
has been called for all outstanding references.
-
decRef
public final void decRef() throws IOException
Expert: decreases the refCount of this IndexReader instance. If the refCount drops to 0, then this reader is closed. If an exception is hit, the refCount is unchanged.- Throws:
IOException
- in case an IOException occurs in doClose()- See Also:
incRef()
-
ensureOpen
protected final void ensureOpen() throws AlreadyClosedException
Throws AlreadyClosedException if this IndexReader or any of its child readers is closed, otherwise returns.- Throws:
AlreadyClosedException
-
equals
public final boolean equals(Object obj)
IndexReader
subclasses are not allowed to implement equals/hashCode, so methods are declared final.
-
hashCode
public final int hashCode()
IndexReader
subclasses are not allowed to implement equals/hashCode, so methods are declared final.
-
getTermVectors
@Deprecated public abstract Fields getTermVectors(int docID) throws IOException
Deprecated.usetermVectors()
to retrieve one or more documentsRetrieve term vectors for this document, or null if term vectors were not indexed. The returned Fields instance acts like a single-document inverted index (the docID will be 0).- Throws:
IOException
-
getTermVector
@Deprecated public final Terms getTermVector(int docID, String field) throws IOException
Deprecated.usetermVectors()
to retrieve one or more documentsRetrieve term vector for this document and field, or null if term vectors were not indexed. The returned Fields instance acts like a single-document inverted index (the docID will be 0).- Throws:
IOException
-
termVectors
public abstract TermVectors termVectors() throws IOException
Returns aTermVectors
reader for the term vectors of this index.This call never returns
null
, even if no term vectors were indexed. The returned instance should only be used by a single thread.Example:
TopDocs hits = searcher.search(query, 10); TermVectors termVectors = reader.termVectors(); for (ScoreDoc hit : hits.scoreDocs) { Fields vector = termVectors.get(hit.doc); }
- Throws:
IOException
- If there is a low-level IO error
-
numDocs
public abstract int numDocs()
Returns the number of documents in this index.NOTE: This operation may run in O(maxDoc). Implementations that can't return this number in constant-time should cache it.
-
maxDoc
public abstract int maxDoc()
Returns one greater than the largest possible document number. This may be used to, e.g., determine how big to allocate an array which will have an element for every document number in an index.
-
numDeletedDocs
public final int numDeletedDocs()
Returns the number of deleted documents.NOTE: This operation may run in O(maxDoc).
-
document
@Deprecated public abstract void document(int docID, StoredFieldVisitor visitor) throws IOException
Deprecated.usestoredFields()
to retrieve one or more documentsExpert: visits the fields of a stored document, for custom processing/loading of each field. If you simply want to load all fields, usedocument(int)
. If you want to load a subset, useDocumentStoredFieldVisitor
.- Throws:
IOException
-
document
@Deprecated public final Document document(int docID) throws IOException
Deprecated.usestoredFields()
to retrieve one or more documentsReturns the stored fields of then
thDocument
in this index. This is just sugar for usingDocumentStoredFieldVisitor
.NOTE: for performance reasons, this method does not check if the requested document is deleted, and therefore asking for a deleted document may yield unspecified results. Usually this is not required, however you can test if the doc is deleted by checking the
Bits
returned fromMultiBits.getLiveDocs(org.apache.lucene.index.IndexReader)
.NOTE: only the content of a field is returned, if that field was stored during indexing. Metadata like boost, omitNorm, IndexOptions, tokenized, etc., are not preserved.
- Throws:
CorruptIndexException
- if the index is corruptIOException
- if there is a low-level IO error
-
document
@Deprecated public final Document document(int docID, Set<String> fieldsToLoad) throws IOException
Deprecated.usestoredFields()
to retrieve one or more documentsLikedocument(int)
but only loads the specified fields. Note that this is simply sugar forDocumentStoredFieldVisitor(Set)
.- Throws:
IOException
-
storedFields
public abstract StoredFields storedFields() throws IOException
Returns aStoredFields
reader for the stored fields of this index.This call never returns
null
, even if no stored fields were indexed. The returned instance should only be used by a single thread.Example:
TopDocs hits = searcher.search(query, 10); StoredFields storedFields = reader.storedFields(); for (ScoreDoc hit : hits.scoreDocs) { Document doc = storedFields.document(hit.doc); }
- Throws:
IOException
- If there is a low-level IO error
-
hasDeletions
public boolean hasDeletions()
-
close
public final void close() throws IOException
Closes files associated with this index. Also saves any new deletions to disk. No other methods should be called after this has been called.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- if there is a low-level IO error
-
doClose
protected abstract void doClose() throws IOException
Implements close.- Throws:
IOException
-
getContext
public abstract IndexReaderContext getContext()
Expert: Returns the rootIndexReaderContext
for thisIndexReader
's sub-reader tree.Iff this reader is composed of sub readers, i.e. this reader being a composite reader, this method returns a
CompositeReaderContext
holding the reader's direct children as well as a view of the reader tree's atomic leaf contexts. All sub-IndexReaderContext
instances referenced from this readers top-level context are private to this reader and are not shared with another context tree. For example, IndexSearcher uses this API to drive searching by one atomic leaf reader at a time. If this reader is not composed of child readers, this method returns anLeafReaderContext
.Note: Any of the sub-
CompositeReaderContext
instances referenced from this top-level context do not supportCompositeReaderContext.leaves()
. Only the top-level context maintains the convenience leaf-view for performance reasons.
-
leaves
public final List<LeafReaderContext> leaves()
Returns the reader's leaves, or itself if this reader is atomic. This is a convenience method callingthis.getContext().leaves()
.- See Also:
IndexReaderContext.leaves()
-
getReaderCacheHelper
public abstract IndexReader.CacheHelper getReaderCacheHelper()
Optional method: Return aIndexReader.CacheHelper
that can be used to cache based on the content of this reader. Two readers that have different data or different sets of deleted documents will be considered different.A return value of
null
indicates that this reader is not suited for caching, which is typically the case for short-lived wrappers that alter the content of the wrapped reader.- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
docFreq
public abstract int docFreq(Term term) throws IOException
Returns the number of documents containing theterm
. This method returns 0 if the term or field does not exists. This method does not take into account deleted documents that have not yet been merged away.- Throws:
IOException
- See Also:
TermsEnum.docFreq()
-
totalTermFreq
public abstract long totalTermFreq(Term term) throws IOException
Returns the total number of occurrences ofterm
across all documents (the sum of the freq() for each doc that has this term). Note that, like other term measures, this measure does not take deleted documents into account.- Throws:
IOException
-
getSumDocFreq
public abstract long getSumDocFreq(String field) throws IOException
Returns the sum ofTermsEnum.docFreq()
for all terms in this field. Note that, just like other term measures, this measure does not take deleted documents into account.- Throws:
IOException
- See Also:
Terms.getSumDocFreq()
-
getDocCount
public abstract int getDocCount(String field) throws IOException
Returns the number of documents that have at least one term for this field. Note that, just like other term measures, this measure does not take deleted documents into account.- Throws:
IOException
- See Also:
Terms.getDocCount()
-
getSumTotalTermFreq
public abstract long getSumTotalTermFreq(String field) throws IOException
Returns the sum ofTermsEnum.totalTermFreq()
for all terms in this field. Note that, just like other term measures, this measure does not take deleted documents into account.- Throws:
IOException
- See Also:
Terms.getSumTotalTermFreq()
-
-