FieldComparator

abstract class FieldComparator<T>

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.

The document IDs passed to these methods must only move forwards, since they are using doc values iterators to retrieve sort values.

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. Segment transitions are handled by creating a dedicated per-segment LeafFieldComparator which also needs to interact with the FieldValueHitQueue but can optimize based on the segment to collect.

The following functions need to be implemented

  • .compare Compare a hit at 'slot a' with hit 'slot b'.

  • .setTopValue This method is called by TopFieldCollector to notify the FieldComparator of the top most value, which is used by future calls to .

  • .getLeafComparator 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 DocValues.

  • .value 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.

See also

Inheritors

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard

Sorts by descending relevance. NOTE: if you are sorting only by descending relevance and then secondarily by ascending docID, performance is faster using TopScoreDocCollector directly (which IndexSearcher.search uses when no Sort is specified).

Link copied to clipboard
class TermValComparator(numHits: Int, field: String, sortMissingLast: Boolean) : FieldComparator<BytesRef> , LeafFieldComparator

Sorts by field's natural Term sort order. All comparisons are done using BytesRef.compareTo, which is slow for medium to large result sets but possibly very fast for very small results sets.

Functions

Link copied to clipboard
abstract fun compare(slot1: Int, slot2: Int): Int

Compare hit at slot1 with hit at slot2.

Link copied to clipboard
open fun compareValues(first: T?, second: T?): Int

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

Link copied to clipboard
open fun disableSkipping()

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.

Link copied to clipboard

Get a per-segment LeafFieldComparator to collect the given [ ]. All docIDs supplied to this [ ] are relative to the current reader (you must add docBase if you need to map it to a top-level docID).

Link copied to clipboard
open fun setSingleSort()

Informs the comparator that sort is done on this single field. This is useful to enable some optimizations for skipping non-competitive documents.

Link copied to clipboard
abstract fun setTopValue(value: T?)

Record the top value, for future calls to LeafFieldComparator.compareTop. This is only called for searches that use searchAfter (deep paging), and is called before any calls to .getLeafComparator.

Link copied to clipboard
abstract fun value(slot: Int): T?

Return the actual value in the slot.