ES|QL piped queries

Elasticsearch · Practical · 10 min

ES|QL makes analytics and log exploration readable without deeply nested JSON.

ES|QL (Elasticsearch Query Language) is a piped query language: a source command followed by processing commands separated by |, each transforming the table of results from the previous step.

  • FROM logs-* selects indices, data streams or aliases (wildcards allowed),
  • WHERE status >= 500 AND host LIKE "web-*" filters,
  • EVAL ms = duration / 1000000 computes columns, KEEP and DROP choose them, RENAME renames,
  • STATS errors = COUNT(*), p95 = PERCENTILE(latency, 95) BY host aggregates,
  • SORT errors DESC | LIMIT 10 orders and limits,
  • DISSECT and GROK parse strings into fields at query time, ENRICH adds data from enrich policies, and LOOKUP JOIN joins with a lookup index.

Send it with POST /_query and a "query" string; add ?format=txt or csv for tabular output. Kibana Discover supports ES|QL directly.

Unlike the Query DSL, which is built for relevance-ranked search, ES|QL is designed for analytics and runs on a dedicated compute engine that processes data in blocks, in parallel across shards. Results without an explicit LIMIT are capped at 1,000 rows by default.

Pass values as parameters (? placeholders with a "params" array) instead of concatenating user input into the query string.

Example

POST /_query?format=txt
{
  "query": "FROM orders | WHERE placed_at >= NOW() - 30 days AND status == ? | STATS revenue = SUM(total), orders = COUNT(*) BY customer.raw | SORT revenue DESC | LIMIT 3",
  "params": ["paid"]
}
#     revenue    |    orders     | customer.raw
# ---------------+---------------+---------------
# 1840.5         |12             |Ana Petrosyan
# 1290.0         |9              |Ben Sargsyan
# 977.25         |7              |Cy Hakobyan

Remember

  • FROM source | command | command ... builds a table step by step
  • WHERE, EVAL, STATS ... BY, SORT, LIMIT cover most analytics
  • POST /_query; use format=txt or csv for tables
  • Use params instead of string concatenation

Watch out for

  • Forgetting the implicit 1,000-row limit and assuming you got the full result.

Check yourself

  1. Which ES|QL fragment counts requests per host?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: www.elastic.co/guide/en/elasticsearch/reference/current/esql.html