org.apache.lucene.index

## Class LeafReader

- java.lang.Object
- - org.apache.lucene.index.IndexReader
  - - org.apache.lucene.index.LeafReader

- All Implemented Interfaces:
    Closeable, AutoCloseable
  Direct Known Subclasses:
    CodecReader, FilterLeafReader, ParallelLeafReader, SlowCompositeReaderWrapper

      public abstract class LeafReader
      extends IndexReader

  `LeafReader` is an abstract class, providing an interface for accessing an index. Search of an index is done entirely through this abstract interface, so that any subclass which implements it is searchable. IndexReaders implemented by this subclass do not consist of several sub-readers, they are atomic. They support retrieval of stored fields, doc values, terms, and 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.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `LeafReader.CoreClosedListener` Called when the shared core for this `LeafReader` is closed.

    - ### Nested classes/interfaces inherited from class org.apache.lucene.index.IndexReader

      `IndexReader.ReaderClosedListener`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `LeafReader()` Sole constructor.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `abstract void` | `addCoreClosedListener(LeafReader.CoreClosedListener listener)` Expert: adds a CoreClosedListener to this reader's shared core
    `protected static void` | `addCoreClosedListenerAsReaderClosedListener(IndexReader reader, LeafReader.CoreClosedListener listener)` Add a `LeafReader.CoreClosedListener` as a `IndexReader.ReaderClosedListener`.
    `abstract void` | `checkIntegrity()` Checks consistency of this reader.
    `int` | `docFreq(Term term)` Returns the number of documents containing the `term`.
    `abstract Fields` | `fields()` Returns `Fields` for this reader.
    `abstract BinaryDocValues` | `getBinaryDocValues(String field)` Returns `BinaryDocValues` for this field, or null if no `BinaryDocValues` were indexed for this field.
    `LeafReaderContext` | `getContext()` Expert: Returns the root `IndexReaderContext` for this `IndexReader`'s sub-reader tree.
    `int` | `getDocCount(String field)` Returns the number of documents that have at least one term for this field, or -1 if this measure isn't stored by the codec.
    `abstract Bits` | `getDocsWithField(String field)` Returns a `Bits` at the size of `reader.maxDoc()`, with turned on bits for each docid that does have a value for this field, or null if no DocValues were indexed for this field.
    `abstract FieldInfos` | `getFieldInfos()` Get the `FieldInfos` describing all fields in this reader.
    `abstract Bits` | `getLiveDocs()` Returns the `Bits` representing live (not deleted) docs.
    `abstract NumericDocValues` | `getNormValues(String field)` Returns `NumericDocValues` representing norms for this field, or null if no `NumericDocValues` were indexed.
    `abstract NumericDocValues` | `getNumericDocValues(String field)` Returns `NumericDocValues` for this field, or null if no `NumericDocValues` were indexed for this field.
    `abstract SortedDocValues` | `getSortedDocValues(String field)` Returns `SortedDocValues` for this field, or null if no `SortedDocValues` were indexed for this field.
    `abstract SortedNumericDocValues` | `getSortedNumericDocValues(String field)` Returns `SortedNumericDocValues` for this field, or null if no `SortedNumericDocValues` were indexed for this field.
    `abstract SortedSetDocValues` | `getSortedSetDocValues(String field)` Returns `SortedSetDocValues` for this field, or null if no `SortedSetDocValues` were indexed for this field.
    `long` | `getSumDocFreq(String field)` Returns the sum of `TermsEnum.docFreq()` for all terms in this field, or -1 if this measure isn't stored by the codec.
    `long` | `getSumTotalTermFreq(String field)` Returns the sum of `TermsEnum.totalTermFreq()` for all terms in this field, or -1 if this measure isn't stored by the codec (or if this fields omits term freq and positions).
    `PostingsEnum` | `postings(Term term)` Returns `PostingsEnum` for the specified term with `PostingsEnum.FREQS`.
    `PostingsEnum` | `postings(Term term, int flags)` Returns `PostingsEnum` for the specified term.
    `abstract void` | `removeCoreClosedListener(LeafReader.CoreClosedListener listener)` Expert: removes a CoreClosedListener from this reader's shared core
    `protected static void` | `removeCoreClosedListenerAsReaderClosedListener(IndexReader reader, LeafReader.CoreClosedListener listener)` Remove a `LeafReader.CoreClosedListener` which has been added with `addCoreClosedListenerAsReaderClosedListener(IndexReader, CoreClosedListener)`.
    `DocsEnum` | `termDocsEnum(Term term)` Deprecated. use `postings(Term)` instead
    `DocsAndPositionsEnum` | `termPositionsEnum(Term term)` Deprecated. use `postings(Term, int)` instead
    `Terms` | `terms(String field)` This may return null if the field does not exist.
    `long` | `totalTermFreq(Term term)` Returns the number of documents containing the term `t`.

    - ### Methods inherited from class org.apache.lucene.index.IndexReader

      `addReaderClosedListener, close, decRef, doClose, document, document, document, ensureOpen, equals, getCombinedCoreAndDeletesKey, getCoreCacheKey, getRefCount, getTermVector, getTermVectors, hasDeletions, hashCode, incRef, leaves, maxDoc, numDeletedDocs, numDocs, registerParentReader, removeReaderClosedListener, tryIncRef`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### LeafReader

          protected LeafReader()

      Sole constructor. (For invocation by subclass constructors, typically implicit.)

  - ### Method Detail

    - #### getContext

          public final LeafReaderContext getContext()

      Description copied from class: `IndexReader`
      Expert: Returns the root `IndexReaderContext` for this `IndexReader`'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 an `LeafReaderContext`.

      Note: Any of the sub-`CompositeReaderContext` instances referenced from this top-level context do not support `CompositeReaderContext.leaves()`. Only the top-level context maintains the convenience leaf-view for performance reasons.

      Specified by:
        `getContext` in class `IndexReader`

    - #### addCoreClosedListenerAsReaderClosedListener

          protected static void addCoreClosedListenerAsReaderClosedListener(IndexReader reader,
                                                         LeafReader.CoreClosedListener listener)

      Add a `LeafReader.CoreClosedListener` as a `IndexReader.ReaderClosedListener`. This method is typically useful for `LeafReader` implementations that don't have the concept of a core that is shared across several `LeafReader` instances in which case the `LeafReader.CoreClosedListener` is called when closing the reader.

    - #### removeCoreClosedListenerAsReaderClosedListener

          protected static void removeCoreClosedListenerAsReaderClosedListener(IndexReader reader,
                                                            LeafReader.CoreClosedListener listener)

      Remove a `LeafReader.CoreClosedListener` which has been added with `addCoreClosedListenerAsReaderClosedListener(IndexReader, CoreClosedListener)`.

    - #### addCoreClosedListener

          public abstract void addCoreClosedListener(LeafReader.CoreClosedListener listener)

      Expert: adds a CoreClosedListener to this reader's shared core
      WARNING: This API is experimental and might change in incompatible ways in the next release.

    - #### removeCoreClosedListener

          public abstract void removeCoreClosedListener(LeafReader.CoreClosedListener listener)

      Expert: removes a CoreClosedListener from this reader's shared core
      WARNING: This API is experimental and might change in incompatible ways in the next release.

    - #### fields

          public abstract Fields fields()
                                 throws IOException

      Returns `Fields` for this reader. This method will not return null.
      Throws:
        `IOException`

    - #### docFreq

          public final int docFreq(Term term)
                            throws IOException

      Description copied from class: `IndexReader`
      Returns the number of documents containing the `term`. 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.
      Specified by:
        `docFreq` in class `IndexReader`
      Throws:
        `IOException`
      See Also:
        `TermsEnum.docFreq()`

    - #### totalTermFreq

          public final long totalTermFreq(Term term)
                                   throws IOException

      Returns the number of documents containing the term `t`. 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.
      Specified by:
        `totalTermFreq` in class `IndexReader`
      Throws:
        `IOException`

    - #### getSumDocFreq

          public final long getSumDocFreq(String field)
                                   throws IOException

      Description copied from class: `IndexReader`
      Returns the sum of `TermsEnum.docFreq()` for all terms in this field, or -1 if this measure isn't stored by the codec. Note that, just like other term measures, this measure does not take deleted documents into account.
      Specified by:
        `getSumDocFreq` in class `IndexReader`
      Throws:
        `IOException`
      See Also:
        `Terms.getSumDocFreq()`

    - #### getDocCount

          public final int getDocCount(String field)
                                throws IOException

      Description copied from class: `IndexReader`
      Returns the number of documents that have at least one term for this field, or -1 if this measure isn't stored by the codec. Note that, just like other term measures, this measure does not take deleted documents into account.
      Specified by:
        `getDocCount` in class `IndexReader`
      Throws:
        `IOException`
      See Also:
        `Terms.getDocCount()`

    - #### getSumTotalTermFreq

          public final long getSumTotalTermFreq(String field)
                                         throws IOException

      Description copied from class: `IndexReader`
      Returns the sum of `TermsEnum.totalTermFreq()` for all terms in this field, or -1 if this measure isn't stored by the codec (or if this fields omits term freq and positions). Note that, just like other term measures, this measure does not take deleted documents into account.
      Specified by:
        `getSumTotalTermFreq` in class `IndexReader`
      Throws:
        `IOException`
      See Also:
        `Terms.getSumTotalTermFreq()`

    - #### terms

          public final Terms terms(String field)
                            throws IOException

      This may return null if the field does not exist.
      Throws:
        `IOException`

    - #### postings

          public final PostingsEnum postings(Term term,
                              int flags)
                                      throws IOException

      Returns `PostingsEnum` for the specified term. This will return null if either the field or term does not exist.

      NOTE: The returned `PostingsEnum` may contain deleted docs.

      Throws:
        `IOException`
      See Also:
        `TermsEnum.postings(PostingsEnum)`

    - #### postings

          public final PostingsEnum postings(Term term)
                                      throws IOException

      Returns `PostingsEnum` for the specified term with `PostingsEnum.FREQS`.

      Use this method if you only require documents and frequencies, and do not need any proximity data. This method is equivalent to `postings(term, PostingsEnum.FREQS)`

      NOTE: The returned `PostingsEnum` may contain deleted docs.

      Throws:
        `IOException`
      See Also:
        `postings(Term, int)`

    - #### getNumericDocValues

          public abstract NumericDocValues getNumericDocValues(String field)
                                                        throws IOException

      Returns `NumericDocValues` for this field, or null if no `NumericDocValues` were indexed for this field. The returned instance should only be used by a single thread.
      Throws:
        `IOException`

    - #### getBinaryDocValues

          public abstract BinaryDocValues getBinaryDocValues(String field)
                                                      throws IOException

      Returns `BinaryDocValues` for this field, or null if no `BinaryDocValues` were indexed for this field. The returned instance should only be used by a single thread.
      Throws:
        `IOException`

    - #### getSortedDocValues

          public abstract SortedDocValues getSortedDocValues(String field)
                                                      throws IOException

      Returns `SortedDocValues` for this field, or null if no `SortedDocValues` were indexed for this field. The returned instance should only be used by a single thread.
      Throws:
        `IOException`

    - #### getSortedNumericDocValues

          public abstract SortedNumericDocValues getSortedNumericDocValues(String field)
                                                                    throws IOException

      Returns `SortedNumericDocValues` for this field, or null if no `SortedNumericDocValues` were indexed for this field. The returned instance should only be used by a single thread.
      Throws:
        `IOException`

    - #### getSortedSetDocValues

          public abstract SortedSetDocValues getSortedSetDocValues(String field)
                                                            throws IOException

      Returns `SortedSetDocValues` for this field, or null if no `SortedSetDocValues` were indexed for this field. The returned instance should only be used by a single thread.
      Throws:
        `IOException`

    - #### getDocsWithField

          public abstract Bits getDocsWithField(String field)
                                         throws IOException

      Returns a `Bits` at the size of `reader.maxDoc()`, with turned on bits for each docid that does have a value for this field, or null if no DocValues were indexed for this field. The returned instance should only be used by a single thread
      Throws:
        `IOException`

    - #### getNormValues

          public abstract NumericDocValues getNormValues(String field)
                                                  throws IOException

      Returns `NumericDocValues` representing norms for this field, or null if no `NumericDocValues` were indexed. The returned instance should only be used by a single thread.
      Throws:
        `IOException`

    - #### getFieldInfos

          public abstract FieldInfos getFieldInfos()

      Get the `FieldInfos` describing all fields in this reader.
      WARNING: This API is experimental and might change in incompatible ways in the next release.

    - #### getLiveDocs

          public abstract Bits getLiveDocs()

      Returns the `Bits` representing live (not deleted) docs. A set bit indicates the doc ID has not been deleted. If this method returns null it means there are no deleted documents (all documents are live). The returned instance has been safely published for use by multiple threads without additional synchronization.

    - #### checkIntegrity

          public abstract void checkIntegrity()
                                       throws IOException

      Checks consistency of this reader.

      Note that this may be costly in terms of I/O, e.g. may involve computing a checksum value against large data files.

      Throws:
        `IOException`
      NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.

    - #### termDocsEnum

          @Deprecated
          public final DocsEnum termDocsEnum(Term term)
                                      throws IOException

      Deprecated. use `postings(Term)` instead
      Returns `DocsEnum` for the specified term. This will return null if either the field or term does not exist.
      Throws:
        `IOException`

    - #### termPositionsEnum

          @Deprecated
          public final DocsAndPositionsEnum termPositionsEnum(Term term)
                                                       throws IOException

      Deprecated. use `postings(Term, int)` instead
      Returns `DocsAndPositionsEnum` for the specified term. This will return null if the field or term does not exist or positions weren't indexed.
      Throws:
        `IOException`

Copyright © 2000-2017 Apache Software Foundation. All Rights Reserved.