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

Package org.apache.lucene.index

# Class TermsEnum

java.lang.Object
org.apache.lucene.index.TermsEnum

All Implemented Interfaces:
  `BytesRefIterator`

Direct Known Subclasses:
  `BaseTermsEnum`, `FilteredTermsEnum`, `FilterLeafReader.FilterTermsEnum`

public abstract class TermsEnum extends Object implements BytesRefIterator
Iterator to seek (`seekCeil(BytesRef)`, `seekExact(BytesRef)`) or step through (`BytesRefIterator.next()` terms to obtain frequency information (`docFreq()`), `PostingsEnum` or `PostingsEnum` for the current term (`postings(org.apache.lucene.index.PostingsEnum)`.

Term enumerations are always ordered by BytesRef.compareTo, which is Unicode sort order if the terms are UTF-8 bytes. Each term in the enumeration is greater than the one before it.

The TermsEnum is unpositioned when you first obtain it and you must first successfully call `BytesRefIterator.next()` or one of the `seek` methods.

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

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static enum`
  `TermsEnum.SeekStatus`
  Represents returned result from `seekCeil(org.apache.lucene.util.BytesRef)`.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final TermsEnum`
  `EMPTY`
  An empty TermsEnum for quickly returning an empty instance e.g.

- ## Constructor Summary

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

- ## Method Summary

  Modifier and Type
  Method
  Description
  `abstract AttributeSource`
  `attributes()`
  Returns the related attributes.
  `abstract int`
  `docFreq()`
  Returns the number of documents containing the current term.
  `abstract ImpactsEnum`
  `impacts(int flags)`
  Return a `ImpactsEnum`.
  `abstract long`
  `ord()`
  Returns ordinal position for current term.
  `final PostingsEnum`
  `postings(PostingsEnum reuse)`
  Get `PostingsEnum` for the current term.
  `abstract PostingsEnum`
  `postings(PostingsEnum reuse, int flags)`
  Get `PostingsEnum` for the current term, with control over whether freqs, positions, offsets or payloads are required.
  `boolean`
  `preferSeekExact()`
  Expert: prefer using seekExact() to seekCeil().
  `abstract IOBooleanSupplier`
  `prepareSeekExact(BytesRef text)`
  Two-phase `seekExact(org.apache.lucene.util.BytesRef)`.
  `abstract TermsEnum.SeekStatus`
  `seekCeil(BytesRef text)`
  Seeks to the specified term, if it exists, or to the next (ceiling) term.
  `abstract void`
  `seekExact(long ord)`
  Seeks to the specified term by ordinal (position) as previously returned by `ord()`.
  `abstract boolean`
  `seekExact(BytesRef text)`
  Attempts to seek to the exact term, returning true if the term is found.
  `abstract void`
  `seekExact(BytesRef term, TermState state)`
  Expert: Seeks a specific position by `TermState` previously obtained from `termState()`.
  `abstract BytesRef`
  `term()`
  Returns current term.
  `abstract TermState`
  `termState()`
  Expert: Returns the TermsEnums internal state to position the TermsEnum without re-seeking the term dictionary.
  `abstract long`
  `totalTermFreq()`
  Returns the total number of occurrences of this term across all documents (the sum of the freq() for each doc that has this term).

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

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

  ### Methods inherited from interface org.apache.lucene.util.BytesRefIterator

  `next`

- ## Field Details

  - ### EMPTY

    public static final TermsEnum EMPTY
    An empty TermsEnum for quickly returning an empty instance e.g. in `MultiTermQuery`

    Please note: This enum should be unmodifiable, but it is currently possible to add Attributes to it. This should not be a problem, as the enum is always empty and the existence of unused Attributes does not matter.

- ## Constructor Details

  - ### TermsEnum

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

- ## Method Details

  - ### attributes

    public abstract AttributeSource attributes()
    Returns the related attributes.

  - ### seekExact

    public abstract boolean seekExact(BytesRef text) throws IOException
    Attempts to seek to the exact term, returning true if the term is found. If this returns false, the enum is unpositioned. For some codecs, seekExact may be substantially faster than `seekCeil(org.apache.lucene.util.BytesRef)`.
    Returns:
      true if the term is found; return false if the enum is unpositioned.
    Throws:
      `IOException`

  - ### prepareSeekExact

    public abstract IOBooleanSupplier prepareSeekExact(BytesRef text) throws IOException
    Two-phase `seekExact(org.apache.lucene.util.BytesRef)`. The first phase typically calls `IndexInput.prefetch(long, long)` on the right range of bytes under the hood, while the second phase `IOBooleanSupplier.get()` actually seeks the term within these bytes. This can be used to parallelize I/O across multiple terms by calling `prepareSeekExact(org.apache.lucene.util.BytesRef)` on multiple terms enums before calling `IOBooleanSupplier.get()`.

    NOTE: It is illegal to call other methods on this `TermsEnum` after calling this method until `IOBooleanSupplier.get()` is called.

    NOTE: This may return `null` if this `TermsEnum` can identify that the term may not exist without performing any I/O.

    NOTE: The returned `IOBooleanSupplier` must be consumed in the same thread.

    Throws:
      `IOException`

  - ### seekCeil

    public abstract TermsEnum.SeekStatus seekCeil(BytesRef text) throws IOException
    Seeks to the specified term, if it exists, or to the next (ceiling) term. Returns SeekStatus to indicate whether exact term was found, a different term was found, or EOF was hit. The target term may be before or after the current term. If this returns SeekStatus.END, the enum is unpositioned.
    Throws:
      `IOException`

  - ### seekExact

    public abstract void seekExact(long ord) throws IOException
    Seeks to the specified term by ordinal (position) as previously returned by `ord()`. The target ord may be before or after the current ord, and must be within bounds.
    Throws:
      `IOException`

  - ### seekExact

    public abstract void seekExact(BytesRef term, TermState state) throws IOException
    Expert: Seeks a specific position by `TermState` previously obtained from `termState()`. Callers should maintain the `TermState` to use this method. Low-level implementations may position the TermsEnum without re-seeking the term dictionary.

    Seeking by `TermState` should only be used iff the state was obtained from the same `TermsEnum` instance.

    NOTE: Using this method with an incompatible `TermState` might leave this `TermsEnum` in undefined state. On a segment level `TermState` instances are compatible only iff the source and the target `TermsEnum` operate on the same field. If operating on segment level, TermState instances must not be used across segments.

    NOTE: A seek by `TermState` might not restore the `AttributeSource`'s state. `AttributeSource` states must be maintained separately if this method is used.

    Parameters:
      `term` - the term the TermState corresponds to
      `state` - the `TermState`
    Throws:
      `IOException`

  - ### term

    public abstract BytesRef term() throws IOException
    Returns current term. Do not call this when the enum is unpositioned.
    Throws:
      `IOException`

  - ### ord

    public abstract long ord() throws IOException
    Returns ordinal position for current term. This is an optional method (the codec may throw `UnsupportedOperationException`). Do not call this when the enum is unpositioned.
    Throws:
      `IOException`

  - ### docFreq

    public abstract int docFreq() throws IOException
    Returns the number of documents containing the current term. Do not call this when the enum is unpositioned. `TermsEnum.SeekStatus.END`.
    Throws:
      `IOException`

  - ### totalTermFreq

    public abstract long totalTermFreq() throws IOException
    Returns the total number of occurrences of this 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.
    Throws:
      `IOException`

  - ### postings

    public final PostingsEnum postings(PostingsEnum reuse) throws IOException
    Get `PostingsEnum` for the current term. Do not call this when the enum is unpositioned. This method will not return null.

    NOTE: the returned iterator may return deleted documents, so deleted documents have to be checked on top of the `PostingsEnum`.

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

    Parameters:
      `reuse` - pass a prior PostingsEnum for possible reuse
    Throws:
      `IOException`
    See Also:
      - `postings(PostingsEnum, int)`

  - ### postings

    public abstract PostingsEnum postings(PostingsEnum reuse, int flags) throws IOException
    Get `PostingsEnum` for the current term, with control over whether freqs, positions, offsets or payloads are required. Do not call this when the enum is unpositioned. This method will not return null.

    NOTE: the returned iterator may return deleted documents, so deleted documents have to be checked on top of the `PostingsEnum`.

    Parameters:
      `reuse` - pass a prior PostingsEnum for possible reuse
      `flags` - specifies which optional per-document values you require; see `PostingsEnum.FREQS`
    Throws:
      `IOException`

  - ### impacts

    public abstract ImpactsEnum impacts(int flags) throws IOException
    Return a `ImpactsEnum`.
    Throws:
      `IOException`
    See Also:
      - `postings(PostingsEnum, int)`

  - ### termState

    public abstract TermState termState() throws IOException
    Expert: Returns the TermsEnums internal state to position the TermsEnum without re-seeking the term dictionary.

    NOTE: A seek by `TermState` might not capture the `AttributeSource`'s state. Callers must maintain the `AttributeSource` states separately

    Throws:
      `IOException`
    See Also:
      - `TermState`
      - `seekExact(BytesRef, TermState)`

  - ### preferSeekExact

    public boolean preferSeekExact()
    Expert: prefer using seekExact() to seekCeil().

    This is consumed within Lucene when performing key lookups for update and delete operations. Override this for implementations that use an approximate membership filter to ensure that the membership filter is consulted.