Iniciar sesión

FROM, KEEP and LIMIT

10 min · 2 ejercicio(s)

An ES|QL query is a pipeline. It starts with a source command that produces a table, followed by processing commands separated by the pipe character |. Each command receives the table of the previous one and returns a new table.

FROM lab-auth
| KEEP @timestamp, user.name, source.ip, event.outcome
| LIMIT 10
Command Purpose
FROM index Reads documents from one or more indices (FROM lab-auth, lab-web or FROM lab-*)
KEEP a, b Keeps only the listed columns, in this order (wildcards allowed: KEEP user.*)
DROP a Removes columns
RENAME a AS b Renames a column
LIMIT n Returns at most n rows (default 1,000)

Unlike KQL, the result of ES|QL is always a table with columns and rows, not a list of documents. That's why ES|QL can also calculate and aggregate.

Counting rows

The quickest way to count is STATS, which you will learn in detail in the next module:

FROM lab-web
| STATS requests = COUNT(*)

🔎 Profundiza: funciones, errores habituales y práctica

Source commands

Command Purpose
FROM read documents from indices, aliases or data streams; supports wildcards and METADATA _id, _index
ROW create a single row with literal values, great for testing functions: ROW ip = TO_IP("10.1.2.3") | EVAL internal = CIDR_MATCH(ip, "10.0.0.0/8")
SHOW INFO version information of the cluster

Shaping the table

  • KEEP defines the order of columns and supports wildcards: KEEP @timestamp, user.*.
  • DROP removes columns, RENAME old AS new renames them.
  • Columns that exist in one index but not another are null in rows from the other index.

Default limit

Without LIMIT ES|QL returns 1,000 rows. The maximum per query is 10,000 (configurable on the cluster). Aggregations with STATS work on all matching documents, only the output is limited.

Using it in Kibana

In Discover switch the query language to ES|QL. A STATS … BY with a date bucket draws a chart automatically, and you can save the query as a visualisation on a dashboard.

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. How big is the web log?

    How many documents are in the index lab-web?

  2. 2. Shape the table

    Return the first 20 events of lab-auth with only the columns @timestamp, user.name and source.ip (in this order). Your result is graded by the user.name values it contains.