LeafReader (Lucene 10.5.1 core API)
Description: declaration: package: org.apache.lucene.index, class: LeafReader

Package 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`

public abstract non-sealed 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/interfaces inherited from class org.apache.lucene.index.IndexReader

  `IndexReader.CacheHelper, IndexReader.CacheKey, IndexReader.ClosedListener`

- ## Constructor Summary

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

- ## Method Summary

  Modifier and Type
  Method
  Description
  `abstract void`
  `checkIntegrity()`
  Checks consistency of this reader.
  `final int`
  `docFreq(Term term)`
  Returns the number of documents containing the `term`.
  `abstract BinaryDocValues`
  `getBinaryDocValues(String field)`
  Returns `BinaryDocValues` for this field, or null if no binary doc values were indexed for this field.
  `abstract ByteVectorValues`
  `getByteVectorValues(String field)`
  Returns `ByteVectorValues` for this field, or null if no `ByteVectorValues` were indexed.
  `final LeafReaderContext`
  `getContext()`
  Expert: Returns the root `IndexReaderContext` for this `IndexReader`'s sub-reader tree.
  `abstract IndexReader.CacheHelper`
  `getCoreCacheHelper()`
  Optional method: Return a `IndexReader.CacheHelper` that can be used to cache based on the content of this leaf regardless of deletions.
  `final int`
  `getDocCount(String field)`
  Returns the number of documents that have at least one term for this field.
  `abstract DocValuesSkipper`
  `getDocValuesSkipper(String field)`
  Returns a `DocValuesSkipper` allowing skipping ranges of doc IDs that are not of interest, or `null` if a skip index was not indexed.
  `abstract FieldInfos`
  `getFieldInfos()`
  Get the `FieldInfos` describing all fields in this reader.
  `abstract FloatVectorValues`
  `getFloatVectorValues(String field)`
  Returns `FloatVectorValues` for this field, or null if no `FloatVectorValues` were indexed.
  `abstract Bits`
  `getLiveDocs()`
  Returns the `Bits` representing live (not deleted) docs.
  `LiveDocs`
  `getLiveDocsWithDeletedIterator()`
  Returns a `LiveDocs` view with efficient iteration support, or null if not available.
  `abstract LeafMetaData`
  `getMetaData()`
  Return metadata about this leaf.
  `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 numeric doc values were indexed for this field.
  `abstract PointValues`
  `getPointValues(String field)`
  Returns the `PointValues` used for numeric or spatial searches for the given field, or null if there are no point fields.
  `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.
  `final long`
  `getSumDocFreq(String field)`
  Returns the sum of `TermsEnum.docFreq()` for all terms in this field.
  `final long`
  `getSumTotalTermFreq(String field)`
  Returns the sum of `TermsEnum.totalTermFreq()` for all terms in this field.
  `final PostingsEnum`
  `postings(Term term)`
  Returns `PostingsEnum` for the specified term with `PostingsEnum.FREQS`.
  `final PostingsEnum`
  `postings(Term term, int flags)`
  Returns `PostingsEnum` for the specified term.
  `final TopDocs`
  `searchNearestVectors(String field, byte[] target, int k, AcceptDocs acceptDocs, int visitedLimit)`
  Return the k nearest neighbor documents as determined by comparison of their vector values for this field, to the given vector, by the field's similarity function.
  `abstract void`
  `searchNearestVectors(String field, byte[] target, KnnCollector knnCollector, AcceptDocs acceptDocs)`
  Return the k nearest neighbor documents as determined by comparison of their vector values for this field, to the given vector, by the field's similarity function.
  `final TopDocs`
  `searchNearestVectors(String field, float[] target, int k, AcceptDocs acceptDocs, int visitedLimit)`
  Return the k nearest neighbor documents as determined by comparison of their vector values for this field, to the given vector, by the field's similarity function.
  `abstract void`
  `searchNearestVectors(String field, float[] target, KnnCollector knnCollector, AcceptDocs acceptDocs)`
  Return the k nearest neighbor documents as determined by comparison of their vector values for this field, to the given vector, by the field's similarity function.
  `abstract Terms`
  `terms(String field)`
  Returns the `Terms` index for this field, or null if it has none.
  `final long`
  `totalTermFreq(Term term)`
  Returns the number of documents containing the term `t`.

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

  `close, decRef, doClose, ensureOpen, equals, getReaderCacheHelper, getRefCount, hasDeletions, hashCode, incRef, leaves, maxDoc, notifyReaderClosedListeners, numDeletedDocs, numDocs, registerParentReader, storedFields, termVectors, tryIncRef`

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

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

- ## Constructor Details

  - ### LeafReader

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

- ## Method Details

  - ### 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`

  - ### getCoreCacheHelper

    public abstract IndexReader.CacheHelper getCoreCacheHelper()
    Optional method: Return a `IndexReader.CacheHelper` that can be used to cache based on the content of this leaf regardless of deletions. Two readers that have the same data but different sets of deleted documents or doc values updates may be considered equal. Consider using `IndexReader.getReaderCacheHelper()` if you need deletions or dv updates to be taken into account.

    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 leaf reader.

    WARNING: This API is experimental and might change in incompatible ways in the next release.

  - ### 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. 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. 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. 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 abstract Terms terms(String field) throws IOException
    Returns the `Terms` index for this field, or null if it has none.
    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 numeric doc values 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 binary doc values 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`

  - ### 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`

  - ### getDocValuesSkipper

    public abstract DocValuesSkipper getDocValuesSkipper(String field) throws IOException
    Returns a `DocValuesSkipper` allowing skipping ranges of doc IDs that are not of interest, or `null` if a skip index was not indexed. The returned instance should be confined to the thread that created it.
    Throws:
      `IOException`

  - ### getFloatVectorValues

    public abstract FloatVectorValues getFloatVectorValues(String field) throws IOException
    Returns `FloatVectorValues` for this field, or null if no `FloatVectorValues` were indexed. The returned instance should only be used by a single thread.
    Throws:
      `IOException`
    WARNING: This API is experimental and might change in incompatible ways in the next release.

  - ### getByteVectorValues

    public abstract ByteVectorValues getByteVectorValues(String field) throws IOException
    Returns `ByteVectorValues` for this field, or null if no `ByteVectorValues` were indexed. The returned instance should only be used by a single thread.
    Throws:
      `IOException`
    WARNING: This API is experimental and might change in incompatible ways in the next release.

  - ### searchNearestVectors

    public final TopDocs searchNearestVectors(String field, float[] target, int k, AcceptDocs acceptDocs, int visitedLimit) throws IOException
    Return the k nearest neighbor documents as determined by comparison of their vector values for this field, to the given vector, by the field's similarity function. The score of each document is derived from the vector similarity in a way that ensures scores are positive and that a larger score corresponds to a higher ranking.

    The search is allowed to be approximate, meaning the results are not guaranteed to be the true k closest neighbors. For large values of k (for example when k is close to the total number of documents), the search may also retrieve fewer than k documents.

    The returned `TopDocs` will contain a `ScoreDoc` for each nearest neighbor, sorted in order of their similarity to the query vector (decreasing scores). The `TotalHits` contains the number of documents visited during the search. If the search stopped early because it hit `visitedLimit`, it is indicated through the relation `TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO`.

    Parameters:
      `field` - the vector field to search
      `target` - the vector-valued query
      `k` - the number of docs to return
      `acceptDocs` - `AcceptDocs` that represents the allowed documents to match
      `visitedLimit` - the maximum number of nodes that the search is allowed to visit
    Returns:
      the k nearest neighbor documents, along with their (searchStrategy-specific) scores.
    Throws:
      `IOException`
    WARNING: This API is experimental and might change in incompatible ways in the next release.

  - ### searchNearestVectors

    public final TopDocs searchNearestVectors(String field, byte[] target, int k, AcceptDocs acceptDocs, int visitedLimit) throws IOException
    Return the k nearest neighbor documents as determined by comparison of their vector values for this field, to the given vector, by the field's similarity function. The score of each document is derived from the vector similarity in a way that ensures scores are positive and that a larger score corresponds to a higher ranking.

    The search is allowed to be approximate, meaning the results are not guaranteed to be the true k closest neighbors. For large values of k (for example when k is close to the total number of documents), the search may also retrieve fewer than k documents.

    The returned `TopDocs` will contain a `ScoreDoc` for each nearest neighbor, sorted in order of their similarity to the query vector (decreasing scores). The `TotalHits` contains the number of documents visited during the search. If the search stopped early because it hit `visitedLimit`, it is indicated through the relation `TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO`.

    Parameters:
      `field` - the vector field to search
      `target` - the vector-valued query
      `k` - the number of docs to return
      `acceptDocs` - `AcceptDocs` that represents the allowed documents to match
      `visitedLimit` - the maximum number of nodes that the search is allowed to visit
    Returns:
      the k nearest neighbor documents, along with their (searchStrategy-specific) scores.
    Throws:
      `IOException`
    WARNING: This API is experimental and might change in incompatible ways in the next release.

  - ### searchNearestVectors

    public abstract void searchNearestVectors(String field, float[] target, KnnCollector knnCollector, AcceptDocs acceptDocs) throws IOException
    Return the k nearest neighbor documents as determined by comparison of their vector values for this field, to the given vector, by the field's similarity function. The score of each document is derived from the vector similarity in a way that ensures scores are positive and that a larger score corresponds to a higher ranking.

    The search is allowed to be approximate, meaning the results are not guaranteed to be the true k closest neighbors. For large values of k (for example when k is close to the total number of documents), the search may also retrieve fewer than k documents.

    The returned `TopDocs` will contain a `ScoreDoc` for each nearest neighbor, in order of their similarity to the query vector (decreasing scores). The `TotalHits` contains the number of documents visited during the search. If the search stopped early because it hit `visitedLimit`, it is indicated through the relation `TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO`.

    The behavior is undefined if the given field doesn't have KNN vectors enabled on its `FieldInfo`. The return value is never `null`.

    Parameters:
      `field` - the vector field to search
      `target` - the vector-valued query
      `knnCollector` - collector with settings for gathering the vector results.
      `acceptDocs` - `AcceptDocs` that represents the allowed documents to match
    Throws:
      `IOException`
    WARNING: This API is experimental and might change in incompatible ways in the next release.

  - ### searchNearestVectors

    public abstract void searchNearestVectors(String field, byte[] target, KnnCollector knnCollector, AcceptDocs acceptDocs) throws IOException
    Return the k nearest neighbor documents as determined by comparison of their vector values for this field, to the given vector, by the field's similarity function. The score of each document is derived from the vector similarity in a way that ensures scores are positive and that a larger score corresponds to a higher ranking.

    The search is allowed to be approximate, meaning the results are not guaranteed to be the true k closest neighbors. For large values of k (for example when k is close to the total number of documents), the search may also retrieve fewer than k documents.

    The returned `TopDocs` will contain a `ScoreDoc` for each nearest neighbor, in order of their similarity to the query vector (decreasing scores). The `TotalHits` contains the number of documents visited during the search. If the search stopped early because it hit `visitedLimit`, it is indicated through the relation `TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO`.

    The behavior is undefined if the given field doesn't have KNN vectors enabled on its `FieldInfo`. The return value is never `null`.

    Parameters:
      `field` - the vector field to search
      `target` - the vector-valued query
      `knnCollector` - collector with settings for gathering the vector results.
      `acceptDocs` - `AcceptDocs` that represents the allowed documents to match
    Throws:
      `IOException`
    WARNING: This API is experimental and might change in incompatible ways in the next release.

  - ### getFieldInfos

    public abstract FieldInfos getFieldInfos()
    Get the `FieldInfos` describing all fields in this reader.

    Note: Implementations should cache the FieldInfos instance returned by this method such that subsequent calls to this method return the same instance.

    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.

  - ### getLiveDocsWithDeletedIterator

    public LiveDocs getLiveDocsWithDeletedIterator()
    Returns a `LiveDocs` view with efficient iteration support, or null if not available. When non-null, provides O(deletedDocs) iteration via `LiveDocs.deletedDocsIterator()` instead of O(maxDoc) scanning. Callers should fall back to `getLiveDocs()` when null.

    This method enables consumers to opt into efficient deleted document iteration when the underlying implementation supports it (e.g., SparseLiveDocs or DenseLiveDocs).

    Default implementation returns the `LiveDocs` instance if `getLiveDocs()` implements that interface.

    WARNING: This API is experimental and might change in incompatible ways in the next release.

  - ### getPointValues

    public abstract PointValues getPointValues(String field) throws IOException
    Returns the `PointValues` used for numeric or spatial searches for the given field, or null if there are no point fields.
    Throws:
      `IOException`

  - ### 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.

  - ### getMetaData

    public abstract LeafMetaData getMetaData()
    Return metadata about this leaf.
    WARNING: This API is experimental and might change in incompatible ways in the next release.