Skip to content

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

NameTypeRequiredDescription
nnumbertrueThe numeric expression to compare
valuenumbertrueA threshold value for comparison
resultanytrueThe value to return if n is greater than the threshold
_anyfalseDefault 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:

{
  "status_code": 201
},
{
  "status_code": 500
},
{
  "status_code": 404
}

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"
}