The Binary Functions class contains many common mathematical functions. These all take two Field Expressions as arguments.
Available Functions:
- ADD - addition
- SUBTRACT - subtraction
- MULTIPLY - multiplication
- DIVIDE - division (NOTE: division by zero yields zero result for int and long values)
- ATAN2 - Math.atan2
- LOG - logarithm
- POW - raise base to specified power
- MIN2 - Return minimal value
- MAX2 - Return maximal value
Multi-Value Inputs
If the arguments to the binary field expression are multi-valued, they will be matched up in order for computation.
For example, adding two fields with the values {2, 3, 4}, and {3, 5, 1} will result in {5, 8, 5}. By default, if the arguments have a different number of field values, the result will be truncated to the shortest. Alternatively, the last value of the shortest argument can be repeated using the MultiValueMode.REPEAT. In this case, adding {1} to {2, 3, 4} will result in {3, 4, 5} (instead of just {3}).
Java Example:
// Assumes the "value1" and "value2" are numeric fields QueryRequest request = new QueryRequest("*:*"); request.addField( new BinaryMathExpression(BinaryMathExpression.Method.ADD, new StoredField("field1"), new StoredField("field2")) ); // ... (Perform Search)
REST Syntax:
FUNCTIONNAME(FIELDEXPRESSION, FIELD_EXPRESSION[, MULTI_VALUE_MODE])
- FUNCTIONNAME - any of the available field function names specified above. (ADD, SUBTRACT, etc)
- FIELDEXPRESSION - any arbitrary field expression
- MULTI_VALUE_MODE - the multi-value mode to use (TRUNCATE (default) or REPEAT)
Example:
# Assumes the "field1" and "field2" are numeric fields &fields=ADD(field1, field2)