@@ -10,13 +10,9 @@ This is the official PHP client for
1010
1111## Contents
1212
13- - [ Getting started] ( #getting-started- )
14- - [ Configuration] ( #configuration )
15- - [ Use Elastic Cloud] ( #use-elastic-cloud )
13+ - [ Installation] ( #installation )
14+ - [ Connecting] ( #connecting )
1615- [ Usage] ( #usage )
17- - [ Index a document] ( #index-a-document )
18- - [ Search a document] ( #search-a-document )
19- - [ Delete a document] ( #delete-a-document )
2016- [ Versioning] ( #versioning )
2117- [ Backward Incompatible Changes] ( #backward-incompatible-changes-boom )
2218- [ Mock the Elasticsearch client] ( #mock-the-elasticsearch-client )
@@ -26,165 +22,15 @@ This is the official PHP client for
2622
2723***
2824
29- ## Getting started 🐣
25+ ## Installation
3026
31- Using this client assumes that you have an
32- [ Elasticsearch] ( https://www.elastic.co/elasticsearch/ ) server installed and
33- running.
27+ Refer to the [ Installation section] ( https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/getting-started-php.html#_installation )
28+ of the getting started documentation.
3429
35- You can install the client in your PHP project using
36- [ composer] ( https://getcomposer.org/ ) :
30+ ## Connecting
3731
38- ``` bash
39- composer require elasticsearch/elasticsearch
40- ```
41-
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:
45-
46- ``` php
47-
48- use Elastic\Elasticsearch\ClientBuilder;
49-
50- $client = ClientBuilder::create()
51- ->setHosts(['localhost:9200'])
52- ->build();
53-
54- // Info API
55- $response = $client->info();
56-
57- echo $response['version']['number']; // 8.0.0
58- ```
59-
60- The ` $response ` is an object of ` Elastic\Elasticsearch\Response\Elasticsearch `
61- class that implements ` ElasticsearchInterface ` , PSR-7
62- [ ResponseInterface] ( https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface )
63- and [ ArrayAccess] ( https://www.php.net/manual/en/class.arrayaccess.php ) .
64-
65- This means the ` $response ` is a [ PSR-7] ( https://www.php-fig.org/psr/psr-7/ )
66- object:
67-
68- ``` php
69- echo $response->getStatusCode(); // 200
70- echo (string) $response->getBody(); // Response body in JSON
71- ```
72-
73- and also an "array", meaning you can access the response body as an
74- associative array, as follows:
75-
76-
77- ``` php
78- echo $response['version']['number']; // 8.0.0
79-
80- var_dump($response->asArray()); // response body content as array
81- ```
82-
83- Moreover, you can access the response body as object, string or bool:
84-
85- ``` php
86- echo $response->version->number; // 8.0.0
87-
88- var_dump($response->asObject()); // response body content as object
89- var_dump($response->asString()); // response body as string (JSON)
90- var_dump($response->asBool()); // true if HTTP response code between 200 and 300
91- ```
92-
93- ## Configuration
94-
95- Elasticsearch 8.0 offers
96- [ security by default] ( https://www.elastic.co/blog/introducing-simplified-elastic-stack-security ) ,
97- that means it uses [ TLS] ( https://en.wikipedia.org/wiki/Transport_Layer_Security )
98- for protect the communication between client and server.
99-
100- In order to configure ` elasticsearch-php ` for connecting to Elasticsearch 8.0 we
101- need to have the certificate authority file (CA).
102-
103- You can install Elasticsearch in different ways, for instance using
104- [ Docker] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html )
105- you need to execute the followind command:
106-
107- ``` bash
108- docker pull docker.elastic.co/elasticsearch/elasticsearch:8.0.1
109- ```
110- Once you have the docker image installed, you can execute Elasticsearch, for
111- instance using a single-node cluster configuration, as follows:
112-
113- ``` bash
114- docker network create elastic
115- docker run --name es01 --net elastic -p 9200:9200 -p 9300:9300 -it docker.elastic.co/elasticsearch/elasticsearch:8.0.1
116- ```
117-
118- This command creates an ` elastic ` Docker network and start Elasticsearch
119- using the port ` 9200 ` (default).
120-
121- When you run the docker image a password is generated for the ` elastic ` user
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.
125-
126- Now that Elasticsearch is running we can get the ` http_ca.crt ` file certificate.
127- We need to copy it from the docker instance, using the following command:
128-
129- ``` bash
130- docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .
131- ```
132-
133- Once we have the ` http_ca.crt ` certificate and the ` password ` , copied during the
134- start of Elasticsearch, we can use it to connect with ` elasticsearch-php ` as
135- follows:
136-
137- ``` php
138- $client = ClientBuilder::create()
139- ->setHosts(['https://localhost:9200'])
140- ->setBasicAuthentication('elastic', 'password copied during Elasticsearch start')
141- ->setCABundle('path/to/http_ca.crt')
142- ->build();
143- ```
144-
145- For more information about the Docker configuration of Elasticsearch you can
146- read the official documentation
147- [ here] ( https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html ) .
148-
149- ### Use Elastic Cloud
150-
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 ) .
154-
155- For connecting to Elastic Cloud you just need the ` Cloud ID ` and the ` API key ` .
156-
157- You can get the ` Cloud ID ` from the ` My deployment ` page of your dashboard (see
158- the red rectangle reported in the screenshot).
159-
160- ![ Cloud ID] ( docs/images/cloud_id.png )
161-
162- You can generate an ` API key ` in the ` Management ` page under the section
163- ` Security ` .
164-
165- ![ Security] ( docs/images/create_api_key.png )
166-
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).
169-
170- ![ Choose an API name] ( docs/images/api_key_name.png )
171-
172- After this step you will get the ` API key ` in the API keys page.
173-
174- ![ API key] ( docs/images/cloud_api_key.png )
175-
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.
178-
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:
181-
182- ``` php
183- $client = ClientBuilder::create()
184- ->setElasticCloudId('insert here the Cloud ID')
185- ->setApiKey('insert here the API key')
186- ->build();
187- ```
32+ Refer to the [ Connecting section] ( https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/getting-started-php.html#_connecting )
33+ of the getting started documentation.
18834
18935## Usage
19036
@@ -196,97 +42,13 @@ of Elasticsearch APIs.
19642Here we reported the basic operation that you can perform with the client:
19743index, search and delete.
19844
199- ### Index a document
200-
201- You can store (index) a JSON document in Elasticsearch using the following code:
202-
203- ``` php
204- use Elastic\Elasticsearch\Exception\ClientResponseException;
205- use Elastic\Elasticsearch\Exception\ServerResponseException;
206-
207- $params = [
208- 'index' => 'my_index',
209- 'body' => [ 'testField' => 'abc']
210- ];
211-
212- try {
213- $response = $client->index($params);
214- } catch (ClientResponseException $e) {
215- // manage the 4xx error
216- } catch (ServerResponseException $e) {
217- // manage the 5xx error
218- } catch (Exception $e) {
219- // eg. network error like NoNodeAvailableException
220- }
221-
222- print_r($response->asArray()); // response body content as array
223- ```
224-
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() ` .
233-
234- ### Search a document
235-
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 ) ,
239- as follows:
240-
241- ``` php
242- $params = [
243- 'index' => 'my_index',
244- 'body' => [
245- 'query' => [
246- 'match' => [
247- 'testField' => 'abc'
248- ]
249- ]
250- ]
251- ];
252- $response = $client->search($params);
253-
254- printf("Total docs: %d\n", $response['hits']['total']['value']);
255- printf("Max score : %.4f\n", $response['hits']['max_score']);
256- printf("Took : %d ms\n", $response['took']);
257-
258- print_r($response['hits']['hits']); // documents
259- ```
260-
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 ) .
264-
265- ### Delete a document
266-
267- You can delete a document specifing the ` index ` name and the ` ID ` of the
268- document, as follows:
269-
270- ``` php
271- use Elastic\Elasticsearch\Exception\ClientResponseException;
272-
273- try {
274- $response = $client->delete([
275- 'index' => 'my_index',
276- 'id' => 'my_id'
277- ]);
278- } catch (ClientResponseException $e) {
279- if ($e->getCode() === 404) {
280- // the document does not exist
281- }
282- }
283- if ($response['acknowledge'] === 1) {
284- // the document has been delete
285- }
286- ```
287-
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 ) .
45+ * [ Creating an index] ( https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/getting-started-php.html#_creating_an_index )
46+ * [ Indexing a document] ( https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/getting-started-php.html#_indexing_documents )
47+ * [ Getting documents] ( https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/getting-started-php.html#_getting_documents )
48+ * [ Searching documents] ( https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/getting-started-php.html#_searching_documents )
49+ * [ Updating documents] ( https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/getting-started-php.html#_updating_documents )
50+ * [ Deleting documents] ( https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/getting-started-php.html#_deleting_documents )
51+ * [ Deleting an index] ( https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/getting-started-php.html#_deleting_an_index )
29052
29153### Versioning
29254
0 commit comments