org.apache.lucene.search

## Class EarlyTerminatingSortingCollector

- java.lang.Object
- - org.apache.lucene.search.FilterCollector
  - - org.apache.lucene.search.EarlyTerminatingSortingCollector

- All Implemented Interfaces:
    Collector

      public class EarlyTerminatingSortingCollector
      extends FilterCollector

  A `Collector` that early terminates collection of documents on a per-segment basis, if the segment was sorted according to the given `Sort`.

  NOTE: the `Collector` detects segments sorted according to a `SortingMergePolicy`'s `Sort` and so it's best used in conjunction with a `SortingMergePolicy`. Also,it collects up to a specified `numDocsToCollect` from each segment, and therefore is mostly suitable for use in conjunction with collectors such as `TopDocsCollector`, and not e.g. `TotalHitCountCollector`.

  NOTE: If you wrap a `TopDocsCollector` that sorts in the same order as the index order, the returned `TopDocs` will be correct. However the total of `hit count` will be underestimated since not all matching documents will have been collected.

  NOTE: This `Collector` uses `Sort.toString()` to detect whether a segment was sorted with the same `Sort`. This has two implications:

  - if a custom comparator is not implemented correctly and returns different identifiers for equivalent instances, this collector will not detect sorted segments,
  - if you suddenly change the `IndexWriter`'s `SortingMergePolicy` to sort according to another criterion and if both the old and the new `Sort`s have the same identifier, this `Collector` will incorrectly detect sorted segments.
  WARNING: This API is experimental and might change in incompatible ways in the next release.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected int` | `numDocsToCollect` Number of documents to collect in each segment
    `protected Sort` | `sort` Sort used to sort the search results

    - ### Fields inherited from class org.apache.lucene.search.FilterCollector

      `in`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `EarlyTerminatingSortingCollector(Collector in, Sort sort, int numDocsToCollect, Sort mergePolicySort)` Create a new `EarlyTerminatingSortingCollector` instance.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static boolean` | `canEarlyTerminate(Sort searchSort, Sort mergePolicySort)` Returns whether collection can be early-terminated if it sorts with the provided `Sort` and if segments are merged with the provided `Sort`.
    `LeafCollector` | `getLeafCollector(LeafReaderContext context)`
    `boolean` | `terminatedEarly()`

    - ### Methods inherited from class org.apache.lucene.search.FilterCollector

      `needsScores, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### sort

          protected final Sort sort

      Sort used to sort the search results

    - #### numDocsToCollect

          protected final int numDocsToCollect

      Number of documents to collect in each segment

  - ### Constructor Detail

    - #### EarlyTerminatingSortingCollector

          public EarlyTerminatingSortingCollector(Collector in,
                                          Sort sort,
                                          int numDocsToCollect,
                                          Sort mergePolicySort)

      Create a new `EarlyTerminatingSortingCollector` instance.
      Parameters:
        `in` - the collector to wrap
        `sort` - the sort you are sorting the search results on
        `numDocsToCollect` - the number of documents to collect on each segment. When wrapping a `TopDocsCollector`, this number should be the number of hits.
        `mergePolicySort` - the sort your `SortingMergePolicy` uses
      Throws:
        `IllegalArgumentException` - if the sort order doesn't allow for early termination with the given merge policy.

  - ### Method Detail

    - #### canEarlyTerminate

          public static boolean canEarlyTerminate(Sort searchSort,
                                  Sort mergePolicySort)

      Returns whether collection can be early-terminated if it sorts with the provided `Sort` and if segments are merged with the provided `Sort`.

    - #### getLeafCollector

          public LeafCollector getLeafCollector(LeafReaderContext context)
                                         throws IOException

      Specified by:
        `getLeafCollector` in interface `Collector`
      Overrides:
        `getLeafCollector` in class `FilterCollector`
      Throws:
        `IOException`

    - #### terminatedEarly

          public boolean terminatedEarly()

Copyright © 2000-2017 Apache Software Foundation. All Rights Reserved.