org.apache.lucene.search.highlight

## Class QueryScorer

- java.lang.Object
- - org.apache.lucene.search.highlight.QueryScorer

- All Implemented Interfaces:
    Scorer

      public class QueryScorer
      extends Object
      implements Scorer

  `Scorer` implementation which scores text fragments by the number of unique query terms found. This class converts appropriate `Query`s to `SpanQuery`s and attempts to score only those terms that participated in generating the 'hit' on the document.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `QueryScorer(Query query)`
    `QueryScorer(Query query, IndexReader reader, String field)`
    `QueryScorer(Query query, IndexReader reader, String field, String defaultField)`
    `QueryScorer(Query query, String field)`
    `QueryScorer(Query query, String field, String defaultField)`
    `QueryScorer(WeightedSpanTerm[] weightedTerms)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `float` | `getFragmentScore()` Called when the `Highlighter` has no more tokens for the current fragment - the Scorer returns the weighting it has derived for the most recent fragment, typically based on the results of `Scorer.getTokenScore()`.
    `float` | `getMaxTermWeight()`
    `float` | `getTokenScore()` Called for each token in the current fragment.
    `WeightedSpanTerm` | `getWeightedSpanTerm(String token)` Retrieve the `WeightedSpanTerm` for the specified token.
    `TokenStream` | `init(TokenStream tokenStream)` Called to init the Scorer with a `TokenStream`.
    `boolean` | `isExpandMultiTermQuery()`
    `boolean` | `isUsePayloads()` Whether or not we should capture payloads in `MemoryIndex` at each position so that queries can access them.
    `protected WeightedSpanTermExtractor` | `newTermExtractor(String defaultField)`
    `void` | `setExpandMultiTermQuery(boolean expandMultiTermQuery)` Controls whether or not multi-term queries are expanded against a `MemoryIndex` `IndexReader`.
    `void` | `setMaxDocCharsToAnalyze(int maxDocCharsToAnalyze)`
    `void` | `setUsePayloads(boolean usePayloads)`
    `void` | `setWrapIfNotCachingTokenFilter(boolean wrap)` By default, `TokenStream`s that are not of the type `CachingTokenFilter` are wrapped in a `CachingTokenFilter` to ensure an efficient reset - if you are already using a different caching `TokenStream` impl and you don't want it to be wrapped, set this to false.
    `void` | `startFragment(TextFragment newFragment)` Called when a new fragment is started for consideration.

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

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

- - ### Constructor Detail

    - #### QueryScorer

          public QueryScorer(Query query)

      Parameters:
        `query` - Query to use for highlighting

    - #### QueryScorer

          public QueryScorer(Query query,
                             String field)

      Parameters:
        `query` - Query to use for highlighting
        `field` - Field to highlight - pass null to ignore fields

    - #### QueryScorer

          public QueryScorer(Query query,
                             IndexReader reader,
                             String field)

      Parameters:
        `query` - Query to use for highlighting
        `field` - Field to highlight - pass null to ignore fields
        `reader` - `IndexReader` to use for quasi tf/idf scoring

    - #### QueryScorer

          public QueryScorer(Query query,
                             IndexReader reader,
                             String field,
                             String defaultField)

      Parameters:
        `query` - to use for highlighting
        `reader` - `IndexReader` to use for quasi tf/idf scoring
        `field` - to highlight - pass null to ignore fields

    - #### QueryScorer

          public QueryScorer(Query query,
                             String field,
                             String defaultField)

      Parameters:
        `defaultField` - - The default field for queries with the field name unspecified

    - #### QueryScorer

          public QueryScorer(WeightedSpanTerm[] weightedTerms)

      Parameters:
        `weightedTerms` - an array of pre-created `WeightedSpanTerm`s

  - ### Method Detail

    - #### getFragmentScore

          public float getFragmentScore()

      Description copied from interface: `Scorer`
      Called when the `Highlighter` has no more tokens for the current fragment - the Scorer returns the weighting it has derived for the most recent fragment, typically based on the results of `Scorer.getTokenScore()`.
      Specified by:
        `getFragmentScore` in interface `Scorer`

    - #### getMaxTermWeight

          public float getMaxTermWeight()

      Returns:
        The highest weighted term (useful for passing to GradientFormatter to set top end of coloring scale).

    - #### getTokenScore

          public float getTokenScore()

      Description copied from interface: `Scorer`
      Called for each token in the current fragment. The `Highlighter` will increment the `TokenStream` passed to init on every call.
      Specified by:
        `getTokenScore` in interface `Scorer`
      Returns:
        a score which is passed to the `Highlighter` class to influence the mark-up of the text (this return value is NOT used to score the fragment)

    - #### init

          public TokenStream init(TokenStream tokenStream)
                           throws IOException

      Description copied from interface: `Scorer`
      Called to init the Scorer with a `TokenStream`. You can grab references to the attributes you are interested in here and access them from `Scorer.getTokenScore()`.
      Specified by:
        `init` in interface `Scorer`
      Parameters:
        `tokenStream` - the `TokenStream` that will be scored.
      Returns:
        either a `TokenStream` that the Highlighter should continue using (eg if you read the tokenSream in this method) or null to continue using the same `TokenStream` that was passed in.
      Throws:
        `IOException` - If there is a low-level I/O error

    - #### getWeightedSpanTerm

          public WeightedSpanTerm getWeightedSpanTerm(String token)

      Retrieve the `WeightedSpanTerm` for the specified token. Useful for passing Span information to a `Fragmenter`.
      Parameters:
        `token` - to get `WeightedSpanTerm` for
      Returns:
        WeightedSpanTerm for token

    - #### newTermExtractor

          protected WeightedSpanTermExtractor newTermExtractor(String defaultField)

    - #### startFragment

          public void startFragment(TextFragment newFragment)

      Description copied from interface: `Scorer`
      Called when a new fragment is started for consideration.
      Specified by:
        `startFragment` in interface `Scorer`
      Parameters:
        `newFragment` - the fragment that will be scored next

    - #### isExpandMultiTermQuery

          public boolean isExpandMultiTermQuery()

      Returns:
        true if multi-term queries should be expanded

    - #### setExpandMultiTermQuery

          public void setExpandMultiTermQuery(boolean expandMultiTermQuery)

      Controls whether or not multi-term queries are expanded against a `MemoryIndex` `IndexReader`.
      Parameters:
        `expandMultiTermQuery` - true if multi-term queries should be expanded

    - #### isUsePayloads

          public boolean isUsePayloads()

      Whether or not we should capture payloads in `MemoryIndex` at each position so that queries can access them. This does not apply to term vector based TokenStreams, which support payloads only when the term vector has them.

    - #### setUsePayloads

          public void setUsePayloads(boolean usePayloads)

    - #### setWrapIfNotCachingTokenFilter

          public void setWrapIfNotCachingTokenFilter(boolean wrap)

      By default, `TokenStream`s that are not of the type `CachingTokenFilter` are wrapped in a `CachingTokenFilter` to ensure an efficient reset - if you are already using a different caching `TokenStream` impl and you don't want it to be wrapped, set this to false. Note that term-vector based tokenstreams are detected and won't be wrapped either.

    - #### setMaxDocCharsToAnalyze

          public void setMaxDocCharsToAnalyze(int maxDocCharsToAnalyze)

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