| endpoint | esql.query |
|---|---|
| lang | python |
| es_version | 9.3 |
| client | elasticsearch==9.3.0 |
Use client.esql.query() to run an ES|QL query. ES|QL uses a
pipe-based syntax for filtering, transforming, and aggregating data:
response = client.esql.query(
query="FROM products | WHERE category == \"electronics\" | SORT price DESC | LIMIT 10",
format="json",
)
for row in response["values"]:
print(row)The response includes a columns array describing each field's
name and type, and a values array with the data rows.
Use STATS ... BY to compute aggregations grouped by a field:
response = client.esql.query(
query="FROM products | STATS avg_price = AVG(price), count = COUNT(*) BY category",
format="json",
)
for col in response["columns"]:
print(f"{col['name']} ({col['type']})", end=" ")
print()
for row in response["values"]:
print(row)Set columnar=True to receive results as column arrays instead of
rows. This is more efficient for analytics workloads:
response = client.esql.query(
query="FROM products | SORT price DESC | LIMIT 10",
format="json",
columnar=True,
)
for col, values in zip(response["columns"], response["values"]):
print(f"{col['name']}: {values}")