Skip to content

case_lessthan

Description

Returns a value based on whether a number is less than one of several thresholds.

This function is a shorthand for case expressions with < (less than) logic and helps shorten queries that would otherwise repeat conditional statements.

Note

Like all case statements, case_lessthan returns the first matching value. The order of clauses is important.

Syntax

case_lessthan {
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 less than the threshold
_anyfalseDefault value if no thresholds match

Example

Use case: Map temperature values to descriptions

Suppose you want to create a field temperature_description that provides a readable label for temperatures in Celsius. Consider these log documents:

{
  "temperature_celsius": 10
},
{
  "temperature_celsius": 50
},
{
  "temperature_celsius": 20
}

The comparison thresholds should be listed in ascending order, since the first match is returned. For example, a value of 20 is less than both 45 and 30, but the correct match is 30 -> 'fun' because it appears first in ascending order.

create temperature_description from
case_lessthan {
  $d.temperature_celsius,
  10 -> 'freezing',
  20 -> 'cold',
  30 -> 'fun',
  45 -> 'hot',
  _  -> 'burning'
}

Output

{
  "temperature_celsius": 10,
  "temperature_description": "freezing"
},
{
  "temperature_celsius": 50,
  "temperature_description": "burning"
},
{
  "temperature_celsius": 20,
  "temperature_description": "cold"
}