org.apache.lucene.expressions
Class Expression

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

public abstract class Expression
extends Object

Base class that computes the value of an expression for a document.

Example usage:

   // 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, null, 10, sort);
 

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

Field Summary
 String sourceText
          The original source text
 String[] variables
          Named variables referred to by this expression
 
Constructor Summary
protected Expression(String sourceText, String[] variables)
          Creates a new Expression.
 
Method Summary
abstract  double evaluate(int document, FunctionValues[] functionValues)
          Evaluates the expression for the given document.
 SortField getSortField(Bindings bindings, boolean reverse)
          Get a sort field which can be used to rank documents by this expression.
 ValueSource getValueSource(Bindings bindings)
          Get a value source which can compute the value of this expression in the context of the given bindings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sourceText

public final String sourceText
The original source text


variables

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

Constructor Detail

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 Detail

evaluate

public abstract double evaluate(int document,
                                FunctionValues[] functionValues)
Evaluates the expression for the given document.

Parameters:
document - docId of the document to compute a value for
functionValues - FunctionValues for each element of variables.
Returns:
The computed value of the expression for the given document.

getValueSource

public ValueSource getValueSource(Bindings bindings)
Get a value source 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 value source 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.



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