Package org.apache.lucene.expressions.js
Class JavascriptCompiler
java.lang.Object
org.apache.lucene.expressions.js.JavascriptCompiler
An expression compiler for javascript expressions.
Example:
Expression foo = JavascriptCompiler.compile("((0.3*popularity)/10.0)+(0.7*score)");
See the package documentation
for the supported
syntax and default functions.
You can compile with an alternate set of functions via compile(String, Map)
. For
example:
Map<String,MethodHandle> functions = new HashMap<>(); // add all the default functions functions.putAll(JavascriptCompiler.DEFAULT_FUNCTIONS); // add cbrt() functions.put("cbrt", MethodHandles.publicLookup().findStatic(Math.class, "cbrt", MethodType.methodType(double.class, double.class))); // call compile with customized function map Expression foo = JavascriptCompiler.compile("cbrt(score)+ln(popularity)", functions);
It is possible to pass any MethodHandle
as function that only takes double
parameters and returns a double
. The method does not need to be public, it just needs to
be resolved correctly using a private MethodHandles.Lookup
instance. Ideally the methods should be
static
, but you can use MethodHandle.bindTo(Object)
to bind it to a receiver.
- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Map
<String, MethodHandle> The default set of functions available to expressions. -
Method Summary
Modifier and TypeMethodDescriptionstatic Expression
Compiles the given expression using default compiler settings.static Expression
compile
(String sourceText, Map<String, MethodHandle> functions) Compiles the given expression with the supplied custom functions using default compiler settings.static Map
<String, MethodHandle> convertLegacyFunctions
(Map<String, Method> functions) Deprecated.Only use this to convert Lucene 9.x or earlier legacy code.
-
Field Details
-
DEFAULT_FUNCTIONS
The default set of functions available to expressions.See the
package documentation
for a list.
-
-
Method Details
-
compile
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, MethodHandle> functions) throws ParseExceptionCompiles the given expression with the supplied custom functions using default compiler settings.Functions must be
public static
, returndouble
and can take from zero to 256double
parameters.- Parameters:
sourceText
- The expression to compilefunctions
- map of String names toMethodHandle
s- Returns:
- A new compiled expression
- Throws:
ParseException
- on failure to compile
-
convertLegacyFunctions
@Deprecated public static Map<String,MethodHandle> convertLegacyFunctions(Map<String, Method> functions) throws IllegalAccessExceptionDeprecated.Only use this to convert Lucene 9.x or earlier legacy code. For new code useMethodHandle
.Converts a legacy map with reflectiveMethod
functions toMap<String,MethodHandle
for use withcompile(String, Map)
.- Parameters:
functions
- a map with only public and accessible reflective methods- Returns:
- a new (modifiable) map with the same function declarations, but converted to
MethodHandle
- Throws:
IllegalAccessException
- if any of the methods infunctions
are not accessible by the publicMethodHandles.Lookup
.
-