Class BlockGroupingCollector
- java.lang.Object
-
- org.apache.lucene.search.SimpleCollector
-
- org.apache.lucene.search.grouping.BlockGroupingCollector
-
- All Implemented Interfaces:
Collector,LeafCollector
public class BlockGroupingCollector extends SimpleCollector
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 atomicIndexWriter.addDocuments()orIndexWriter.updateDocuments()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
AllGroupsCollector. 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.groupingfor more details including a full code example.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classBlockGroupingCollector.GroupQueueprivate static classBlockGroupingCollector.OneGroupprivate static classBlockGroupingCollector.ScoreAndDoc
-
Field Summary
Fields Modifier and Type Field Description private intbottomSlotprivate FieldComparator<?>[]comparatorsprivate intcompIDXEndprivate LeafReaderContextcurrentReaderContextprivate intdocBaseprivate booleangroupCompetesprivate intgroupEndDocIDprivate BlockGroupingCollector.GroupQueuegroupQueueprivate SortgroupSortprivate WeightlastDocPerGroupprivate DocIdSetIteratorlastDocPerGroupBitsprivate LeafFieldComparator[]leafComparatorsprivate booleanneedsScoresprivate int[]pendingSubDocsprivate float[]pendingSubScoresprivate booleanqueueFullprivate int[]reversedprivate Scorablescorerprivate intsubDocUptoprivate inttopGroupDocprivate inttopNGroupsprivate inttotalGroupCountprivate inttotalHitCount
-
Constructor Summary
Constructors Constructor Description BlockGroupingCollector(Sort groupSort, int topNGroups, boolean needsScores, Weight lastDocPerGroup)Create the single pass collector.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcollect(int doc)Called once for every document matching a query, with the unbased document number.protected voiddoSetNextReader(LeafReaderContext readerContext)This method is called before collectingcontext.TopGroups<?>getTopGroups(Sort withinGroupSort, int groupOffset, int withinGroupOffset, int maxDocsPerGroup)Returns the grouped results.private voidprocessGroup()ScoreModescoreMode()Indicates what features are required from the scorer.voidsetScorer(Scorable scorer)Called before successive calls toLeafCollector.collect(int).-
Methods inherited from class org.apache.lucene.search.SimpleCollector
getLeafCollector
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.lucene.search.LeafCollector
competitiveIterator
-
-
-
-
Field Detail
-
pendingSubDocs
private int[] pendingSubDocs
-
pendingSubScores
private float[] pendingSubScores
-
subDocUpto
private int subDocUpto
-
groupSort
private final Sort groupSort
-
topNGroups
private final int topNGroups
-
lastDocPerGroup
private final Weight lastDocPerGroup
-
needsScores
private final boolean needsScores
-
comparators
private final FieldComparator<?>[] comparators
-
leafComparators
private final LeafFieldComparator[] leafComparators
-
reversed
private final int[] reversed
-
compIDXEnd
private final int compIDXEnd
-
bottomSlot
private int bottomSlot
-
queueFull
private boolean queueFull
-
currentReaderContext
private LeafReaderContext currentReaderContext
-
topGroupDoc
private int topGroupDoc
-
totalHitCount
private int totalHitCount
-
totalGroupCount
private int totalGroupCount
-
docBase
private int docBase
-
groupEndDocID
private int groupEndDocID
-
lastDocPerGroupBits
private DocIdSetIterator lastDocPerGroupBits
-
scorer
private Scorable scorer
-
groupQueue
private final BlockGroupingCollector.GroupQueue groupQueue
-
groupCompetes
private boolean groupCompetes
-
-
Constructor Detail
-
BlockGroupingCollector
public BlockGroupingCollector(Sort groupSort, int topNGroups, boolean needsScores, Weight lastDocPerGroup)
Create the single pass collector.- Parameters:
groupSort- TheSortused 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 togetTopGroups(org.apache.lucene.search.Sort, int, int, int)lastDocPerGroup- aWeightthat marks the last document in each group.
-
-
Method Detail
-
processGroup
private void processGroup() throws java.io.IOException- Throws:
java.io.IOException
-
getTopGroups
public TopGroups<?> getTopGroups(Sort withinGroupSort, int groupOffset, int withinGroupOffset, int maxDocsPerGroup) throws java.io.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 DocValues, etc.)
- Parameters:
withinGroupSort- TheSortused to sort documents within each group.groupOffset- Which group to start fromwithinGroupOffset- Which document to start from within each groupmaxDocsPerGroup- How many top documents to keep within each group.- Throws:
java.io.IOException
-
setScorer
public void setScorer(Scorable scorer) throws java.io.IOException
Description copied from interface:LeafCollectorCalled before successive calls toLeafCollector.collect(int). Implementations that need the score of the current document (passed-in toLeafCollector.collect(int)), should save the passed-in Scorer and call scorer.score() when needed.- Specified by:
setScorerin interfaceLeafCollector- Overrides:
setScorerin classSimpleCollector- Throws:
java.io.IOException
-
collect
public void collect(int doc) throws java.io.IOExceptionDescription copied from interface:LeafCollectorCalled 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 currentLeafReaderContextwill be skipped andIndexSearcherwill 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
IndexSearcher.doc(int)orIndexReader.document(int)on every hit. Doing so can slow searches by an order of magnitude or more.- Specified by:
collectin interfaceLeafCollector- Specified by:
collectin classSimpleCollector- Throws:
java.io.IOException
-
doSetNextReader
protected void doSetNextReader(LeafReaderContext readerContext) throws java.io.IOException
Description copied from class:SimpleCollectorThis method is called before collectingcontext.- Overrides:
doSetNextReaderin classSimpleCollector- Throws:
java.io.IOException
-
-