Skip to content

Commit 9e24bec

Browse files
documentation
1 parent 5468f6f commit 9e24bec

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

docs/reference/esql.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ There are two ways to use ES|QL in the PHP client:
1414
* Use the Elasticsearch [ES|QL API](https://www.elastic.co/docs/api/doc/elasticsearch/group/endpoint-esql) directly: This is the most flexible approach, but it’s also the most complex because you must handle results in their raw form. You can choose the precise format of results, such as JSON, CSV, or text.
1515
* Use ES|QL `mapTo($class)` helper. This mapper takes care of parsing the raw response and converting into an array of objects. If you don’t specify the class using the `$class` parameter, the mapper uses [stdClass](https://www.php.net/manual/en/class.stdclass.php).
1616

17+
ES|QL queries can be given directly as regular strings or heredoc strings, or for a more convenient option you can use the [ES|QL query builder](#esql-query-builder) helper, which can define ES|QL queries using PHP code.
1718

1819
## How to use the ES|QL API [esql-how-to]
1920

@@ -206,3 +207,71 @@ $result = $client->esql()->query([
206207
$books = $result->mapTo(Book::class); // Array of Book
207208
```
208209

210+
## Using the ES|QL query builder [esql-query-builder]
211+
212+
::::{warning}
213+
This 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.
214+
::::
215+
216+
The ES|QL query builder allows you to construct ES|QL queries using PHP syntax. Consider the following example:
217+
218+
```php
219+
use Elastic\Elasticsearch\Helper\Esql\Query;
220+
221+
$query = Query::from("employees")
222+
->sort("emp_no")
223+
->keep("first_name", "last_name", "height")
224+
->eval(height_feet: "height * 3.281", height_cm: "height * 100")
225+
->limit(3);
226+
```
227+
228+
Casting this query object to a string returns the raw ES|QL query, which you can send to the Elasticsearch ES|QL API:
229+
230+
```php
231+
$result = $client->esql()->query([
232+
'body' => ['query' => (string)$query]
233+
]);
234+
```
235+
236+
### Creating an ES|QL query
237+
238+
To construct an ES|QL query object you typically use `Query::from()`, although there are more [source commands](https://www.elastic.co/docs/reference/query-languages/esql/commands/source-commands) available. Here are some examples:
239+
240+
```php
241+
// FROM employees
242+
$query = Query::from("employees");
243+
244+
// FROM <logs-{now/d}>
245+
$query = Query::from("<logs-{now/d}>");
246+
247+
// FROM employees-00001, other-employees-*
248+
$query = Query::from("employees-00001", "other-employees-*");
249+
250+
// FROM cluster_one:employees-00001, cluster_two:other-employees-*
251+
$query = Query::from("cluster_one:employees-00001", "cluster_two:other-employees-*");
252+
253+
// FROM employees METADATA _id
254+
$query = Query::from("employees")->metadata("_id");
255+
```
256+
257+
Note how in the last example the optional `METADATA` clause of the `FROM` command is added as a chained method.
258+
259+
### Adding processing commands
260+
261+
Once you have a query object, you can add one or more processing commands to it by chaining them. The following example shows how to create a query that uses the `WHERE` and `LIMIT` processing commands to filter the results:
262+
263+
```php
264+
$query = Query::from("employees")
265+
->where("still_hired == true")
266+
->limit(10);
267+
```
268+
269+
The ES|QL documentation includes the complete list of available [processing commands](https://www.elastic.co/docs/reference/query-languages/esql/commands/processing-commands).
270+
271+
### Using Code completion
272+
273+
You can rely on autocompletion as an aid in constructing ES|QL queries with the query builder. Note that this requires an IDE that is configured with a PHP language server.
274+
275+
![Autocompleting Query::](images/autocompleting-query.png)
276+
277+
![Autocompleting from()](images/autocompleting-from.png)
213 KB
Loading
108 KB
Loading

tests/Helper/EsqlTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
namespace Elastic\Elasticsearch\Tests\Helper\Esql;
1616

1717
use Elastic\Elasticsearch\Helper\Esql\Query;
18-
use Elastic\Elasticsearch\Helper\Esql\Expression;
1918
use PHPUnit\Framework\TestCase;
2019

2120
class EsqlTest extends TestCase

0 commit comments

Comments
 (0)