Class TopDocsCollector<T>
A base class for all collectors that return a TopDocs output. This collector allows easy extension by providing a single constructor which accepts a PriorityQueue<T> as well as protected members for that priority queue and a counter of the number of total hits.
Extending classes can override any of the methods to provide their own implementation, as well as avoid the use of the priority queue entirely by passing null to TopDocsCollector(PriorityQueue<T>). In that case however, you might want to consider overriding all methods, in order to avoid a System.NullReferenceException.
Inherited Members
Namespace: Lucene.Net.Search
Assembly: Lucene.Net.dll
Syntax
public abstract class TopDocsCollector<T> : ITopDocsCollector, ICollector where T : ScoreDoc
Type Parameters
Name | Description |
---|---|
T |
Constructors
| Improve this Doc View SourceTopDocsCollector(PriorityQueue<T>)
Sole constructor.
Declaration
protected TopDocsCollector(PriorityQueue<T> pq)
Parameters
Type | Name | Description |
---|---|---|
PriorityQueue<T> | pq |
Fields
| Improve this Doc View SourceEMPTY_TOPDOCS
This is used in case GetTopDocs() is called with illegal parameters, or there simply aren't (enough) results.
Declaration
protected static readonly TopDocs EMPTY_TOPDOCS
Field Value
Type | Description |
---|---|
TopDocs |
m_pq
The priority queue which holds the top documents. Note that different implementations of PriorityQueue<T> give different meaning to 'top documents'. Lucene.Net.Search.HitQueue for example aggregates the top scoring documents, while other priority queue implementations may hold documents sorted by other criteria.
Declaration
protected PriorityQueue<T> m_pq
Field Value
Type | Description |
---|---|
PriorityQueue<T> |
m_totalHits
The total number of documents that the collector encountered.
Declaration
protected int m_totalHits
Field Value
Type | Description |
---|---|
System.Int32 |
Properties
| Improve this Doc View SourceAcceptsDocsOutOfOrder
Return true
if this collector does not
require the matching docIDs to be delivered in int sort
order (smallest to largest) to Collect(Int32).
Most Lucene Query implementations will visit matching docIDs in order. However, some queries (currently limited to certain cases of BooleanQuery) can achieve faster searching if the ICollector allows them to deliver the docIDs out of order.
Many collectors don't mind getting docIDs out of
order, so it's important to return true
here.
Declaration
public abstract bool AcceptsDocsOutOfOrder { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
TopDocsCount
The number of valid priority queue entries
Declaration
protected virtual int TopDocsCount { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
TotalHits
The total number of documents that matched this query.
Declaration
public virtual int TotalHits { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Methods
| Improve this Doc View SourceCollect(Int32)
Called once for every document matching a query, with the unbased document number.
Note: The collection of the current segment can be terminated by throwing a CollectionTerminatedException. In this case, the last docs of the current AtomicReaderContext will be skipped and IndexSearcher will swallow the exception and continue collection with the next leaf.
Note: this is called in an inner search loop. For good search performance, implementations of this method should not call Doc(Int32) or Document(Int32) on every hit. Doing so can slow searches by an order of magnitude or more.
Declaration
public abstract void Collect(int doc)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | doc |
GetTopDocs()
Returns the top docs that were collected by this collector.
Declaration
public virtual TopDocs GetTopDocs()
Returns
Type | Description |
---|---|
TopDocs |
GetTopDocs(Int32)
Returns the documents in the rage [start
.. pq.Count) that were collected
by this collector. Note that if start
>= pq.Count, an empty TopDocs is
returned.
This method is convenient to call if the application always asks for the last results, starting from the last 'page'.
NOTE: you cannot call this method more than once for each search
execution. If you need to call it more than once, passing each time a
different start
, you should call GetTopDocs() and work
with the returned TopDocs object, which will contain all the
results this search execution collected.
Declaration
public virtual TopDocs GetTopDocs(int start)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | start |
Returns
Type | Description |
---|---|
TopDocs |
GetTopDocs(Int32, Int32)
Returns the documents in the rage [start
.. start
+howMany
) that were
collected by this collector. Note that if start
>= pq.Count, an empty
TopDocs is returned, and if pq.Count - start
< howMany
, then only the
available documents in [start
.. pq.Count) are returned.
This method is useful to call in case pagination of search results is
allowed by the search application, as well as it attempts to optimize the
memory used by allocating only as much as requested by howMany
.
NOTE: you cannot call this method more than once for each search execution. If you need to call it more than once, passing each time a different range, you should call GetTopDocs() and work with the returned TopDocs object, which will contain all the results this search execution collected.
Declaration
public virtual TopDocs GetTopDocs(int start, int howMany)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | start | |
System.Int32 | howMany |
Returns
Type | Description |
---|---|
TopDocs |
NewTopDocs(ScoreDoc[], Int32)
Returns a TopDocs instance containing the given results. If
results
is null
it means there are no results to return,
either because there were 0 calls to Collect(Int32) or because the arguments to
TopDocs were invalid.
Declaration
protected virtual TopDocs NewTopDocs(ScoreDoc[] results, int start)
Parameters
Type | Name | Description |
---|---|---|
ScoreDoc[] | results | |
System.Int32 | start |
Returns
Type | Description |
---|---|
TopDocs |
PopulateResults(ScoreDoc[], Int32)
Populates the results array with the ScoreDoc instances. This can be overridden in case a different ScoreDoc type should be returned.
Declaration
protected virtual void PopulateResults(ScoreDoc[] results, int howMany)
Parameters
Type | Name | Description |
---|---|---|
ScoreDoc[] | results | |
System.Int32 | howMany |
SetNextReader(AtomicReaderContext)
Called before collecting from each AtomicReaderContext. All doc ids in Collect(Int32) will correspond to Reader.
Add DocBase to the current Reader's internal document id to re-base ids in Collect(Int32).
Declaration
public abstract void SetNextReader(AtomicReaderContext context)
Parameters
Type | Name | Description |
---|---|---|
AtomicReaderContext | context | Next atomic reader context |
SetScorer(Scorer)
Called before successive calls to Collect(Int32). Implementations that need the score of the current document (passed-in to Collect(Int32)), should save the passed-in Scorer and call GetScore() when needed.
Declaration
public abstract void SetScorer(Scorer scorer)
Parameters
Type | Name | Description |
---|---|---|
Scorer | scorer |