Iniciar sesión

Types and conversion

12 min · 2 ejercicio(s)

Everything Grok captures is a string unless you say otherwise. Add the target type as a third part of the pattern:

%{SYNTAX:SEMANTIC:TYPE}
FROM lab-raw
| WHERE event.dataset == "kestrel.shop"
| GROK message "order_id=%{NOTSPACE:order_id} customer=%{NOTSPACE:customer} items=%{INT:items:int} total=%{NUMBER:total:double}"
| WHERE items >= 5
| KEEP order_id, items, total
Type ES|QL Ingest pipeline
int integer int (long too)
long long long
float double, but parsed with 32-bit precision float
double double double
boolean boolean ("true" → true, everything else false) boolean

Why types matter

  • Comparisons: items >= 5 only works with a number. With a string you get an error, or worse, a lexical comparison where "10" < "9".
  • Aggregations: SUM, AVG, MAX need numbers.
  • Sorting: SORT total DESC on strings puts 99.00 before 129.90.

You can also convert after parsing with TO_INTEGER(items), TO_DOUBLE(total) or TO_IP(client). The :int suffix is just shorter.

Rounding money

Use :double for amounts. :float only keeps about seven significant digits: summing the week's orders with :float returns 35000.35998916626 instead of 35000.36. Even doubles are binary floating point, so round results you show to people: ROUND(SUM(total), 2), or ROUND(SUM(total)) for whole euros.

🔎 Profundiza: funciones, errores habituales y práctica

Timestamps

Grok captures timestamps as strings, too. In ES|QL convert them with DATE_PARSE:

FROM lab-raw
| WHERE event.dataset == "nginx.access"
| GROK message """\[%{HTTPDATE:time}\]"""
| EVAL ts = DATE_PARSE("dd/MMM/yyyy:HH:mm:ss Z", time)
| KEEP time, ts

In an ingest pipeline you follow the grok processor with a date processor that writes the parsed time into @timestamp. Syslog timestamps have no year: the date processor assumes the current one, which breaks around New Year. Prefer sources that log ISO 8601.

IP addresses

An IP captured by Grok is a string. To use CIDR functions such as CIDR_MATCH(client, "10.0.0.0/8") or to join with an ip field, convert it with TO_IP(client).

Ejercicios prácticos de esta lección

Resuelve estos ejercicios con una consulta en el laboratorio Elasticsearch real. Inicia sesión gratis para acceder al laboratorio, las pistas y la corrección automática.

  1. 1. Numbers you can sum

    Extract the order amount of every order placed line into total, as a number the lab can sum:

    2026-03-09T06:22:33.850Z INFO  [checkout] order placed order_id=ORD-2026-004014 customer=cust-52444 items=2 total=279.58 currency=EUR client=85.129.181.102

    The lab computes ROUND(SUM(total), 2). With a string field, that fails.

  2. 2. Compare as numbers

    Extract the number of items of every order (items=3) into items so that the lab can filter WHERE items > 4. Comparing a string with a number fails.