|
| 1 | +--- |
| 2 | +mapped_pages: |
| 3 | + - https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/connecting.html |
| 4 | +--- |
| 5 | + |
| 6 | +# Connecting [connecting] |
| 7 | + |
| 8 | +This page contains the information you need to connect and use the Client with {{es}}. |
| 9 | + |
| 10 | +## Elastic Cloud [auth-ec] |
| 11 | + |
| 12 | +You can connect to [Elastic Cloud](https://www.elastic.co/cloud/) using an ***API key*** and a ***Cloud ID***: |
| 13 | + |
| 14 | +```php |
| 15 | +$client = ClientBuilder::create() |
| 16 | + ->setElasticCloudId('<cloud-id>') |
| 17 | + ->setApiKey('<api-key>') |
| 18 | + ->build(); |
| 19 | +``` |
| 20 | + |
| 21 | +Where <cloud-id> and <api-key> can be retrieved using the Elastic Cloud web UI. |
| 22 | + |
| 23 | +You can get the `Cloud ID` from the `My deployment` page of your dashboard (see the red rectangle reported in the screenshot). |
| 24 | + |
| 25 | +:::{image} ../images/cloud_id.png |
| 26 | +:alt: Elastic Cloud ID |
| 27 | +::: |
| 28 | + |
| 29 | +You can generate an `API key` in the `Management` page under the section `Security`. |
| 30 | + |
| 31 | +:::{image} ../images/create_api_key.png |
| 32 | +:alt: Create API key |
| 33 | +::: |
| 34 | + |
| 35 | +When you click on `Create API key` button you can choose a name and set the other options (eg. restrict privileges, expire after time, etc). |
| 36 | + |
| 37 | +:::{image} ../images/api_key_name.png |
| 38 | +:alt: Choose an API name |
| 39 | +::: |
| 40 | + |
| 41 | +After this step you will get the `API key`in the API keys page. |
| 42 | + |
| 43 | +:::{image} ../images/cloud_api_key.png |
| 44 | +:alt: Cloud API key |
| 45 | +::: |
| 46 | + |
| 47 | +***IMPORTANT***: you need to copy and store the `API key`in a secure place, since you will not be able to view it again in Elastic Cloud. |
| 48 | + |
| 49 | + |
| 50 | +## Security by default (HTTPS) [auth-http] |
| 51 | + |
| 52 | +{{es}} 8.0 offers [security by default](https://www.elastic.co/blog/introducing-simplified-elastic-stack-security), that means it uses [TLS](https://en.wikipedia.org/wiki/Transport_Layer_Security) for protect the communication between client and server. |
| 53 | + |
| 54 | +In order to configure `elasticsearch-php` for connecting to {{es}} 8.0 we need to have the certificate authority file (CA). |
| 55 | + |
| 56 | +You can install {{es}} in different ways, for instance using [Docker](docs-content://deploy-manage/deploy/self-managed/install-elasticsearch-with-docker.md) you need to execute the followind command: |
| 57 | + |
| 58 | +```shell |
| 59 | +docker pull docker.elastic.co/elasticsearch/elasticsearch:8.0.1 |
| 60 | +``` |
| 61 | + |
| 62 | +Once you have the docker image installed you can execute {{es}}, for instance using a single-node cluster configuration, as follows: |
| 63 | + |
| 64 | +```shell |
| 65 | +docker network create elastic |
| 66 | +docker run --name es01 --net elastic -p 9200:9200 -p 9300:9300 -it docker.elastic.co/elasticsearch/elasticsearch:8.0.1 |
| 67 | +``` |
| 68 | + |
| 69 | +This command creates an `elastic` Docker network and start {{es}} using the port `9200` (default). |
| 70 | + |
| 71 | +When you run the docker image a password is generated for the `elastic` user and it’s printed to the terminal (you might need to scroll back a bit in the terminal to view it). You have to copy it since we will need to connect to {{es}}. |
| 72 | + |
| 73 | +Now that {{es}} is running we can get the `http_ca.crt` file certificate. We need to copy it from the docker instance, using the following command: |
| 74 | + |
| 75 | +```shell |
| 76 | +docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt . |
| 77 | +``` |
| 78 | + |
| 79 | +Once we have the `http_ca.crt` certificate and the `password`, copied during the start of {{es}} , we can use it to connect with `elasticsearch-php` as follows: |
| 80 | + |
| 81 | +```php |
| 82 | +$client = ClientBuilder::create() |
| 83 | + ->setHosts(['https://localhost:9200']) |
| 84 | + ->setBasicAuthentication('elastic', 'password copied during Elasticsearch start') |
| 85 | + ->setCABundle('path/to/http_ca.crt') |
| 86 | + ->build(); |
| 87 | +``` |
| 88 | + |
| 89 | +For more information about the Docker configuration of Elasticsearch you can read the official documentation [here](docs-content://deploy-manage/deploy/self-managed/install-elasticsearch-with-docker.md). |
| 90 | + |
| 91 | + |
| 92 | +## Usage [client-usage] |
| 93 | + |
| 94 | +This section is a crash-course overview of the client and its syntax. If you are familiar with {{es}}, you’ll notice that the methods are named just like REST endpoints. |
| 95 | + |
| 96 | +You may also notice that the client is configured in a manner that facilitates easy discovery via your IDE. All core actions are available under the `$client` object (indexing, searching, getting, etc). Index and cluster management are located under the `$client->indices()` and `$client->cluster()` objects, respectively. |
| 97 | + |
| 98 | + |
| 99 | +### Info API [_info_api] |
| 100 | + |
| 101 | +You can get information about the {{es}} instance using the `info()` API, as follows: |
| 102 | + |
| 103 | +```php |
| 104 | +$response = $client->info(); |
| 105 | +``` |
| 106 | + |
| 107 | +The response that you get back contains the information about {{es}}. The `$response` is an object of `Elastic\Elasticsearch\Response\Elasticsearch` class that implements `ElasticsearchInterface`, PSR-7 [ResponseInterface](https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface) and [ArrayAccess](https://www.php.net/manual/en/class.arrayaccess.php). |
| 108 | + |
| 109 | +This means the `$response` is a [PSR-7](https://www.php-fig.org/psr/psr-7/) object: |
| 110 | + |
| 111 | +```php |
| 112 | +echo $response->getStatusCode(); // 200 |
| 113 | +echo (string) $response->getBody(); // Response body in JSON |
| 114 | +``` |
| 115 | + |
| 116 | +and also an "array", meaning you can access the response body as an associative array, as follows: |
| 117 | + |
| 118 | +```php |
| 119 | +echo $response['version']['number']; // 8.0.0 |
| 120 | + |
| 121 | +var_dump($response->asArray()); // response body content as array |
| 122 | +``` |
| 123 | + |
| 124 | +Moreover, you can also access the response body as object, string or bool: |
| 125 | + |
| 126 | +```php |
| 127 | +echo $response->version->number; // 8.0.0 |
| 128 | + |
| 129 | +var_dump($response->asObject()); // response body content as object |
| 130 | +var_dump($response->asString()); // response body as string (JSON) |
| 131 | +var_dump($response->asBool()); // true if HTTP response code between 200 and 300 |
| 132 | +``` |
| 133 | + |
| 134 | + |
| 135 | +### Indexing a document [_indexing_a_document] |
| 136 | + |
| 137 | +To index a document, we need to specify three pieces of information: index, id and a document body. This is done by constructing an associative array of key:value pairs. The request body is itself an associative array with key:value pairs corresponding to the data in your document: |
| 138 | + |
| 139 | +```php |
| 140 | +$params = [ |
| 141 | + 'index' => 'my_index', |
| 142 | + 'id' => 'my_id', |
| 143 | + 'body' => ['testField' => 'abc'] |
| 144 | +]; |
| 145 | + |
| 146 | +$response = $client->index($params); |
| 147 | +print_r($response->asArray()); |
| 148 | +``` |
| 149 | + |
| 150 | +The response that you get back indicates that the document was created in the index that you specified. The response can be rendered as associatve array using the `asArray()` function. The array response contains a decoded version of the JSON that Elasticsearch returns: |
| 151 | + |
| 152 | +```php |
| 153 | +Array |
| 154 | +( |
| 155 | + [_index] => my_index |
| 156 | + [_type] => _doc |
| 157 | + [_id] => my_id |
| 158 | + [_version] => 1 |
| 159 | + [created] => 1 |
| 160 | +) |
| 161 | +``` |
| 162 | + |
| 163 | + |
| 164 | +### Set the body as JSON string [_set_the_body_as_json_string] |
| 165 | + |
| 166 | +If you want you can specify the `body`parameter as JSON string. This can be useful for testing (eg. copy & paste from online code examples) or if you have already some JSON documents to be stored in Elasticsearch. |
| 167 | + |
| 168 | +For instance, the previous index example can be re-written as follows: |
| 169 | + |
| 170 | +```php |
| 171 | +$params = [ |
| 172 | + 'index' => 'my_index', |
| 173 | + 'id' => 'my_id', |
| 174 | + 'body' => '{"testField" : "abc"}' |
| 175 | +]; |
| 176 | + |
| 177 | +$response = $client->index($params); |
| 178 | +print_r($response->asArray()); |
| 179 | +``` |
| 180 | + |
| 181 | + |
| 182 | +### Getting a document [_getting_a_document] |
| 183 | + |
| 184 | +Let’s get the document that we just indexed. This returns the document: |
| 185 | + |
| 186 | +```php |
| 187 | +$params = [ |
| 188 | + 'index' => 'my_index', |
| 189 | + 'id' => 'my_id' |
| 190 | +]; |
| 191 | + |
| 192 | +$response = $client->get($params); |
| 193 | +print_r($response->asArray()); |
| 194 | +``` |
| 195 | + |
| 196 | +The response contains metadata such as index, version, and so on as well as a `_source` field, which is the original document you sent to {{es}}. |
| 197 | + |
| 198 | +```php |
| 199 | +Array |
| 200 | +( |
| 201 | + [_index] => my_index |
| 202 | + [_type] => _doc |
| 203 | + [_id] => my_id |
| 204 | + [_version] => 1 |
| 205 | + [found] => 1 |
| 206 | + [_source] => Array |
| 207 | + ( |
| 208 | + [testField] => abc |
| 209 | + ) |
| 210 | + |
| 211 | +) |
| 212 | +``` |
| 213 | + |
| 214 | + |
| 215 | +### Searching for a document [_searching_for_a_document] |
| 216 | + |
| 217 | +Searching is a hallmark of {{es}}, so let’s perform a search. We are going to use the `match` query as a demonstration: |
| 218 | + |
| 219 | +```php |
| 220 | +$params = [ |
| 221 | + 'index' => 'my_index', |
| 222 | + 'body' => [ |
| 223 | + 'query' => [ |
| 224 | + 'match' => [ |
| 225 | + 'testField' => 'abc' |
| 226 | + ] |
| 227 | + ] |
| 228 | + ] |
| 229 | +]; |
| 230 | + |
| 231 | +$response = $client->search($params); |
| 232 | +print_r($response->asArray()); |
| 233 | +``` |
| 234 | + |
| 235 | +The response here is different from the previous ones. You can see metadata (`took`, `timed_out`, etc.) and an array named `hits`. This represents your search results. Inside of `hits` is another array named `hits`, which contains individual search results: |
| 236 | + |
| 237 | +```php |
| 238 | +Array |
| 239 | +( |
| 240 | + [took] => 1 |
| 241 | + [timed_out] => |
| 242 | + [_shards] => Array |
| 243 | + ( |
| 244 | + [total] => 5 |
| 245 | + [successful] => 5 |
| 246 | + [failed] => 0 |
| 247 | + ) |
| 248 | + |
| 249 | + [hits] => Array |
| 250 | + ( |
| 251 | + [total] => 1 |
| 252 | + [max_score] => 0.30685282 |
| 253 | + [hits] => Array |
| 254 | + ( |
| 255 | + [0] => Array |
| 256 | + ( |
| 257 | + [_index] => my_index |
| 258 | + [_type] => _doc |
| 259 | + [_id] => my_id |
| 260 | + [_score] => 0.30685282 |
| 261 | + [_source] => Array |
| 262 | + ( |
| 263 | + [testField] => abc |
| 264 | + ) |
| 265 | + ) |
| 266 | + ) |
| 267 | + ) |
| 268 | +) |
| 269 | +``` |
| 270 | + |
| 271 | + |
| 272 | +### Deleting a document [_deleting_a_document] |
| 273 | + |
| 274 | +Alright, let’s go ahead and delete the document that we added previously: |
| 275 | + |
| 276 | +```php |
| 277 | +$params = [ |
| 278 | + 'index' => 'my_index', |
| 279 | + 'id' => 'my_id' |
| 280 | +]; |
| 281 | + |
| 282 | +$response = $client->delete($params); |
| 283 | +print_r($response->asArray()); |
| 284 | +``` |
| 285 | + |
| 286 | +This syntax is identical to the `get` syntax. The only difference is the operation: `delete` instead of `get`. The response confirms the document is deleted: |
| 287 | + |
| 288 | +```php |
| 289 | +Array |
| 290 | +( |
| 291 | + [found] => 1 |
| 292 | + [_index] => my_index |
| 293 | + [_type] => _doc |
| 294 | + [_id] => my_id |
| 295 | + [_version] => 2 |
| 296 | +) |
| 297 | +``` |
| 298 | + |
| 299 | + |
| 300 | +### Deleting an index [_deleting_an_index_2] |
| 301 | + |
| 302 | +Due to the dynamic nature of {{es}}, the first document you added automatically built an index with some default settings. Delete that index and specify your own settings later: |
| 303 | + |
| 304 | +```php |
| 305 | +$deleteParams = [ |
| 306 | + 'index' => 'my_index' |
| 307 | +]; |
| 308 | +$response = $client->indices()->delete($deleteParams); |
| 309 | +print_r($response->asArray()); |
| 310 | +``` |
| 311 | + |
| 312 | +The response: |
| 313 | + |
| 314 | +```php |
| 315 | +Array |
| 316 | +( |
| 317 | + [acknowledged] => 1 |
| 318 | +) |
| 319 | +``` |
| 320 | + |
| 321 | + |
| 322 | +### Creating an index [_creating_an_index_2] |
| 323 | + |
| 324 | +Now that you are starting fresh (no data or index), add a new index with custom settings: |
| 325 | + |
| 326 | +```php |
| 327 | +$params = [ |
| 328 | + 'index' => 'my_index', |
| 329 | + 'body' => [ |
| 330 | + 'settings' => [ |
| 331 | + 'number_of_shards' => 2, |
| 332 | + 'number_of_replicas' => 0 |
| 333 | + ] |
| 334 | + ] |
| 335 | +]; |
| 336 | + |
| 337 | +$response = $client->indices()->create($params); |
| 338 | +print_r($response->asArray()); |
| 339 | +``` |
| 340 | + |
| 341 | +{{es}} now creates that index with your chosen settings and return an acknowledgement: |
| 342 | + |
| 343 | +```php |
| 344 | +Array |
| 345 | +( |
| 346 | + [acknowledged] => 1 |
| 347 | +) |
| 348 | +``` |
| 349 | + |
0 commit comments