TermOrdValComparator (Lucene 10.5.0 core API)
Description: declaration: package: org.apache.lucene.search.comparators, class: TermOrdValComparator

Package org.apache.lucene.search.comparators

# Class TermOrdValComparator

java.lang.Object
org.apache.lucene.search.FieldComparator<BytesRef>
org.apache.lucene.search.comparators.TermOrdValComparator

public class TermOrdValComparator extends FieldComparator<BytesRef>
Sorts by field's natural Term sort order, using ordinals. This is functionally equivalent to `FieldComparator.TermValComparator`, but it first resolves the string to their relative ordinal positions (using the index returned by `LeafReader.getSortedDocValues(String)`), and does most comparisons using the ordinals. For medium to large results, this comparator will be much faster than `FieldComparator.TermValComparator`. For very small result sets it may be slower.

- ## Nested Class Summary

  ## Nested classes/interfaces inherited from class org.apache.lucene.search.FieldComparator

  `FieldComparator.RelevanceComparator, FieldComparator.TermValComparator`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `TermOrdValComparator(int numHits, String field, boolean sortMissingLast, boolean reverse, Pruning pruning)`
  Creates this, with control over how missing values are sorted.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `int`
  `compare(int slot1, int slot2)`
  Compare hit at slot1 with hit at slot2.
  `int`
  `compareValues(BytesRef val1, BytesRef val2)`
  Returns a negative integer if first is less than second, 0 if they are equal and a positive integer otherwise.
  `void`
  `disableSkipping()`
  Informs the comparator that the skipping of documents should be disabled.
  `LeafFieldComparator`
  `getLeafComparator(LeafReaderContext context)`
  Get a per-segment `LeafFieldComparator` to collect the given `LeafReaderContext`.
  `protected SortedDocValues`
  `getSortedDocValues(LeafReaderContext context, String field)`
  Retrieves the SortedDocValues for the field in this segment
  `void`
  `setSingleSort()`
  Informs the comparator that sort is done on this single field.
  `void`
  `setTopValue(BytesRef value)`
  Record the top value, for future calls to `LeafFieldComparator.compareTop(int)`.
  `BytesRef`
  `value(int slot)`
  Return the actual value in the slot.

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

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

- ## Constructor Details

  - ### TermOrdValComparator

    public TermOrdValComparator(int numHits, String field, boolean sortMissingLast, boolean reverse, Pruning pruning)
    Creates this, with control over how missing values are sorted. Pass sortMissingLast=true to put missing values at the end.

- ## Method Details

  - ### disableSkipping

    public void disableSkipping()
    Description copied from class: `FieldComparator`
    Informs the comparator that the skipping of documents should be disabled. This function is called by TopFieldCollector in cases when the skipping functionality should not be applied or not necessary. An example could be when search sort is a part of the index sort, and can be already efficiently handled by TopFieldCollector, and doing extra work for skipping in the comparator is redundant.
    Overrides:
      `disableSkipping` in class `FieldComparator<BytesRef>`

  - ### setSingleSort

    public void setSingleSort()
    Description copied from class: `FieldComparator`
    Informs the comparator that sort is done on this single field. This is useful to enable some optimizations for skipping non-competitive documents.
    Overrides:
      `setSingleSort` in class `FieldComparator<BytesRef>`

  - ### compare

    public int compare(int slot1, int slot2)
    Description copied from class: `FieldComparator`
    Compare hit at slot1 with hit at slot2.
    Specified by:
      `compare` in class `FieldComparator<BytesRef>`
    Parameters:
      `slot1` - first slot to compare
      `slot2` - second slot to compare
    Returns:
      any `N < 0` if slot2's value is sorted after slot1, any `N > 0` if the slot2's value is sorted before slot1 and `0` if they are equal

  - ### getSortedDocValues

    protected SortedDocValues getSortedDocValues(LeafReaderContext context, String field) throws IOException
    Retrieves the SortedDocValues for the field in this segment
    Throws:
      `IOException`

  - ### getLeafComparator

    public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException
    Description copied from class: `FieldComparator`
    Get a per-segment `LeafFieldComparator` to collect the given `LeafReaderContext`. All docIDs supplied to this `LeafFieldComparator` are relative to the current reader (you must add docBase if you need to map it to a top-level docID).
    Specified by:
      `getLeafComparator` in class `FieldComparator<BytesRef>`
    Parameters:
      `context` - current reader context
    Returns:
      the comparator to use for this segment
    Throws:
      `IOException` - if there is a low-level IO error

  - ### setTopValue

    public void setTopValue(BytesRef value)
    Description copied from class: `FieldComparator`
    Record the top value, for future calls to `LeafFieldComparator.compareTop(int)`. This is only called for searches that use searchAfter (deep paging), and is called before any calls to `FieldComparator.getLeafComparator(LeafReaderContext)`.
    Specified by:
      `setTopValue` in class `FieldComparator<BytesRef>`

  - ### value

    public BytesRef value(int slot)
    Description copied from class: `FieldComparator`
    Return the actual value in the slot.
    Specified by:
      `value` in class `FieldComparator<BytesRef>`
    Parameters:
      `slot` - the value
    Returns:
      value in this slot

  - ### compareValues

    public int compareValues(BytesRef val1, BytesRef val2)
    Description copied from class: `FieldComparator`
    Returns a negative integer if first is less than second, 0 if they are equal and a positive integer otherwise. Default impl to assume the type implements Comparable and invoke .compareTo; be sure to override this method if your FieldComparator's type isn't a Comparable or if your values may sometimes be null
    Overrides:
      `compareValues` in class `FieldComparator<BytesRef>`