Class CustomScoreQuery

java.lang.Object
org.apache.lucene.search.Query
org.apache.lucene.queries.CustomScoreQuery
All Implemented Interfaces:
Cloneable

public class CustomScoreQuery extends Query
Query that sets document score as a programmatic function of several (sub) scores:
  1. the score of its subQuery (any query)
  2. (optional) the score of its FunctionQuery (or queries).
Subclasses can modify the computation by overriding getCustomScoreProvider(org.apache.lucene.index.AtomicReaderContext).
  • Constructor Details

    • CustomScoreQuery

      public CustomScoreQuery(Query subQuery)
      Create a CustomScoreQuery over input subQuery.
      Parameters:
      subQuery - the sub query whose scored is being customized. Must not be null.
    • CustomScoreQuery

      public CustomScoreQuery(Query subQuery, FunctionQuery scoringQuery)
      Create a CustomScoreQuery over input subQuery and a FunctionQuery.
      Parameters:
      subQuery - the sub query whose score is being customized. Must not be null.
      scoringQuery - a value source query whose scores are used in the custom score computation. This parameter is optional - it can be null.
    • CustomScoreQuery

      public CustomScoreQuery(Query subQuery, FunctionQuery... scoringQueries)
      Create a CustomScoreQuery over input subQuery and a FunctionQuery.
      Parameters:
      subQuery - the sub query whose score is being customized. Must not be null.
      scoringQueries - value source queries whose scores are used in the custom score computation. This parameter is optional - it can be null or even an empty array.
  • Method Details

    • rewrite

      public Query rewrite(IndexReader reader) throws IOException
      Description copied from class: Query
      Expert: called to re-write queries into primitive queries. For example, a PrefixQuery will be rewritten into a BooleanQuery that consists of TermQuerys.
      Overrides:
      rewrite in class Query
      Throws:
      IOException
    • extractTerms

      public void extractTerms(Set<Term> terms)
      Description copied from class: Query
      Expert: adds all terms occurring in this query to the terms set. Only works if this query is in its rewritten form.
      Overrides:
      extractTerms in class Query
    • clone

      public CustomScoreQuery clone()
      Description copied from class: Query
      Returns a clone of this query.
      Overrides:
      clone in class Query
    • toString

      public String toString(String field)
      Description copied from class: Query
      Prints a query to a string, with field assumed to be the default field and omitted.
      Specified by:
      toString in class Query
    • equals

      public boolean equals(Object o)
      Returns true if o is equal to this.
      Overrides:
      equals in class Query
    • hashCode

      public int hashCode()
      Returns a hash code value for this object.
      Overrides:
      hashCode in class Query
    • createWeight

      public Weight createWeight(IndexSearcher searcher) throws IOException
      Description copied from class: Query
      Expert: Constructs an appropriate Weight implementation for this query.

      Only implemented by primitive queries, which re-write to themselves.

      Overrides:
      createWeight in class Query
      Throws:
      IOException
    • isStrict

      public boolean isStrict()
      Checks if this is strict custom scoring. In strict custom scoring, the ValueSource part does not participate in weight normalization. This may be useful when one wants full control over how scores are modified, and does not care about normalizing by the ValueSource part. One particular case where this is useful if for testing this query.

      Note: only has effect when the ValueSource part is not null.

    • setStrict

      public void setStrict(boolean strict)
      Set the strict mode of this query.
      Parameters:
      strict - The strict mode to set.
      See Also:
    • getSubQuery

      public Query getSubQuery()
      The sub-query that CustomScoreQuery wraps, affecting both the score and which documents match.
    • getScoringQueries

      public Query[] getScoringQueries()
      The scoring queries that only affect the score of CustomScoreQuery.
    • name

      public String name()
      A short name of this query, used in toString(String).