BlockGroupingCollector (Lucene 10.5.0 grouping API)
Description: declaration: package: org.apache.lucene.search.grouping, class: BlockGroupingCollector

Package org.apache.lucene.search.grouping

# 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 atomic `IndexWriter.addDocuments()` or `IndexWriter.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.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

  Constructors
  Constructor
  Description
  `BlockGroupingCollector(Sort groupSort, int topNGroups, boolean needsScores, Weight lastDocPerGroup)`
  Create the single pass collector.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `collect(int doc)`
  `protected void`
  `doSetNextReader(LeafReaderContext readerContext)`
  `void`
  `finish()`
  `TopGroups<?>`
  `getTopGroups(Sort withinGroupSort, int groupOffset, int withinGroupOffset, int maxDocsPerGroup)`
  Returns the grouped results.
  `ScoreMode`
  `scoreMode()`
  `void`
  `setScorer(Scorable scorer)`

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

  `setWeight`

  ### Methods inherited from interface org.apache.lucene.search.LeafCollector

  `collect, collectRange, competitiveIterator`

- ## Constructor Details

  - ### BlockGroupingCollector

    public BlockGroupingCollector(Sort groupSort, int topNGroups, boolean needsScores, Weight lastDocPerGroup)
    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)`
      `lastDocPerGroup` - a `Weight` that marks the last document in each group.

- ## Method Details

  - ### getTopGroups

    public TopGroups<?> getTopGroups(Sort withinGroupSort, int groupOffset, int withinGroupOffset, int maxDocsPerGroup) 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 DocValues, etc.)

    Parameters:
      `withinGroupSort` - The `Sort` used to sort documents within each group.
      `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.
    Throws:
      `IOException`

  - ### setScorer

    public void setScorer(Scorable scorer) throws IOException
    Specified by:
      `setScorer` in interface `LeafCollector`
    Overrides:
      `setScorer` in class `SimpleCollector`
    Throws:
      `IOException`

  - ### collect

    public void collect(int doc) throws IOException
    Specified by:
      `collect` in interface `LeafCollector`
    Specified by:
      `collect` in class `SimpleCollector`
    Throws:
      `IOException`

  - ### doSetNextReader

    protected void doSetNextReader(LeafReaderContext readerContext) throws IOException
    Overrides:
      `doSetNextReader` in class `SimpleCollector`
    Throws:
      `IOException`

  - ### finish

    public void finish() throws IOException
    Throws:
      `IOException`

  - ### scoreMode

    public ScoreMode scoreMode()