Skip to content

Commit 620040d

Browse files
committed
Updated the README with Connect section + usage
1 parent f4f9fa5 commit 620040d

File tree

5 files changed

+181
-1
lines changed

5 files changed

+181
-1
lines changed

README.md

Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This is the official PHP client for connecting to [Elasticsearch](https://www.el
1010
## Contents
1111

1212
- [Getting started](#getting-started-)
13+
- [Configuration](#configuration)
1314
- [Usage](#usage)
1415
- [Backward Incompatible Changes](#backward-incompatible-changes-)
1516
- [FAQ](#faq-)
@@ -69,10 +70,189 @@ var_dump($response->asObject()); // response body content as object
6970
var_dump($response->asString()); // response body as string (JSON)
7071
```
7172

73+
## Configuration
74+
75+
Elasticsearch 8.0 offers security by default, that means it uses [TLS](https://en.wikipedia.org/wiki/Transport_Layer_Security)
76+
for encrypt the communication between client and server.
77+
78+
In order to configure `elasticsearch-php` for connecting to Elasticsearch 8.0 we
79+
need to have the certificate files (CA).
80+
81+
You can install Elasticsearch in different ways, for instance using [Docker](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html)
82+
you need to execute the followind command:
83+
84+
```bash
85+
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.0.1
86+
```
87+
88+
Once you have the docker image installed you can execute Elasticsearch `8.0.1`,
89+
for instance using a single-node Elasticsearch cluster, as follows:
90+
91+
```bash
92+
docker network create elastic
93+
docker run --name es01 --net elastic -p 9200:9200 -p 9300:9300 -it docker.elastic.co/elasticsearch/elasticsearch:8.0.1
94+
```
95+
96+
This command will create an `elastic` Docker network and start Elasticsearch
97+
using the default port `9200`.
98+
99+
During the start of Elasticsearch a password is generated for the `elastic` user
100+
and output to the terminal (you might need to scroll back a bit in the terminal
101+
to view it). Please copy it since we will need to connect to Elasticsearch.
102+
103+
In order to get the `http_ca.crt` file certificate, you need to copy it from the
104+
Docker instance, using the following command:
105+
106+
```bash
107+
docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .
108+
```
109+
110+
Once you have the `http_ca.crt` certificate and the `password` copied during the
111+
start of Elasticsearch we can use it to connect using `elasticsearch-php`
112+
as follows:
113+
114+
```php
115+
$client = ClientBuilder::create()
116+
->setHosts(['https://localhost:9200'])
117+
->setBasicAuthentication('elastic', 'password-here')
118+
->setCABundle('path/to/http_ca.crt')
119+
->build();
120+
```
121+
122+
For more information about the Docker configuration of Elasticsearch you can
123+
read the official documentation [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html).
124+
125+
### Use Elastic Cloud
126+
127+
You can use [Elastic Cloud](https://www.elastic.co/cloud/) as server to connect using `elasticsearch-php`.
128+
Elastic Cloud is the PaaS solution offered by [Elastic](https://www.elastic.co).
129+
130+
To connect to Elastic Cloud you just need the `Cloud ID` and the `API key`.
131+
132+
You can get the `Cloud ID` from the `My deployment` page of your dashboard (see the red
133+
rectangle reported in the screenshot).
134+
135+
![Cloud ID](docs/images/cloud_id.png)
136+
137+
You can generate an `API key` in the `Management` page under the section `Security`.
138+
139+
![Security](docs/images/create_api_key.png)
140+
141+
When you click on `Create API key` button you can choose a name and set the other
142+
options (eg. restrict privileges, expire after time, etc).
143+
144+
![Choose an API name](docs/images/api_key_name.png)
145+
146+
After this step you will get the `API key`in the API keys page.
147+
148+
![API key](docs/images/cloud_api_key.png)
149+
150+
**IMPORTANT**: you need to copy and store the `API key`in a secure place, since you will not
151+
be able to view it again in Elastic Cloud.
152+
153+
Once you have collected the `Cloud ID` and the `API key` you can use the `ClientBuilder` of
154+
`elasticsearch-php` to connect to your Elastic Cloud instance, as follows:
155+
156+
```php
157+
$client = ClientBuilder::create()
158+
->setElasticCloudId('insert here the Cloud ID')
159+
->setApiKey('insert here the API key')
160+
->build();
161+
```
162+
72163
## Usage
73164

74-
To be completed
165+
The `elasticsearch-php` client offers 400+ endpoints for interacting with Elasticsearch.
166+
A list of all these endpoints is available in the [official documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html)
167+
of Elasticsearch APIs.
75168

169+
Here we reported the basic operation that you can perform with the client: index, search and delete.
170+
171+
### Index a document
172+
173+
You can store (index) a JSON document in Elasticsearch using the following code:
174+
175+
```php
176+
use Elastic\Elasticsearch\Exception\ClientResponseException;
177+
use Elastic\Elasticsearch\Exception\ServerResponseException;
178+
179+
$params = [
180+
'index' => 'my_index',
181+
'body' => [ 'testField' => 'abc']
182+
];
183+
184+
try {
185+
$response = $client->index($params);
186+
} catch (ClientResponseException $e) {
187+
// manage the 4xx error
188+
} catch (ServerResponseException $e) {
189+
// manage the 5xx error
190+
} catch (Exception $e) {
191+
// eg. network error like NoNodeAvailableException
192+
}
193+
194+
print_r($response->asArray()); // response body content as array
195+
```
196+
Elasticsearch stores the `{"testField":"abc"}` JSON document in the `my_index` index.
197+
The `ID` of the document is created automatically by Elasticsearch and stored in
198+
`$response['_id']` field value. If you want to specify an `ID` for the document you need
199+
to store it in `$params['id']`.
200+
201+
You can manage errors using `ClientResponseException` and `ServerResponseException`.
202+
The PSR-7 response is available using `$e->getResponse()` and the HTTP status code is
203+
available using `$e->getCode()`.
204+
205+
### Search a document
206+
207+
Elasticsearch provides many different way to search documents. The simplest search
208+
that you can perform is a [match query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html),
209+
as follows:
210+
211+
```php
212+
$params = [
213+
'index' => 'my_index',
214+
'body' => [
215+
'query' => [
216+
'match' => [
217+
'testField' => 'abc'
218+
]
219+
]
220+
]
221+
];
222+
$response = $client->search($params);
223+
224+
printf("Total docs: %d\n", $response['hits']['total']['value']);
225+
printf("Max score : %.4f\n", $response['hits']['max_score']);
226+
printf("Took : %d ms\n", $response['took']);
227+
228+
print_r($response['hits']['hits']); // documents
229+
```
230+
231+
Using Elasticsearch you can perform different query search, for more information we suggest to
232+
read the official documention reported [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-your-data.html).
233+
234+
### Delete a document
235+
236+
You can delete a document specifing the `index` name and the `ID` of the document,
237+
as follows:
238+
239+
```php
240+
use Elastic\Elasticsearch\Exception\ClientResponseException;
241+
242+
try {
243+
$response = $client->delete([
244+
'index' => 'my_index',
245+
'id' => 'my_id'
246+
]);
247+
} catch (ClientResponseException $e) {
248+
if ($e->getCode() === 404) {
249+
// the document does not exist
250+
}
251+
}
252+
if ($response['acknowledge'] === 1) {
253+
// the document has been delete
254+
}
255+
```
76256

77257
For more information about the Elasticsearch REST API you can read the official documentation
78258
[here](https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html).

docs/images/api_key_name.png

32 KB
Loading

docs/images/cloud_api_key.png

117 KB
Loading

docs/images/cloud_id.png

163 KB
Loading

docs/images/create_api_key.png

78.7 KB
Loading

0 commit comments

Comments
 (0)