Expression (Lucene 10.5.1 expressions API)
Description: declaration: package: org.apache.lucene.expressions, class: Expression

Package org.apache.lucene.expressions

# Class Expression

java.lang.Object
org.apache.lucene.expressions.Expression

public abstract class Expression extends Object
Base class that computes the value of an expression for a document.

Example that sorts based on an expression:

       // compile an expression:
       Expression expr = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)");

       // SimpleBindings just maps variables to DoubleValuesSource instances
       SimpleBindings bindings = new SimpleBindings();
       bindings.add("_score", DoubleValuesSource.SCORES);
       bindings.add("popularity", DoubleValuesSource.fromIntField("popularity"));

       // create a sort field and sort by it (reverse order)
       Sort sort = new Sort(expr.getSortField(bindings, true));
       Query query = new TermQuery(new Term("body", "contents"));
       searcher.search(query, 10, sort);

Example that modifies the scores produced by the query:

       // compile an expression:
       Expression expr = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)");

       // SimpleBindings just maps variables to DoubleValuesSource instances
       SimpleBindings bindings = new SimpleBindings();
       bindings.add("_score", DoubleValuesSource.SCORES);
       bindings.add("popularity", DoubleValuesSource.fromIntField("popularity"));

       // create a query that matches based on body:contents but
       // scores using expr
       Query query = new FunctionScoreQuery(
           new TermQuery(new Term("body", "contents")),
           expr.getDoubleValuesSource(bindings));
       searcher.search(query, 10);

See Also:
  - `JavascriptCompiler.compile(java.lang.String)`
WARNING: This API is experimental and might change in incompatible ways in the next release.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `final String`
  `sourceText`
  The original source text
  `final String[]`
  `variables`
  Named variables referred to by this expression

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `Expression(String sourceText, String[] variables)`
  Creates a new `Expression`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `abstract double`
  `evaluate(DoubleValues[] functionValues)`
  Evaluates the expression for the current document.
  `DoubleValuesSource`
  `getDoubleValuesSource(Bindings bindings)`
  Get a DoubleValuesSource which can compute the value of this expression in the context of the given bindings.
  `Rescorer`
  `getRescorer(Bindings bindings)`
  Get a `Rescorer`, to rescore first-pass hits using this expression.
  `SortField`
  `getSortField(Bindings bindings, boolean reverse)`
  Get a sort field which can be used to rank documents by this expression.

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

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

- ## Field Details

  - ### sourceText

    public final String sourceText
    The original source text

  - ### variables

    public final String[] variables
    Named variables referred to by this expression

- ## Constructor Details

  - ### Expression

    protected Expression(String sourceText, String[] variables)
    Creates a new `Expression`.
    Parameters:
      `sourceText` - Source text for the expression: e.g. `ln(popularity)`
      `variables` - Names of external variables referred to by the expression

- ## Method Details

  - ### evaluate

    public abstract double evaluate(DoubleValues[] functionValues) throws IOException
    Evaluates the expression for the current document.
    Parameters:
      `functionValues` - `DoubleValues` for each element of `variables`.
    Returns:
      The computed value of the expression for the given document.
    Throws:
      `IOException`

  - ### getDoubleValuesSource

    public DoubleValuesSource getDoubleValuesSource(Bindings bindings)
    Get a DoubleValuesSource which can compute the value of this expression in the context of the given bindings.
    Parameters:
      `bindings` - Bindings to use for external values in this expression
    Returns:
      A DoubleValuesSource which will evaluate this expression when used

  - ### getSortField

    public SortField getSortField(Bindings bindings, boolean reverse)
    Get a sort field which can be used to rank documents by this expression.

  - ### getRescorer

    public Rescorer getRescorer(Bindings bindings)
    Get a `Rescorer`, to rescore first-pass hits using this expression.