@@ -5,7 +5,8 @@ Elasticsearch PHP client
55
66[ ![ 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 )
77
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/ ) .
910
1011## Contents
1112
@@ -27,18 +28,20 @@ This is the official PHP client for [Elasticsearch](https://www.elastic.co/elast
2728
2829## Getting started 🐣
2930
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.
3234
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/ ) :
3437
3538``` bash
3639composer require elasticsearch/elasticsearch
3740```
3841
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:
4245
4346``` php
4447
@@ -55,7 +58,8 @@ echo $response['version']['number']; // 8.0.0
5558```
5659
5760The ` $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 )
5963and [ ArrayAccess] ( https://www.php.net/manual/en/class.arrayaccess.php ) .
6064
6165This 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
8892
8993## Configuration
9094
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 ) ,
9297that means it uses [ TLS] ( https://en.wikipedia.org/wiki/Transport_Layer_Security )
9398for protect the communication between client and server.
9499
95100In order to configure ` elasticsearch-php ` for connecting to Elasticsearch 8.0 we
96101need to have the certificate authority file (CA).
97102
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 )
99105you need to execute the followind command:
100106
101107``` bash
102108docker pull docker.elastic.co/elasticsearch/elasticsearch:8.0.1
103109```
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:
106112
107113``` bash
108114docker network create elastic
@@ -113,8 +119,9 @@ This command creates an `elastic` Docker network and start Elasticsearch
113119using the port ` 9200 ` (default).
114120
115121When 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.
118125
119126Now that Elasticsearch is running we can get the ` http_ca.crt ` file certificate.
120127We 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 .
124131```
125132
126133Once 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:
129136
130137``` php
131138$client = ClientBuilder::create()
@@ -136,38 +143,41 @@ $client = ClientBuilder::create()
136143```
137144
138145For 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 ) .
140148
141149### Use Elastic Cloud
142150
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 ) .
145154
146155For connecting to Elastic Cloud you just need the ` Cloud ID ` and the ` API key ` .
147156
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).
150159
151160![ Cloud ID] ( docs/images/cloud_id.png )
152161
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 ` .
154164
155165![ Security] ( docs/images/create_api_key.png )
156166
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 ).
159169
160170![ Choose an API name] ( docs/images/api_key_name.png )
161171
162172After this step you will get the ` API key ` in the API keys page.
163173
164174![ API key] ( docs/images/cloud_api_key.png )
165175
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.
168178
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:
171181
172182``` php
173183$client = ClientBuilder::create()
@@ -178,11 +188,13 @@ $client = ClientBuilder::create()
178188
179189## Usage
180190
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 )
183194of Elasticsearch APIs.
184195
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.
186198
187199### Index a document
188200
@@ -209,19 +221,21 @@ try {
209221
210222print_r($response->asArray()); // response body content as array
211223```
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'] ` .
216224
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() ` .
220233
221234### Search a document
222235
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 ) ,
225239as follows:
226240
227241``` php
@@ -244,13 +258,14 @@ printf("Took : %d ms\n", $response['took']);
244258print_r($response['hits']['hits']); // documents
245259```
246260
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 ) .
249264
250265### Delete a document
251266
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:
254269
255270``` php
256271use Elastic\Elasticsearch\Exception\ClientResponseException;
@@ -270,43 +285,54 @@ if ($response['acknowledge'] === 1) {
270285}
271286```
272287
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 ) .
275290
276291### Versioning
277292
278- This client is versioned and released alongside Elasticsearch server.
293+ This client is versioned and released alongside Elasticsearch server.
279294
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.
281297
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 ` .
283300
284301## Backward Incompatible Changes :boom :
285302
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.
289307
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:
291310
292311- 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
295317 ` 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)
300325
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.
302328
303329## Mock the Elasticsearch client
304330
305331If you need to mock the Elasticsearch client you just need to mock a
306332[ PSR-18] ( https://www.php-fig.org/psr/psr-18/ ) HTTP Client.
307333
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:
310336
311337``` php
312338use Elastic\Elasticsearch\ClientBuilder;
@@ -332,43 +358,54 @@ $result = $client->info(); // Just calling an Elasticsearch endpoint
332358
333359echo $result->asString(); // This is the body!
334360```
361+
335362We 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:
341369
342370``` php
343371$response = $this->createMock('Psr\Http\Message\ResponseInterface');
344372```
345373
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+.
349377
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 )
351380section of PHP-HTTP documentation.
352381
353382## FAQ 🔮
354383
355384### Where do I report issues with the client?
356385
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 ) .
358388
359389### Where else can I go to get help?
360390
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/ ) .
362393
363394## Contribute 🚀
364395
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/ ) .
372409
373410Thanks in advance for your contribution! :heart :
374411
0 commit comments