You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/esql.md
+69Lines changed: 69 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ There are two ways to use ES|QL in the PHP client:
14
14
* 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.
15
15
* 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).
16
16
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.
$books = $result->mapTo(Book::class); // Array of Book
207
208
```
208
209
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:
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:
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.
0 commit comments