Collector (Lucene 10.5.1 core API)
Description: declaration: package: org.apache.lucene.search, interface: Collector

Package org.apache.lucene.search

# Interface Collector

All Known Implementing Classes:
  `CachingCollector`, `FilterCollector`, `MultiCollector`, `PositiveScoresOnlyCollector`, `SimpleCollector`, `TopDocsCollector`, `TopFieldCollector`, `TopScoreDocCollector`, `TotalHitCountCollector`

public interface Collector
Expert: Collectors are primarily meant to be used to gather raw results from a search, and implement sorting or custom result filtering, collation, etc.

Lucene's core collectors are derived from `Collector` and `SimpleCollector`. Likely your application can use one of these classes, or subclass `TopDocsCollector`, instead of implementing Collector directly:

- `TopDocsCollector` is an abstract base class that assumes you will retrieve the top N docs, according to some criteria, after collection is done.
- `TopScoreDocCollector` is a concrete subclass `TopDocsCollector` and sorts according to score + docID. This is used internally by the `IndexSearcher` search methods that do not take an explicit `Sort`. It is likely the most frequently used collector.
- `TopFieldCollector` subclasses `TopDocsCollector` and sorts according to a specified `Sort` object (sort by field). This is used internally by the `IndexSearcher` search methods that take an explicit `Sort`.
- `PositiveScoresOnlyCollector` wraps any other Collector and prevents collection of hits whose score is <= 0.0

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

- ## Method Summary

  Modifier and Type
  Method
  Description
  `LeafCollector`
  `getLeafCollector(LeafReaderContext context)`
  Create a new `collector` to collect the given context.
  `ScoreMode`
  `scoreMode()`
  Indicates what features are required from the scorer.
  `default void`
  `setWeight(Weight weight)`
  Set the `Weight` that will be used to produce scorers that will feed `LeafCollector`s.

- ## Method Details

  - ### getLeafCollector

    LeafCollector getLeafCollector(LeafReaderContext context) throws IOException
    Create a new `collector` to collect the given context.
    Parameters:
      `context` - next atomic reader context
    Throws:
      `IOException`

  - ### scoreMode

    ScoreMode scoreMode()
    Indicates what features are required from the scorer.

  - ### setWeight

    default void setWeight(Weight weight)
    Set the `Weight` that will be used to produce scorers that will feed `LeafCollector`s. This is typically useful to have access to `Weight.count(org.apache.lucene.index.LeafReaderContext)` from `getLeafCollector(org.apache.lucene.index.LeafReaderContext)`.