Package org.apache.lucene.expressions.js
Class JavascriptCompiler
- java.lang.Object
-
- org.apache.lucene.expressions.js.JavascriptCompiler
-
public final class JavascriptCompiler extends Object
An expression compiler for javascript expressions.Example:
Expression foo = JavascriptCompiler.compile("((0.3*popularity)/10.0)+(0.7*score)");See the
package documentationfor the supported syntax and default functions.You can compile with an alternate set of functions via
compile(String, Map, ClassLoader). For example:Map<String,Method> functions = new HashMap<>(); // add all the default functions functions.putAll(JavascriptCompiler.DEFAULT_FUNCTIONS); // add cbrt() functions.put("cbrt", Math.class.getMethod("cbrt", double.class)); // call compile with customized function map Expression foo = JavascriptCompiler.compile("cbrt(score)+ln(popularity)", functions, getClass().getClassLoader());- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
-
Field Summary
Fields Modifier and Type Field Description static Map<String,Method>DEFAULT_FUNCTIONSThe default set of functions available to expressions.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Expressioncompile(String sourceText)Compiles the given expression using default compiler settings.static Expressioncompile(String sourceText, Map<String,Method> functions, ClassLoader parent)Compiles the given expression with the supplied custom functions using default compiler settings.
-
-
-
Field Detail
-
DEFAULT_FUNCTIONS
public static final Map<String,Method> DEFAULT_FUNCTIONS
The default set of functions available to expressions.See the
package documentationfor a list.
-
-
Method Detail
-
compile
public static Expression compile(String sourceText) throws ParseException
Compiles the given expression using default compiler settings.- Parameters:
sourceText- The expression to compile- Returns:
- A new compiled expression
- Throws:
ParseException- on failure to compile
-
compile
public static Expression compile(String sourceText, Map<String,Method> functions, ClassLoader parent) throws ParseException
Compiles the given expression with the supplied custom functions using default compiler settings.Functions must be
public static, returndoubleand can take from zero to 256doubleparameters.- Parameters:
sourceText- The expression to compilefunctions- map of String names to functionsparent- aClassLoaderthat should be used as the parent of the loaded class. It must contain all classes referred to by the givenfunctions.- Returns:
- A new compiled expression
- Throws:
ParseException- on failure to compile
-
-