public abstract class Expression
extends java.lang.Object
Example that sorts based on an expression:
// compile an expression: Expression expr = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)"); // SimpleBindings just maps variables to SortField instances SimpleBindings bindings = new SimpleBindings(); bindings.add(new SortField("_score", SortField.Type.SCORE)); bindings.add(new SortField("popularity", SortField.Type.INT)); // 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 SortField instances SimpleBindings bindings = new SimpleBindings(); bindings.add(new SortField("_score", SortField.Type.SCORE)); bindings.add(new SortField("popularity", SortField.Type.INT)); // 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);
Modifier and Type | Field and Description |
---|---|
java.lang.String |
sourceText
The original source text
|
java.lang.String[] |
variables
Named variables referred to by this expression
|
Modifier | Constructor and Description |
---|---|
protected |
Expression(java.lang.String sourceText,
java.lang.String[] variables)
Creates a new
Expression . |
Modifier and Type | Method and 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.
|
public final java.lang.String sourceText
public final java.lang.String[] variables
protected Expression(java.lang.String sourceText, java.lang.String[] variables)
Expression
.sourceText
- Source text for the expression: e.g. ln(popularity)
variables
- Names of external variables referred to by the expressionpublic abstract double evaluate(DoubleValues[] functionValues)
functionValues
- DoubleValues
for each element of variables
.public DoubleValuesSource getDoubleValuesSource(Bindings bindings)
bindings
- Bindings to use for external values in this expressionpublic SortField getSortField(Bindings bindings, boolean reverse)
Copyright © 2000–2019 The Apache Software Foundation. All rights reserved.