IndexReader
IndexReader is an abstract class, providing an interface for accessing a point-in-time view of an index. Any changes made to the index via IndexWriter will not be visible until a new IndexReader is opened. It's best to use DirectoryReader.open to obtain an IndexReader, if your IndexWriter is in-process. When you need to re-open to see changes to the index, it's best to use 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 (like DirectoryReader) 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 via CompositeReader.getSequentialSubReaders.
IndexReader instances for indexes on disk are usually constructed with a call to one of the static DirectoryReader.open() methods, e.g. . DirectoryReader implements the CompositeReader 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 the IndexReader instance; use your own (non-Lucene) objects instead.
Inheritors
Types
A utility class that gives hooks in order to help build a cache based on the data that is contained in this index.
A listener that is called when a resource gets closed.
Properties
Expert: Returns the root IndexReaderContext for this IndexReader's sub-reader tree.
Optional method: Return a 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.
Functions
Throws AlreadyClosedException if this IndexReader or any of its child readers is closed, otherwise returns.
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.
Expert: returns the current refCount for this reader
Returns the sum of TermsEnum.docFreq for all terms in this field. Note that, just like other term measures, this measure does not take deleted documents into account.
Returns the sum of TermsEnum.totalTermFreq for all terms in this field. Note that, just like other term measures, this measure does not take deleted documents into account.
Returns true if any documents have been deleted. Implementers should consider overriding this method if .maxDoc or .numDocs are not constant-time operations.
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 corresponding .decRef, in a finally clause; otherwise the reader may never be closed. Note that .close simply calls decRef(), which means that the IndexReader will not really be closed until .decRef has been called for all outstanding references.
Returns the reader's leaves, or itself if this reader is atomic. This is a convenience method calling this.getContext().leaves().
Returns the number of deleted documents.
Expert: This method is called by IndexReaders which wrap other readers (e.g. [ ] or FilterLeafReader) 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.
Returns a StoredFields reader for the stored fields of this index.
Returns a TermVectors reader for the term vectors of this index.
Returns the total number of occurrences of term 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.
Expert: increments the refCount of this IndexReader instance only if the IndexReader has not been closed yet and returns true iff the refCount was successfully incremented, otherwise false. If this method returns false the reader is either already closed or is currently being closed. Either way this reader instance shouldn't be used by an application unless true is returned.