@@ -5,7 +5,8 @@ Elasticsearch PHP client
5
5
6
6
[ ![ Build status] ( https://github.com/elastic/elasticsearch-php/workflows/PHP%20test/badge.svg )] ( https://github.com/elastic/elasticsearch-php/actions ) [ ![ Latest Stable Version] ( https://poser.pugx.org/elasticsearch/elasticsearch/v/stable )] ( https://packagist.org/packages/elasticsearch/elasticsearch ) [ ![ Total Downloads] ( https://poser.pugx.org/elasticsearch/elasticsearch/downloads )] ( https://packagist.org/packages/elasticsearch/elasticsearch )
7
7
8
- This is the official PHP client for [ Elasticsearch] ( https://www.elastic.co/elasticsearch/ ) .
8
+ This is the official PHP client for
9
+ [ Elasticsearch] ( https://www.elastic.co/elasticsearch/ ) .
9
10
10
11
## Contents
11
12
@@ -27,18 +28,20 @@ This is the official PHP client for [Elasticsearch](https://www.elastic.co/elast
27
28
28
29
## Getting started 🐣
29
30
30
- Using this client assumes that you have an [ Elasticsearch] ( https://www.elastic.co/elasticsearch/ )
31
- server installed and running.
31
+ Using this client assumes that you have an
32
+ [ Elasticsearch] ( https://www.elastic.co/elasticsearch/ ) server installed and
33
+ running.
32
34
33
- You can install the client in your PHP project using [ composer] ( https://getcomposer.org/ ) :
35
+ You can install the client in your PHP project using
36
+ [ composer] ( https://getcomposer.org/ ) :
34
37
35
38
``` bash
36
39
composer require elasticsearch/elasticsearch
37
40
```
38
41
39
- After the installation you can connect to Elasticsearch using the ` ClientBuilder `
40
- class. For instance, if your Elasticsearch is running on ` localhost:9200 `
41
- you can use the following code:
42
+ After the installation you can connect to Elasticsearch using the
43
+ ` ClientBuilder ` class. For instance, if your Elasticsearch is running on
44
+ ` localhost:9200 ` you can use the following code:
42
45
43
46
``` php
44
47
@@ -55,7 +58,8 @@ echo $response['version']['number']; // 8.0.0
55
58
```
56
59
57
60
The ` $response ` is an object of ` Elastic\Elasticsearch\Response\Elasticsearch `
58
- class that implements ` ElasticsearchInterface ` , PSR-7 [ ResponseInterface] ( https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface )
61
+ class that implements ` ElasticsearchInterface ` , PSR-7
62
+ [ ResponseInterface] ( https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface )
59
63
and [ ArrayAccess] ( https://www.php.net/manual/en/class.arrayaccess.php ) .
60
64
61
65
This means the ` $response ` is a [ PSR-7] ( https://www.php-fig.org/psr/psr-7/ )
@@ -88,21 +92,23 @@ var_dump($response->asBool()); // true if HTTP response code between 200 and 3
88
92
89
93
## Configuration
90
94
91
- Elasticsearch 8.0 offers [ security by default] ( https://www.elastic.co/blog/introducing-simplified-elastic-stack-security ) ,
95
+ Elasticsearch 8.0 offers
96
+ [ security by default] ( https://www.elastic.co/blog/introducing-simplified-elastic-stack-security ) ,
92
97
that means it uses [ TLS] ( https://en.wikipedia.org/wiki/Transport_Layer_Security )
93
98
for protect the communication between client and server.
94
99
95
100
In order to configure ` elasticsearch-php ` for connecting to Elasticsearch 8.0 we
96
101
need to have the certificate authority file (CA).
97
102
98
- You can install Elasticsearch in different ways, for instance using [ Docker] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html )
103
+ You can install Elasticsearch in different ways, for instance using
104
+ [ Docker] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html )
99
105
you need to execute the followind command:
100
106
101
107
``` bash
102
108
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.0.1
103
109
```
104
- Once you have the docker image installed you can execute Elasticsearch,
105
- for instance using a single-node cluster configuration, as follows:
110
+ Once you have the docker image installed, you can execute Elasticsearch, for
111
+ instance using a single-node cluster configuration, as follows:
106
112
107
113
``` bash
108
114
docker network create elastic
@@ -113,8 +119,9 @@ This command creates an `elastic` Docker network and start Elasticsearch
113
119
using the port ` 9200 ` (default).
114
120
115
121
When you run the docker imnage a password is generated for the ` elastic ` user
116
- and it's printed to the terminal (you might need to scroll back a bit in the terminal
117
- to view it). You have to copy it since we will need to connect to Elasticsearch.
122
+ and it's printed to the terminal (you might need to scroll back a bit in the
123
+ terminal to view it). You have to copy it since we will need to connect to
124
+ Elasticsearch.
118
125
119
126
Now that Elasticsearch is running we can get the ` http_ca.crt ` file certificate.
120
127
We need to copy it from the docker instance, using the following command:
@@ -124,8 +131,8 @@ docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .
124
131
```
125
132
126
133
Once we have the ` http_ca.crt ` certificate and the ` password ` , copied during the
127
- start of Elasticsearch, we can use it to connect with ` elasticsearch-php `
128
- as follows:
134
+ start of Elasticsearch, we can use it to connect with ` elasticsearch-php ` as
135
+ follows:
129
136
130
137
``` php
131
138
$client = ClientBuilder::create()
@@ -136,38 +143,41 @@ $client = ClientBuilder::create()
136
143
```
137
144
138
145
For more information about the Docker configuration of Elasticsearch you can
139
- read the official documentation [ here] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html ) .
146
+ read the official documentation
147
+ [ here] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html ) .
140
148
141
149
### Use Elastic Cloud
142
150
143
- You can use [ Elastic Cloud] ( https://www.elastic.co/cloud/ ) as server with ` elasticsearch-php ` .
144
- Elastic Cloud is the PaaS solution offered by [ Elastic] ( https://www.elastic.co ) .
151
+ You can use [ Elastic Cloud] ( https://www.elastic.co/cloud/ ) as server with
152
+ ` elasticsearch-php ` . Elastic Cloud is the PaaS solution offered by
153
+ [ Elastic] ( https://www.elastic.co ) .
145
154
146
155
For connecting to Elastic Cloud you just need the ` Cloud ID ` and the ` API key ` .
147
156
148
- You can get the ` Cloud ID ` from the ` My deployment ` page of your dashboard (see the red
149
- rectangle reported in the screenshot).
157
+ You can get the ` Cloud ID ` from the ` My deployment ` page of your dashboard (see
158
+ the red rectangle reported in the screenshot).
150
159
151
160
![ Cloud ID] ( docs/images/cloud_id.png )
152
161
153
- You can generate an ` API key ` in the ` Management ` page under the section ` Security ` .
162
+ You can generate an ` API key ` in the ` Management ` page under the section
163
+ ` Security ` .
154
164
155
165
![ Security] ( docs/images/create_api_key.png )
156
166
157
- When you click on ` Create API key ` button you can choose a name and set the other
158
- options (eg. restrict privileges, expire after time, etc ).
167
+ When you click on ` Create API key ` button you can choose a name and set the
168
+ other options (for example, restrict privileges, expire after time, and so on ).
159
169
160
170
![ Choose an API name] ( docs/images/api_key_name.png )
161
171
162
172
After this step you will get the ` API key ` in the API keys page.
163
173
164
174
![ API key] ( docs/images/cloud_api_key.png )
165
175
166
- ** IMPORTANT** : you need to copy and store the ` API key ` in a secure place, since you will not
167
- be able to view it again in Elastic Cloud.
176
+ ** IMPORTANT** : you need to copy and store the ` API key ` in a secure place, since
177
+ you will not be able to view it again in Elastic Cloud.
168
178
169
- Once you have collected the ` Cloud ID ` and the ` API key ` you can use ` elasticsearch-php `
170
- to connect to your Elastic Cloud instance, as follows:
179
+ Once you have collected the ` Cloud ID ` and the ` API key ` , you can use
180
+ ` elasticsearch-php ` to connect to your Elastic Cloud instance, as follows:
171
181
172
182
``` php
173
183
$client = ClientBuilder::create()
@@ -178,11 +188,13 @@ $client = ClientBuilder::create()
178
188
179
189
## Usage
180
190
181
- The ` elasticsearch-php ` client offers 400+ endpoints for interacting with Elasticsearch.
182
- A list of all these endpoints is available in the [ official documentation] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html )
191
+ The ` elasticsearch-php ` client offers 400+ endpoints for interacting with
192
+ Elasticsearch. A list of all these endpoints is available in the
193
+ [ official documentation] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html )
183
194
of Elasticsearch APIs.
184
195
185
- Here we reported the basic operation that you can perform with the client: index, search and delete.
196
+ Here we reported the basic operation that you can perform with the client:
197
+ index, search and delete.
186
198
187
199
### Index a document
188
200
@@ -209,19 +221,21 @@ try {
209
221
210
222
print_r($response->asArray()); // response body content as array
211
223
```
212
- Elasticsearch stores the ` {"testField":"abc"} ` JSON document in the ` my_index ` index.
213
- The ` ID ` of the document is created automatically by Elasticsearch and stored in
214
- ` $response['_id'] ` field value. If you want to specify an ` ID ` for the document you need
215
- to store it in ` $params['id'] ` .
216
224
217
- You can manage errors using ` ClientResponseException ` and ` ServerResponseException ` .
218
- The PSR-7 response is available using ` $e->getResponse() ` and the HTTP status code is
219
- available using ` $e->getCode() ` .
225
+ Elasticsearch stores the ` {"testField":"abc"} ` JSON document in the ` my_index `
226
+ index. The ` ID ` of the document is created automatically by Elasticsearch and
227
+ stored in ` $response['_id'] ` field value. If you want to specify an ` ID ` for the
228
+ document you need to store it in ` $params['id'] ` .
229
+
230
+ You can manage errors using ` ClientResponseException ` and
231
+ ` ServerResponseException ` . The PSR-7 response is available using
232
+ ` $e->getResponse() ` and the HTTP status code is available using ` $e->getCode() ` .
220
233
221
234
### Search a document
222
235
223
- Elasticsearch provides many different way to search documents. The simplest search
224
- that you can perform is a [ match query] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html ) ,
236
+ Elasticsearch provides many different way to search documents. The simplest
237
+ search that you can perform is a
238
+ [ match query] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html ) ,
225
239
as follows:
226
240
227
241
``` php
@@ -244,13 +258,14 @@ printf("Took : %d ms\n", $response['took']);
244
258
print_r($response['hits']['hits']); // documents
245
259
```
246
260
247
- Using Elasticsearch you can perform different query search, for more information we suggest to
248
- read the official documention reported [ here] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/search-your-data.html ) .
261
+ Using Elasticsearch you can perform different query search, for more information
262
+ we suggest toread the official documention reported
263
+ [ here] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/search-your-data.html ) .
249
264
250
265
### Delete a document
251
266
252
- You can delete a document specifing the ` index ` name and the ` ID ` of the document,
253
- as follows:
267
+ You can delete a document specifing the ` index ` name and the ` ID ` of the
268
+ document, as follows:
254
269
255
270
``` php
256
271
use Elastic\Elasticsearch\Exception\ClientResponseException;
@@ -270,43 +285,54 @@ if ($response['acknowledge'] === 1) {
270
285
}
271
286
```
272
287
273
- For more information about the Elasticsearch REST API you can read the official documentation
274
- [ here] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html ) .
288
+ For more information about the Elasticsearch REST API you can read the official
289
+ documentation [ here] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html ) .
275
290
276
291
### Versioning
277
292
278
- This client is versioned and released alongside Elasticsearch server.
293
+ This client is versioned and released alongside Elasticsearch server.
279
294
280
- To guarantee compatibility, use the most recent version of this library within the major version of the corresponding Enterprise Search implementation.
295
+ To guarantee compatibility, use the most recent version of this library within
296
+ the major version of the corresponding Enterprise Search implementation.
281
297
282
- For example, for Elasticsearch ` 7.16 ` , use ` 7.16 ` of this library or above, but not ` 8.0 ` .
298
+ For example, for Elasticsearch ` 7.16 ` , use ` 7.16 ` of this library or above, but
299
+ not ` 8.0 ` .
283
300
284
301
## Backward Incompatible Changes :boom :
285
302
286
- The 8.0.0 version of ` elasticsearch-php ` contains a new implementation compared with 7.x.
287
- It supports [ PSR-7] ( https://www.php-fig.org/psr/psr-7/ ) for HTTP messages and [ PSR-18] ( https://www.php-fig.org/psr/psr-18/ )
288
- for HTTP client communications.
303
+ The 8.0.0 version of ` elasticsearch-php ` contains a new implementation compared
304
+ with 7.x. It supports [ PSR-7] ( https://www.php-fig.org/psr/psr-7/ ) for HTTP
305
+ messages and [ PSR-18] ( https://www.php-fig.org/psr/psr-18/ ) for HTTP client
306
+ communications.
289
307
290
- We tried to reduce the BC breaks as much as possible with ` 7.x ` but there are some (big) differences:
308
+ We tried to reduce the BC breaks as much as possible with ` 7.x ` but there are
309
+ some (big) differences:
291
310
292
311
- we changed the namespace, now everything is under ` Elastic\Elasticsearch `
293
- - we used the [ elastic-transport-php] ( https://github.com/elastic/elastic-transport-php ) library for HTTP communications;
294
- - we changed the ` Exception ` model, using the namespace ` Elastic\Elasticsearch\Exception ` . All the exceptions extends the
312
+ - we used the
313
+ [ elastic-transport-php] ( https://github.com/elastic/elastic-transport-php )
314
+ library for HTTP communications;
315
+ - we changed the ` Exception ` model, using the namespace
316
+ ` Elastic\Elasticsearch\Exception ` . All the exceptions extends the
295
317
` ElasticsearchException ` interface, as in 7.x
296
- - we changed the response type of each endpoints using an [ Elasticsearch] ( src/Response/Elasticsearch.php ) response class.
297
- This class wraps a a [ PSR-7] ( https://www.php-fig.org/psr/psr-7/ ) response allowing the access of the body response
298
- as array or object. This means you can access the API response as in 7.x, no BC break here! :angel :
299
- - we changed the ` ConnectionPool ` in ` NodePool ` . The ` connection ` naming was ambigous since the objects are nodes (hosts)
318
+ - we changed the response type of each endpoints using an
319
+ [ Elasticsearch] ( src/Response/Elasticsearch.php ) response class. This class
320
+ wraps a a [ PSR-7] ( https://www.php-fig.org/psr/psr-7/ ) response allowing the
321
+ access of the body response as array or object. This means you can access the
322
+ API response as in 7.x, no BC break here! :angel :
323
+ - we changed the ` ConnectionPool ` in ` NodePool ` . The ` connection ` naming was
324
+ ambigous since the objects are nodes (hosts)
300
325
301
- You can have a look at the [ BREAKING_CHANGES] ( BREAKING_CHANGES.md ) file for more information.
326
+ You can have a look at the [ BREAKING_CHANGES] ( BREAKING_CHANGES.md ) file for more
327
+ information.
302
328
303
329
## Mock the Elasticsearch client
304
330
305
331
If you need to mock the Elasticsearch client you just need to mock a
306
332
[ PSR-18] ( https://www.php-fig.org/psr/psr-18/ ) HTTP Client.
307
333
308
- For instance, you can use the [ php-http/mock-client ] ( https://github.com/php-http/mock-client )
309
- as follows:
334
+ For instance, you can use the
335
+ [ php-http/mock-client ] ( https://github.com/php-http/mock-client ) as follows:
310
336
311
337
``` php
312
338
use Elastic\Elasticsearch\ClientBuilder;
@@ -332,43 +358,54 @@ $result = $client->info(); // Just calling an Elasticsearch endpoint
332
358
333
359
echo $result->asString(); // This is the body!
334
360
```
361
+
335
362
We are using the ` ClientBuilder::setHttpClient() ` to set the mock client.
336
- You can specify the response that you want to have using the ` addResponse($response) `
337
- function. As you can see the ` $response ` is a PSR-7 response object. In this
338
- example we used the ` Nyholm\Psr7\Response ` object from the [ nyholm/psr7] ( https://github.com/Nyholm/psr7 )
339
- project. If you are using [ PHPUnit] ( https://phpunit.de/ ) you can even mock the
340
- ` ResponseInterface ` as follows:
363
+ You can specify the response that you want to have using the
364
+ ` addResponse($response) ` function. As you can see the ` $response ` is a PSR-7
365
+ response object. In this example we used the ` Nyholm\Psr7\Response ` object from
366
+ the [ nyholm/psr7] ( https://github.com/Nyholm/psr7 ) project. If you are using
367
+ [ PHPUnit] ( https://phpunit.de/ ) you can even mock the ` ResponseInterface ` as
368
+ follows:
341
369
342
370
``` php
343
371
$response = $this->createMock('Psr\Http\Message\ResponseInterface');
344
372
```
345
373
346
- ** Notice** : we added a special header in the HTTP response. This is the product check
347
- header, and it is required for guarantee that ` elasticsearch-php ` is communicating
348
- with an Elasticsearch server 8.0+.
374
+ ** Notice** : we added a special header in the HTTP response. This is the product
375
+ check header, and it is required for guarantee that ` elasticsearch-php ` is
376
+ communicating with an Elasticsearch server 8.0+.
349
377
350
- For more information you can read the [ Mock client] ( https://docs.php-http.org/en/latest/clients/mock-client.html )
378
+ For more information you can read the
379
+ [ Mock client] ( https://docs.php-http.org/en/latest/clients/mock-client.html )
351
380
section of PHP-HTTP documentation.
352
381
353
382
## FAQ 🔮
354
383
355
384
### Where do I report issues with the client?
356
385
357
- If something is not working as expected, please open an [ issue] ( https://github.com/elastic/elasticsearh-php/issues/new ) .
386
+ If something is not working as expected, please open an
387
+ [ issue] ( https://github.com/elastic/elasticsearh-php/issues/new ) .
358
388
359
389
### Where else can I go to get help?
360
390
361
- You can checkout the [ Elastic community discuss forums] ( https://discuss.elastic.co/ ) .
391
+ You can checkout the
392
+ [ Elastic community discuss forums] ( https://discuss.elastic.co/ ) .
362
393
363
394
## Contribute 🚀
364
395
365
- We welcome contributors to the project. Before you begin, a couple notes...
366
-
367
- + If you want to contribute to this project you need to subscribe to a [ Contributor Agreement] ( https://www.elastic.co/contributor-agreement ) .
368
- + Before opening a pull request, please create an issue to [ discuss the scope of your proposal] ( https://github.com/elastic/elasticsearch-php/issues ) .
369
- + If you want to send a PR for version ` 8.0 ` please use the ` 8.0 ` branch, for ` 8.1 ` use the ` 8.1 ` branch and so on.
370
- + Never send PR to ` master ` unless you want to contribute to the development version of the client (` master ` represents the next major version).
371
- + Each PR should include a ** unit test** using [ PHPUnit] ( https://phpunit.de/ ) . If you are not familiar with PHPUnit you can have a look at the [ reference] ( https://phpunit.readthedocs.io/en/9.5/ ) .
396
+ We welcome contributors to the project. Before you begin, some useful info...
397
+
398
+ + If you want to contribute to this project you need to subscribe to a
399
+ [ Contributor Agreement] ( https://www.elastic.co/contributor-agreement ) .
400
+ + Before opening a pull request, please create an issue to
401
+ [ discuss the scope of your proposal] ( https://github.com/elastic/elasticsearch-php/issues ) .
402
+ + If you want to send a PR for version ` 8.0 ` please use the ` 8.0 ` branch, for
403
+ ` 8.1 ` use the ` 8.1 ` branch and so on.
404
+ + Never send PR to ` master ` unless you want to contribute to the development
405
+ version of the client (` master ` represents the next major version).
406
+ + Each PR should include a ** unit test** using [ PHPUnit] ( https://phpunit.de/ ) .
407
+ If you are not familiar with PHPUnit you can have a look at the
408
+ [ reference] ( https://phpunit.readthedocs.io/en/9.5/ ) .
372
409
373
410
Thanks in advance for your contribution! :heart :
374
411
0 commit comments