Skip to content

Latest commit

 

History

History
58 lines (44 loc) · 1.34 KB

File metadata and controls

58 lines (44 loc) · 1.34 KB
endpoint esql.query
lang python
es_version 9.3
client elasticsearch==9.3.0

Elasticsearch 9.3 esql.query endpoint (Python example)

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.

Aggregations

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)

Columnar format

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}")