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 >= 5only works with a number. With a string you get an error, or worse, a lexical comparison where"10" < "9". - Aggregations:
SUM,AVG,MAXneed numbers. - Sorting:
SORT total DESCon strings puts99.00before129.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, tsIn 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).