Class RankingExpression
java.lang.Object
com.yahoo.searchlib.rankingexpression.RankingExpression
- All Implemented Interfaces:
Serializable
A ranking expression. Ranking expressions are used to calculate a rank score for a searched instance from a set of rank features.
A ranking expression wraps a expression node tree and may also optionally have a name.
The identity of a ranking expression is decided by both its name and expression tree. Two expressions which looks the same in string form are the same.
Simple usage
try {
MapContext context = new MapContext();
context.put("one", 1d);
RankingExpression expression = new RankingExpression("10*if(i>35,if(i>one,if(i>=670,4,8),if(i>8000,5,3)),if(i==478,90,91))");
double result = expression.evaluate(context);
}
catch (ParseException e) {
throw new RuntimeException(e);
}
Or, usage optimized for repeated evaluation of the same expression
// Members in a class living across multiple evaluations
RankingExpression expression;
ArrayContext contextPrototype;
...
// Initialization of the above members (once)
// Create reusable, gbdt optimized expression and context.
// The expression is multithread-safe while the context created is not
try {
RankingExpression expression = new RankingExpression("10*if(i>35,if(i>one,if(i>=670,4,8),if(i>8000,5,3)),if(i==478,90,91))");
ArrayContext contextPrototype = new ArrayContext(expression);
ExpressionOptimizer optimizer = new ExpressionOptimizer(); // Increases evaluation speed of gbdt form expressions by 3-4x
OptimizationReport triviaAboutTheOptimization = optimizer.optimize(expression, contextPrototype);
}
catch (ParseException e) {
throw new RuntimeException(e);
}
...
// Execution (many)
context = contextPrototype.clone(); // If evaluation is multithreaded - skip this if execution is single-threaded
context.put("one",1d);
double result = expression.evaluate(context);
- Author:
- Simon Thoresen Hult, bratseth
- See Also:
-
Constructor Summary
ConstructorDescriptionCreates a ranking expression from an expression root node.RankingExpression
(File file) Creates a ranking expression from a file.RankingExpression
(Reader reader) Creates an anonymous ranking expression by consuming from the readerRankingExpression
(String expression) Creates a ranking expression from a stringRankingExpression
(String name, ExpressionNode root) Creates a named ranking expression from an expression root node.RankingExpression
(String name, Reader reader) Creates a new ranking expression by consuming from the readerRankingExpression
(String name, String expression) Creates a new ranking expression by consuming from the reader -
Method Summary
Modifier and TypeMethodDescriptioncopy()
Returns a deep copy of this expressionboolean
Returns the value of evaluating this expression over the given context.static String
extractScriptName
(String propertyName) static RankingExpression
Creates a ranking expression from a stringgetName()
Returns the name of this ranking expression, or "" if no name is set.getRankProperties
(SerializationContext context) Creates the necessary rank properties required to implement this expression.getRankProperties
(List<ExpressionFunction> functions) Deprecated.getRoot()
Returns the root of the expression tree of this expression.int
hashCode()
static String
propertyExpressionName
(String expressionName) static String
propertyName
(String expressionName) Returns the rank-property name for a given expression name.void
Sets the name of this ranking expression.void
setRoot
(ExpressionNode root) Sets the root of the expression tree of this expression.toString()
com.yahoo.tensor.TensorType
Validates the type correctness of the given expression with the given context and returns the type this expression will produce from the given type context
-
Constructor Details
-
RankingExpression
Creates an anonymous ranking expression by consuming from the reader- Throws:
ParseException
-
RankingExpression
Creates a new ranking expression by consuming from the reader- Parameters:
name
- the name of the ranking expressionreader
- the reader that contains the string to parse.- Throws:
ParseException
- if the string could not be parsed.
-
RankingExpression
Creates a new ranking expression by consuming from the reader- Parameters:
name
- the name of the ranking expressionexpression
- the expression to parse.- Throws:
ParseException
- if the string could not be parsed.
-
RankingExpression
Creates a ranking expression from a string- Parameters:
expression
- The reader that contains the string to parse.- Throws:
ParseException
- if the string could not be parsed.
-
RankingExpression
Creates a ranking expression from a file. For convenience, the file.getName() up to any dot becomes the name of this expression.- Parameters:
file
- the name of the file whose content to parse.- Throws:
ParseException
- if the string could not be parsed.IllegalArgumentException
- if the file could not be found
-
RankingExpression
Creates a named ranking expression from an expression root node. -
RankingExpression
Creates a ranking expression from an expression root node.- Parameters:
root
- The root node.
-
-
Method Details
-
copy
Returns a deep copy of this expression -
getName
Returns the name of this ranking expression, or "" if no name is set.- Returns:
- The name of this expression.
-
setName
Sets the name of this ranking expression.- Parameters:
name
- The name to set.
-
getRoot
Returns the root of the expression tree of this expression.- Returns:
- The root node.
-
setRoot
Sets the root of the expression tree of this expression.- Parameters:
root
- The root node to set.
-
hashCode
public int hashCode() -
equals
-
toString
-
getRankProperties
Creates the necessary rank properties required to implement this expression.- Parameters:
context
- context for serialization- Returns:
- a list of named rank properties required to implement this expression
-
getRankProperties
Deprecated. -
propertyName
Returns the rank-property name for a given expression name.- Parameters:
expressionName
- the expression name to mangle.- Returns:
- the property name.
-
propertyExpressionName
-
extractScriptName
-
type
Validates the type correctness of the given expression with the given context and returns the type this expression will produce from the given type context- Throws:
IllegalArgumentException
- if this expression is not type correct in this context
-
evaluate
Returns the value of evaluating this expression over the given context.- Parameters:
context
- The variable bindings to use for this evaluation.- Returns:
- the evaluation result.
- Throws:
IllegalArgumentException
- if there are variables which are not bound in the given map
-
from
Creates a ranking expression from a string- Throws:
IllegalArgumentException
- if the string is not a valid ranking expression
-