org.apache.lucene.search.grouping
Class BlockGroupingCollector

java.lang.Object
  extended by org.apache.lucene.search.Collector
      extended by org.apache.lucene.search.grouping.BlockGroupingCollector

public class BlockGroupingCollector
extends Collector

BlockGroupingCollector performs grouping with a single pass collector, as long as you are grouping by a doc block field, ie all documents sharing a given group value were indexed as a doc block using the atomic IndexWriter.addDocuments(java.util.Collection) or IndexWriter.updateDocuments(org.apache.lucene.index.Term, java.util.Collection) API.

This results in faster performance (~25% faster QPS) than the two-pass grouping collectors, with the tradeoff being that the documents in each group must always be indexed as a block. This collector also fills in TopGroups.totalGroupCount without requiring the separate TermAllGroupsCollector. However, this collector does not fill in the groupValue of each group; this field will always be null.

NOTE: this collector makes no effort to verify the docs were in fact indexed as a block, so it's up to you to ensure this was the case.

See org.apache.lucene.search.grouping for more details including a full code example.

WARNING: This API is experimental and might change in incompatible ways in the next release.

Constructor Summary
BlockGroupingCollector(Sort groupSort, int topNGroups, boolean needsScores, Filter lastDocPerGroup)
          Create the single pass collector.
 
Method Summary
 boolean acceptsDocsOutOfOrder()
          Return true if this collector does not require the matching docIDs to be delivered in int sort order (smallest to largest) to Collector.collect(int).
 void collect(int doc)
          Called once for every document matching a query, with the unbased document number.
 TopGroups getTopGroups(Sort withinGroupSort, int groupOffset, int withinGroupOffset, int maxDocsPerGroup, boolean fillSortFields)
          Returns the grouped results.
 void setNextReader(IndexReader reader, int docBase)
          Called before collecting from each IndexReader.
 void setScorer(Scorer scorer)
          Called before successive calls to Collector.collect(int).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BlockGroupingCollector

public BlockGroupingCollector(Sort groupSort,
                              int topNGroups,
                              boolean needsScores,
                              Filter lastDocPerGroup)
                       throws IOException
Create the single pass collector.

Parameters:
groupSort - The Sort used to sort the groups. The top sorted document within each group according to groupSort, determines how that group sorts against other groups. This must be non-null, ie, if you want to groupSort by relevance use Sort.RELEVANCE.
topNGroups - How many top groups to keep.
needsScores - true if the collected documents require scores, either because relevance is included in the withinGroupSort or because you plan to pass true for either getSscores or getMaxScores to getTopGroups(org.apache.lucene.search.Sort, int, int, int, boolean)
lastDocPerGroup - a Filter that marks the last document in each group.
Throws:
IOException
Method Detail

getTopGroups

public TopGroups getTopGroups(Sort withinGroupSort,
                              int groupOffset,
                              int withinGroupOffset,
                              int maxDocsPerGroup,
                              boolean fillSortFields)
                       throws IOException
Returns the grouped results. Returns null if the number of groups collected is <= groupOffset.

NOTE: This collector is unable to compute the groupValue per group so it will always be null. This is normally not a problem, as you can obtain the value just like you obtain other values for each matching document (eg, via stored fields, via FieldCache, etc.)

Parameters:
withinGroupSort - The Sort used to sort documents within each group. Passing null is allowed, to sort by relevance.
groupOffset - Which group to start from
withinGroupOffset - Which document to start from within each group
maxDocsPerGroup - How many top documents to keep within each group.
fillSortFields - If true then the Comparable values for the sort fields will be set
Throws:
IOException

setScorer

public void setScorer(Scorer scorer)
               throws IOException
Description copied from class: Collector
Called before successive calls to Collector.collect(int). Implementations that need the score of the current document (passed-in to Collector.collect(int)), should save the passed-in Scorer and call scorer.score() when needed.

Specified by:
setScorer in class Collector
Throws:
IOException

collect

public void collect(int doc)
             throws IOException
Description copied from class: Collector
Called once for every document matching a query, with the unbased document number.

Note: This is called in an inner search loop. For good search performance, implementations of this method should not call Searcher.doc(int) or IndexReader.document(int) on every hit. Doing so can slow searches by an order of magnitude or more.

Specified by:
collect in class Collector
Throws:
IOException

acceptsDocsOutOfOrder

public boolean acceptsDocsOutOfOrder()
Description copied from class: Collector
Return true if this collector does not require the matching docIDs to be delivered in int sort order (smallest to largest) to Collector.collect(int).

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 Collector 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.

Specified by:
acceptsDocsOutOfOrder in class Collector

setNextReader

public void setNextReader(IndexReader reader,
                          int docBase)
                   throws IOException
Description copied from class: Collector
Called before collecting from each IndexReader. All doc ids in Collector.collect(int) will correspond to reader. Add docBase to the current IndexReaders internal document id to re-base ids in Collector.collect(int).

Specified by:
setNextReader in class Collector
Parameters:
reader - next IndexReader
Throws:
IOException