|
| 1 | +[[esql]] |
| 2 | +=== ES|QL in the PHP client |
| 3 | +++++ |
| 4 | +<titleabbrev>ES|QL</titleabbrev> |
| 5 | +++++ |
| 6 | + |
| 7 | +This page helps you understand and use {ref}/esql.html[ES|QL] in the |
| 8 | +PHP client. |
| 9 | + |
| 10 | +There are two ways to use ES|QL in the PHP client: |
| 11 | + |
| 12 | +* Use the Elasticsearch {es-docs}/esql-apis.html[ES|QL API] directly: This |
| 13 | +is the most flexible approach, but it's also the most complex because you must handle |
| 14 | +results in their raw form. You can choose the precise format of results, |
| 15 | +such as JSON, CSV, or text. |
| 16 | +* Use ES|QL `mapTo($class)` helper. This mapper takes care of parsing the raw |
| 17 | +response and converting into an array of objects. If you don't specify the class |
| 18 | +using the `$class` parameter, the mapper uses https://www.php.net/manual/en/class.stdclass.php[stdClass]. |
| 19 | + |
| 20 | +[discrete] |
| 21 | +[[esql-how-to]] |
| 22 | +==== How to use the ES|QL API |
| 23 | + |
| 24 | +The {es-docs}/esql-query-api.html[ES|QL query API] allows you to specify how |
| 25 | +results should be returned. You can choose a |
| 26 | +{es-docs}/esql-rest.html#esql-rest-format[response format] such as CSV, text, or |
| 27 | +JSON, then fine-tune it with parameters like column separators |
| 28 | +and locale. |
| 29 | + |
| 30 | +The default response from Elasticsearch is a table in JSON, where `columns` |
| 31 | +is an array of descriptions and `values` is an array of rows containing the values. |
| 32 | + |
| 33 | +[[query-script]] |
| 34 | +Here's an example query and PHP script: |
| 35 | + |
| 36 | +```php |
| 37 | +$query = <<<EOD |
| 38 | + FROM books |
| 39 | + | WHERE author == "Stephen King" |
| 40 | + | SORT rating DESC |
| 41 | + | LIMIT 10 |
| 42 | +EOD; |
| 43 | + |
| 44 | +$result = $client->esql()->query([ |
| 45 | + 'body' => ['query' => $query] |
| 46 | +]); |
| 47 | + |
| 48 | +foreach ($result['values'] as $value) { |
| 49 | + $i=0; |
| 50 | + foreach ($result['columns'] as $col) { |
| 51 | + printf("%s : %s\n", $col['name'], $value[$i++]); |
| 52 | + } |
| 53 | + print("---\n"); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Here's the JSON response from Elasticsearch: |
| 58 | + |
| 59 | +```json |
| 60 | +{ |
| 61 | + "columns": [ |
| 62 | + { "name": "author", "type": "text" }, |
| 63 | + { "name": "description", "type": "text" }, |
| 64 | + { "name": "publisher", "type": "keyword" }, |
| 65 | + { "name": "rating", "type": "double" }, |
| 66 | + { "name": "title", "type": "text" }, |
| 67 | + { "name": "year", "type": "integer" } |
| 68 | + ], |
| 69 | + "values": [ |
| 70 | + [ |
| 71 | + "Stephen King", |
| 72 | + "The author ...", |
| 73 | + "Turtleback", |
| 74 | + 5.0, |
| 75 | + "How writers write", |
| 76 | + 2002 |
| 77 | + ], |
| 78 | + [ |
| 79 | + "Stephen King", |
| 80 | + "In Blockade Billy, a retired coach...", |
| 81 | + "Simon and Schuster", |
| 82 | + 5.0, |
| 83 | + "Blockade", |
| 84 | + 2010 |
| 85 | + ], |
| 86 | + [ |
| 87 | + "Stephen King", |
| 88 | + "A chilling collection of twenty horror stories.", |
| 89 | + "Signet Book", |
| 90 | + 4.55859375, |
| 91 | + "Night Shift (Signet)", |
| 92 | + 1979 |
| 93 | + ], |
| 94 | + ... |
| 95 | + ] |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +Using this response, the PHP script (provided <<query-script,above>>) produces the following output: |
| 100 | + |
| 101 | +```php |
| 102 | +author : Stephen King |
| 103 | +description : The author ... |
| 104 | +publisher : Turtleback |
| 105 | +rating : 5.0 |
| 106 | +title : How writers write |
| 107 | +year : 2002 |
| 108 | +--- |
| 109 | +author : Stephen King |
| 110 | +description : In Blockade Billy, a retired coach... |
| 111 | +publisher : Simon and Schuster |
| 112 | +rating : 5.0 |
| 113 | +title : Blockade |
| 114 | +year : 2010 |
| 115 | +--- |
| 116 | +author : Stephen King |
| 117 | +description : A chilling collection of twenty horror stories. |
| 118 | +publisher : Signet Book |
| 119 | +rating : 4.55859375 |
| 120 | +title : Night Shift (Signet) |
| 121 | +year : 1979 |
| 122 | +--- |
| 123 | +``` |
| 124 | + |
| 125 | +The following example gets ES|QL results as CSV and parses them: |
| 126 | + |
| 127 | +```php |
| 128 | +$result = $client->esql()->query([ |
| 129 | + 'format' => 'csv', |
| 130 | + 'body' => ['query' => $query] |
| 131 | +]); |
| 132 | + |
| 133 | +var_dump($result->asArray()); |
| 134 | +``` |
| 135 | + |
| 136 | +The response looks something like this: |
| 137 | + |
| 138 | +```json |
| 139 | +array(12) { |
| 140 | + [0]=> |
| 141 | + array(6) { |
| 142 | + [0]=> |
| 143 | + string(6) "author" |
| 144 | + [1]=> |
| 145 | + string(11) "description" |
| 146 | + [2]=> |
| 147 | + string(9) "publisher" |
| 148 | + [3]=> |
| 149 | + string(6) "rating" |
| 150 | + [4]=> |
| 151 | + string(5) "title" |
| 152 | + [5]=> |
| 153 | + string(4) "year" |
| 154 | + } |
| 155 | + [1]=> |
| 156 | + array(6) { |
| 157 | + [0]=> |
| 158 | + string(12) "Stephen King" |
| 159 | + [1]=> |
| 160 | + string(249) "The author ..." |
| 161 | + [2]=> |
| 162 | + string(18) "Turtleback" |
| 163 | + [3]=> |
| 164 | + string(3) "5.0" |
| 165 | + [4]=> |
| 166 | + string(8) "How writers write" |
| 167 | + [5]=> |
| 168 | + string(4) "2002" |
| 169 | + } |
| 170 | +``` |
| 171 | +In the response, the first row contains the column descriptions and the other rows contain |
| 172 | +the values, using a plain PHP array. |
| 173 | + |
| 174 | + |
| 175 | +[discrete] |
| 176 | +[[esql-custom-mapping]] |
| 177 | +==== Define your own mapping |
| 178 | + |
| 179 | +Although the `esql()->query()` API covers many use cases, your application |
| 180 | +might require a custom mapping. |
| 181 | + |
| 182 | +You can map the ES|QL result into an array of objects, using the `mapTo()` |
| 183 | +function. Here's an example: |
| 184 | + |
| 185 | +```php |
| 186 | +$result = $client->esql()->query([ |
| 187 | + 'body' => ['query' => $query] |
| 188 | +]); |
| 189 | + |
| 190 | +$books = $result->mapTo(); // Array of stdClass |
| 191 | +foreach ($books as $book) { |
| 192 | + printf( |
| 193 | + "%s, %s, %d, Rating: %.2f\n", |
| 194 | + $book->author, |
| 195 | + $book->title, |
| 196 | + $book->year, |
| 197 | + $book->rating |
| 198 | + ); |
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +You can also specify a class name for the mapping. |
| 203 | +All the values will be assigned to the properties of the class. |
| 204 | + |
| 205 | +Here's an example mapper that returns an array of `Book` objects: |
| 206 | + |
| 207 | +```php |
| 208 | +class Book |
| 209 | +{ |
| 210 | + public string $author; |
| 211 | + public string $title; |
| 212 | + public string $description; |
| 213 | + public int $year; |
| 214 | + public float $rating; |
| 215 | +} |
| 216 | + |
| 217 | +$result = $client->esql()->query([ |
| 218 | + 'body' => ['query' => $query] |
| 219 | +]); |
| 220 | +$books = $result->mapTo(Book::class); // Array of Book |
| 221 | +``` |
0 commit comments