Class FieldComparator<T>

java.lang.Object
org.apache.lucene.search.FieldComparator<T>
Direct Known Subclasses:
FieldComparator.DocComparator, FieldComparator.NumericComparator, FieldComparator.RelevanceComparator, FieldComparator.TermOrdValComparator, FieldComparator.TermValComparator

public abstract class FieldComparator<T> extends Object
Expert: a FieldComparator compares hits so as to determine their sort order when collecting the top results with TopFieldCollector. The concrete public FieldComparator classes here correspond to the SortField types.

This API is designed to achieve high performance sorting, by exposing a tight interaction with FieldValueHitQueue as it visits hits. Whenever a hit is competitive, it's enrolled into a virtual slot, which is an int ranging from 0 to numHits-1. The FieldComparator is made aware of segment transitions during searching in case any internal state it's tracking needs to be recomputed during these transitions.

A comparator must define these functions:

  • compare(int, int) Compare a hit at 'slot a' with hit 'slot b'.
  • setBottom(int) This method is called by FieldValueHitQueue to notify the FieldComparator of the current weakest ("bottom") slot. Note that this slot may not hold the weakest value according to your comparator, in cases where your comparator is not the primary one (ie, is only used to break ties from the comparators before it).
  • compareBottom(int) Compare a new hit (docID) against the "weakest" (bottom) entry in the queue.
  • setTopValue(T) This method is called by TopFieldCollector to notify the FieldComparator of the top most value, which is used by future calls to compareTop(int).
  • compareBottom(int) Compare a new hit (docID) against the "weakest" (bottom) entry in the queue.
  • compareTop(int) Compare a new hit (docID) against the top value previously set by a call to setTopValue(T).
  • copy(int, int) Installs a new hit into the priority queue. The FieldValueHitQueue calls this method when a new hit is competitive.
  • setNextReader(AtomicReaderContext) Invoked when the search is switching to the next segment. You may need to update internal state of the comparator, for example retrieving new values from the FieldCache.
  • value(int) Return the sort value stored in the specified slot. This is only called at the end of the search, in order to populate FieldDoc.fields when returning the top results.
  • Constructor Details

    • FieldComparator

      public FieldComparator()
  • Method Details

    • compare

      public abstract int compare(int slot1, int slot2)
      Compare hit at slot1 with hit at slot2.
      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
    • setBottom

      public abstract void setBottom(int slot)
      Set the bottom slot, ie the "weakest" (sorted last) entry in the queue. When compareBottom(int) is called, you should compare against this slot. This will always be called before compareBottom(int).
      Parameters:
      slot - the currently weakest (sorted last) slot in the queue
    • setTopValue

      public abstract void setTopValue(T value)
      Record the top value, for future calls to compareTop(int). This is only called for searches that use searchAfter (deep paging), and is called before any calls to setNextReader(org.apache.lucene.index.AtomicReaderContext).
    • compareBottom

      public abstract int compareBottom(int doc) throws IOException
      Compare the bottom of the queue with this doc. This will only invoked after setBottom has been called. This should return the same result as compare(int,int)} as if bottom were slot1 and the new document were slot 2.

      For a search that hits many results, this method will be the hotspot (invoked by far the most frequently).

      Parameters:
      doc - that was hit
      Returns:
      any N < 0 if the doc's value is sorted after the bottom entry (not competitive), any N > 0 if the doc's value is sorted before the bottom entry and 0 if they are equal.
      Throws:
      IOException
    • compareTop

      public abstract int compareTop(int doc) throws IOException
      Compare the top value with this doc. This will only invoked after setTopValue has been called. This should return the same result as compare(int,int)} as if topValue were slot1 and the new document were slot 2. This is only called for searches that use searchAfter (deep paging).
      Parameters:
      doc - that was hit
      Returns:
      any N < 0 if the doc's value is sorted after the bottom entry (not competitive), any N > 0 if the doc's value is sorted before the bottom entry and 0 if they are equal.
      Throws:
      IOException
    • copy

      public abstract void copy(int slot, int doc) throws IOException
      This method is called when a new hit is competitive. You should copy any state associated with this document that will be required for future comparisons, into the specified slot.
      Parameters:
      slot - which slot to copy the hit to
      doc - docID relative to current reader
      Throws:
      IOException
    • setNextReader

      public abstract FieldComparator<T> setNextReader(AtomicReaderContext context) throws IOException
      Set a new AtomicReaderContext. All subsequent docIDs are relative to the current reader (you must add docBase if you need to map it to a top-level docID).
      Parameters:
      context - current reader context
      Returns:
      the comparator to use for this segment; most comparators can just return "this" to reuse the same comparator across segments
      Throws:
      IOException - if there is a low-level IO error
    • setScorer

      public void setScorer(Scorer scorer)
      Sets the Scorer to use in case a document's score is needed.
      Parameters:
      scorer - Scorer instance that you should use to obtain the current hit's score, if necessary.
    • value

      public abstract T value(int slot)
      Return the actual value in the slot.
      Parameters:
      slot - the value
      Returns:
      value in this slot
    • compareValues

      public int compareValues(T first, T second)
      Returns -1 if first is less than second. 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