|
| 1 | +# ES|QL Query Builder |
| 2 | + |
| 3 | +The ES|QL Query Builder allows you to construct ES|QL queries using Python syntax. Consider the following example: |
| 4 | + |
| 5 | +```python |
| 6 | +>>> from elasticsearch.esql import ESQL |
| 7 | +>>> query = ( |
| 8 | + ESQL.from_("employees") |
| 9 | + .sort("emp_no") |
| 10 | + .keep("first_name", "last_name", "height") |
| 11 | + .eval(height_feet="height * 3.281", height_cm="height * 100") |
| 12 | +) |
| 13 | +``` |
| 14 | + |
| 15 | +You can then see the assembled ES|QL query by printing the resulting query object: |
| 16 | + |
| 17 | +```python |
| 18 | +>>> query |
| 19 | +FROM employees |
| 20 | +| SORT emp_no |
| 21 | +| KEEP first_name, last_name, height |
| 22 | +| EVAL height_feet = height * 3.281, height_cm = height * 100 |
| 23 | +``` |
| 24 | + |
| 25 | +To execute this query, you can cast it to a string and pass the string to the `client.esql.query()` endpoint: |
| 26 | + |
| 27 | +```python |
| 28 | +>>> response = client.esql.query(query=str(query)) |
| 29 | +``` |
| 30 | + |
| 31 | +The response containts a `columns` attribute with the list of columns included in each result, and a `values` attribute with the list of results for the query. |
| 32 | + |
| 33 | +## Creating an ES|QL query |
| 34 | + |
| 35 | +To construct an ES|QL query you start from one of the ES|QL source commands: |
| 36 | + |
| 37 | +### `ESQL.from_` |
| 38 | + |
| 39 | +The `FROM` command selects the indices, data streams or aliases to be queried. |
| 40 | + |
| 41 | +Examples: |
| 42 | + |
| 43 | +```python |
| 44 | +from elasticsearch.esql import ESQL |
| 45 | + |
| 46 | +query1 = ESQL.from_("employees") |
| 47 | +query2 = ESQL.from_("<logs-{now/d}>") |
| 48 | +query3 = ESQL.from_("employees-00001", "other-employees-*") |
| 49 | +query4 = ESQL.from_("cluster_one:employees-00001", "cluster_two:other-employees-*") |
| 50 | +query5 = ESQL.from_("employees").metadata("_id") |
| 51 | +``` |
| 52 | + |
| 53 | +Note how in the last example the optional |
| 54 | + |
| 55 | +### `ESQL.row` |
| 56 | + |
| 57 | +The `ROW` command produces a row with one or more columns, with the values that you specify. |
| 58 | + |
| 59 | +Examples: |
| 60 | + |
| 61 | +```python |
| 62 | +from elasticsearch.esql import ESQL, functions |
| 63 | + |
| 64 | +query1 = ESQL.row(a=1, b="two", c=None) |
| 65 | +query2 = ESQL.row(a=[1, 2]) |
| 66 | +query3 = ESQL.row(a=functions.round(1.23, 0)) |
| 67 | +``` |
| 68 | + |
| 69 | +### `ESQL.show` |
| 70 | + |
| 71 | +The `SHOW` command returns information about the deployment and its capabilities. |
| 72 | + |
| 73 | +Example: |
| 74 | + |
| 75 | +```python |
| 76 | +query = ESQL.show("INFO") |
| 77 | +``` |
| 78 | + |
| 79 | +## Adding processing commands |
| 80 | + |
| 81 | +Once you have a query object, you can start adding processing commands to it. The following |
| 82 | +example shows how to create a query that uses the `WHERE` and `LIMIT` clauses to filter the |
| 83 | +results: |
| 84 | + |
| 85 | +```python |
| 86 | +query = ESQL.from_("employees").where("still_hired == true").limit(10) |
| 87 | +``` |
| 88 | + |
| 89 | +For a complete list of available commands, review the methods of the [`ESQLBase` class](https://elasticsearch-py.readthedocs.io/en/stable/esql.html) in the Elasticsearch Python API documentation. |
| 90 | + |
| 91 | +## Creating Expressions and Conditions |
| 92 | + |
| 93 | +The ES|QL query builder for Python provides two different ways to create expressions and conditions in ES|QL queries. |
| 94 | + |
| 95 | +The most basic option is to provide expressions as strings. Consider the following example, which defines two expressions in the `EVAL` command: |
| 96 | + |
| 97 | +```python |
| 98 | +query = ( |
| 99 | + ESQL.from_("employees") |
| 100 | + .sort("emp_no") |
| 101 | + .keep("first_name", "last_name", "height") |
| 102 | + .eval(height_feet="height * 3.281", height_cm="height * 100") |
| 103 | +) |
| 104 | +``` |
| 105 | + |
| 106 | +A more advanced alternative is to use Python expressions, which are automatically translated to ES|QL. The following example is functionally equivalent to the above: |
| 107 | + |
| 108 | +```python |
| 109 | +from elasticsearch.esql import E |
| 110 | + |
| 111 | +query = ( |
| 112 | + ESQL.from_("employees") |
| 113 | + .sort("emp_no") |
| 114 | + .keep("first_name", "last_name", "height") |
| 115 | + .eval(height_feet=E("height") * 3.281, height_cm=E("height") * 100) |
| 116 | +) |
| 117 | +``` |
| 118 | + |
| 119 | +Here the `E()` helper function is used as a wrapper that accepts a column name and transforms it into an ES|QL expression that can be modified with Python operations. |
| 120 | + |
| 121 | +Here is a second example, which uses a function and a comparison in the `WHERE` command: |
| 122 | + |
| 123 | +```python |
| 124 | +query = ( |
| 125 | + ESQL.from_("employees") |
| 126 | + .keep("first_name", "last_name", "height") |
| 127 | + .where("LENGTH(first_name) < 4") |
| 128 | +) |
| 129 | +``` |
| 130 | + |
| 131 | +Using Python syntax, the condition can be rewritten as follows: |
| 132 | + |
| 133 | +```python |
| 134 | +from elasticsearch.esql import E, functions |
| 135 | + |
| 136 | +query = ( |
| 137 | + ESQL.from_("employees") |
| 138 | + .keep("first_name", "last_name", "height") |
| 139 | + .where(functions.length("first_name") < 4) |
| 140 | +) |
| 141 | +``` |
| 142 | + |
| 143 | +All available ES|QL functions have Python wrappers in the `esql.functions` module. Because the functions already return ES|QL expressions, it is not necessary to wrap them with the `E()` helper function, which is only used when starting an expression from a field name given as a string. |
| 144 | + |
| 145 | +You can find the complete list of available functions in the [API reference documentation](https://elasticsearch-py.readthedocs.io/en/stable/esql.html#module-elasticsearch.esql.functions). |
0 commit comments