Class FieldComparer<T>
Expert: a FieldComparer compares hits so as to determine their sort order when collecting the top results with TopFieldCollector. The concrete public FieldComparer 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 FieldComparer is made aware of segment transitions during searching in case any internal state it's tracking needs to be recomputed during these transitions.
A comparer 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 FieldComparer of the current weakest ("bottom") slot. Note that this slot may not hold the weakest value according to your comparer, in cases where your comparer is not the primary one (ie, is only used to break ties from the comparers 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 FieldComparer of the top most value, which is used by future calls to CompareTop(int).
- 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 comparer, for example retrieving new values from the IFieldCache.
- GetValue(int)Return the sort value stored in the specified slot. This is only called at the end of the search, in order to populate Fields when returning the top results.
Note
This API is experimental and might change in incompatible ways in the next release.
Inheritance
Inherited Members
Namespace: Lucene.Net.Search
Assembly: Lucene.Net.dll
Syntax
public abstract class FieldComparer<T> : FieldComparer where T : class
Type Parameters
Name | Description |
---|---|
T |
Properties
this[int]
Return the actual value in the slot. LUCENENET NOTE: This was value(int) in Lucene.
Declaration
public abstract T this[int slot] { get; }
Parameters
Type | Name | Description |
---|---|---|
int | slot | The value |
Property Value
Type | Description |
---|---|
T | Value in this slot |
Methods
Compare(int, int)
Compare hit at slot1
with hit at slot2
.
Declaration
public override abstract int Compare(int slot1, int slot2)
Parameters
Type | Name | Description |
---|---|---|
int | slot1 | first slot to compare |
int | slot2 | second slot to compare |
Returns
Type | Description |
---|---|
int | any N < 0 if |
Overrides
CompareBottom(int)
Compare the bottom of the queue with this doc. This will only invoked after SetBottom(int) 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).
Declaration
public override abstract int CompareBottom(int doc)
Parameters
Type | Name | Description |
---|---|---|
int | doc | Doc that was hit |
Returns
Type | Description |
---|---|
int | 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. |
Overrides
CompareTop(int)
Compare the top value with this doc. This will only invoked after SetTopValue(T) 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).
Declaration
public override abstract int CompareTop(int doc)
Parameters
Type | Name | Description |
---|---|---|
int | doc | Doc that was hit |
Returns
Type | Description |
---|---|
int | 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. |
Overrides
CompareValues(object, object)
Returns -1 if first is less than second. Default
implementation to assume the type implements IComparable<T> and
invoke CompareTo(T); be sure to override this method if
your FieldComparer's type isn't a IComparable<T> or
if you need special null
handling.
Declaration
public override int CompareValues(object first, object second)
Parameters
Type | Name | Description |
---|---|---|
object | first | |
object | second |
Returns
Type | Description |
---|---|
int |
Overrides
Exceptions
Type | Condition |
---|---|
ArgumentException |
|
CompareValues(T, T)
Returns -1 if first is less than second. Default
implementation to assume the type implements IComparable<T> and
invoke CompareTo(T); be sure to override this method if
your FieldComparer<T>'s type isn't a IComparable<T> or
if you need special null
handling.
Declaration
public virtual int CompareValues(T first, T second)
Parameters
Type | Name | Description |
---|---|---|
T | first | |
T | second |
Returns
Type | Description |
---|---|
int |
Copy(int, int)
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.
Declaration
public override abstract void Copy(int slot, int doc)
Parameters
Type | Name | Description |
---|---|---|
int | slot | Which slot to copy the hit to |
int | doc | DocID relative to current reader |
Overrides
GetValue(int)
Return the actual value in the slot. LUCENENET NOTE: This was value(int) in Lucene.
Declaration
public override object GetValue(int slot)
Parameters
Type | Name | Description |
---|---|---|
int | slot | The value |
Returns
Type | Description |
---|---|
object | Value in this slot |
Overrides
SetBottom(int)
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).
Declaration
public override abstract void SetBottom(int slot)
Parameters
Type | Name | Description |
---|---|---|
int | slot | the currently weakest (sorted last) slot in the queue |
Overrides
SetNextReader(AtomicReaderContext)
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).
Declaration
public override abstract FieldComparer SetNextReader(AtomicReaderContext context)
Parameters
Type | Name | Description |
---|---|---|
AtomicReaderContext | context | Current reader context |
Returns
Type | Description |
---|---|
FieldComparer | The comparer to use for this segment; most comparers can just return "this" to reuse the same comparer across segments |
Overrides
Exceptions
Type | Condition |
---|---|
IOException | If there is a low-level IO error |
SetTopValue(T)
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(AtomicReaderContext).
Declaration
public abstract void SetTopValue(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value |
SetTopValue<TValue>(TValue)
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(AtomicReaderContext).
Declaration
public override void SetTopValue<TValue>(TValue value) where TValue : class
Parameters
Type | Name | Description |
---|---|---|
TValue | value | The |
Type Parameters
Name | Description |
---|---|
TValue |
Overrides
Exceptions
Type | Condition |
---|---|
ArgumentException |
|