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 System.Int32 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(Int32, Int32) Compare a hit at 'slot a' with hit 'slot b'.
- SetBottom(Int32)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(Int32)Compare a new hit (docID) against the "weakest" (bottom) entry in the queue.
- SetTopValue(Object)This method is called by TopFieldCollector to notify the FieldComparer of the top most value, which is used by future calls to CompareTop(Int32).
- CompareTop(Int32)Compare a new hit (docID) against the top value previously set by a call to SetTopValue(Object).
- Copy(Int32, Int32)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.
- Item[Int32]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.
Inheritance
Inherited Members
Namespace: Lucene.Net.Search
Assembly: Lucene.Net.dll
Syntax
public abstract class FieldComparer<T> : FieldComparer
Type Parameters
Name | Description |
---|---|
T |
Methods
| Improve this Doc View SourceCompare(Int32, Int32)
Compare hit at slot1
with hit at slot2
.
Declaration
public abstract override int Compare(int slot1, int slot2)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | slot1 | first slot to compare |
System.Int32 | slot2 | second slot to compare |
Returns
Type | Description |
---|---|
System.Int32 | any N < 0 if |
Overrides
| Improve this Doc View SourceCompareBottom(Int32)
Compare the bottom of the queue with this doc. This will only invoked after SetBottom(Int32) has been called. This should return the same result as Compare(Int32, Int32) 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 abstract override int CompareBottom(int doc)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | doc | Doc that was hit |
Returns
Type | Description |
---|---|
System.Int32 | 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
| Improve this Doc View SourceCompareTop(Int32)
Compare the top value with this doc. This will only invoked after SetTopValue(Object) has been called. This should return the same result as Compare(Int32, Int32) 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 abstract override int CompareTop(int doc)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | doc | Doc that was hit |
Returns
Type | Description |
---|---|
System.Int32 | 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
| Improve this Doc View SourceCompareValues(T, T)
Returns -1 if first is less than second. Default
impl to assume the type implements System.IComparable<T> and
invoke System.IComparable<T>.CompareTo(T); be sure to override this method if
your FieldComparer's type isn't a System.IComparable<T> or
if your values may sometimes be null
Declaration
public virtual int CompareValues(T first, T second)
Parameters
Type | Name | Description |
---|---|---|
T | first | |
T | second |
Returns
Type | Description |
---|---|
System.Int32 |
CompareValues(Object, Object)
Returns -1 if first is less than second. Default
impl to assume the type implements System.IComparable<T> and
invoke System.IComparable<T>.CompareTo(T); be sure to override this method if
your FieldComparer's type isn't a System.IComparable<T> or
if your values may sometimes be null
Declaration
public override int CompareValues(object first, object second)
Parameters
Type | Name | Description |
---|---|---|
System.Object | first | |
System.Object | second |
Returns
Type | Description |
---|---|
System.Int32 |
Overrides
| Improve this Doc View SourceCopy(Int32, Int32)
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 abstract override void Copy(int slot, int doc)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | slot | Which slot to copy the hit to |
System.Int32 | doc | DocID relative to current reader |
Overrides
| Improve this Doc View SourceSetBottom(Int32)
Set the bottom slot, ie the "weakest" (sorted last) entry in the queue. When CompareBottom(Int32) is called, you should compare against this slot. This will always be called before CompareBottom(Int32).
Declaration
public abstract override void SetBottom(int slot)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | slot | the currently weakest (sorted last) slot in the queue |
Overrides
| Improve this Doc View SourceSetNextReader(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 abstract override 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 |
---|---|
System.IO.IOException | If there is a low-level IO error |
SetTopValue(Object)
Record the top value, for future calls to CompareTop(Int32). This is only called for searches that use SearchAfter (deep paging), and is called before any calls to SetNextReader(AtomicReaderContext).
Declaration
public abstract override void SetTopValue(object value)
Parameters
Type | Name | Description |
---|---|---|
System.Object | value |