ES|QL Query Builder
editES|QL Query Builder
editThis functionality is in technical preview and may be changed or removed in a future release. Elastic will work to fix any issues, but features in technical preview are not subject to the support SLA of official GA features.
The ES|QL Query Builder allows you to construct ES|QL queries using Python syntax. Consider the following example:
>>> from elasticsearch.esql import ESQL >>> query = ( ESQL.from_("employees") .sort("emp_no") .keep("first_name", "last_name", "height") .eval(height_feet="height * 3.281", height_cm="height * 100") .limit(3) )
You can then see the assembled ES|QL query by printing the resulting query object:
>>> print(query) FROM employees | SORT emp_no | KEEP first_name, last_name, height | EVAL height_feet = height * 3.281, height_cm = height * 100 | LIMIT 3
To execute this query, you can pass it to the client.esql.query()
endpoint:
>>> from elasticsearch import Elasticsearch >>> client = Elasticsearch(hosts=[os.environ['ELASTICSEARCH_URL']]) >>> response = client.esql.query(query=query)
The response body contains a columns
attribute with the list of columns included in the results, and a values
attribute with the list of results for the query, each given as a list of column values. Here is a possible response body returned by the example query given above:
>>> from pprint import pprint >>> pprint(response.body) {'columns': [{'name': 'first_name', 'type': 'text'}, {'name': 'last_name', 'type': 'text'}, {'name': 'height', 'type': 'double'}, {'name': 'height_feet', 'type': 'double'}, {'name': 'height_cm', 'type': 'double'}], 'is_partial': False, 'took': 11, 'values': [['Adrian', 'Wells', 2.424, 7.953144, 242.4], ['Aaron', 'Gonzalez', 1.584, 5.1971, 158.4], ['Miranda', 'Kramer', 1.55, 5.08555, 155]]}