You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -69,10 +70,189 @@ var_dump($response->asObject()); // response body content as object
69
70
var_dump($response->asString()); // response body as string (JSON)
70
71
```
71
72
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)
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
+

136
+
137
+
You can generate an `API key` in the `Management` page under the section `Security`.
138
+
139
+

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
+

145
+
146
+
After this step you will get the `API key`in the API keys page.
147
+
148
+

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
+
72
163
## Usage
73
164
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.
75
168
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),
0 commit comments