case_greaterthan
Description
Returns a value based on whether a number is greater than one of several thresholds.
This function is a shorthand for case
expressions with >
(greater than) logic and helps shorten queries that would otherwise repeat conditional statements.
Note
Like all case statements, case_greaterthan
returns the first matching value. The order of clauses is important.
Syntax
case_greaterthan {
n: number,
value1: number -> result1,
value2: number -> result2,
...
valueN: number -> resultN,
_ -> default
}
Arguments
Name | Type | Required | Description |
---|---|---|---|
n | number | true | The numeric expression to compare |
value | number | true | A threshold value for comparison |
result | any | true | The value to return if n is greater than the threshold |
_ | any | false | Default value if no thresholds match |
Example
Use case: Map HTTP status codes to text descriptions
Suppose you want to create a field status_description
that provides a readable label for HTTP status codes. Consider these log documents:
The comparison thresholds should be listed in descending order, since the first match is returned. Also note that each threshold must be reduced by 1 because case_greaterthan
uses a strict >
operator (not >=
). For example, to capture 4xx codes, use 399
as the threshold, not 400
.
create status_description from
case_greaterthan {
$d.status_code,
499 -> 'server-error',
399 -> 'client-error',
299 -> 'redirection',
199 -> 'success',
99 -> 'information',
_ -> 'other'
}
Output
{
"status_code": 201,
"status_description": "success"
},
{
"status_code": 500,
"status_description": "server-error"
},
{
"status_code": 404,
"status_description": "client-error"
}