Skip to content

Commit adb0c11

Browse files
committed
Merge branch 'docs-review'
2 parents 53b4cec + 1068434 commit adb0c11

File tree

8 files changed

+185
-219
lines changed

8 files changed

+185
-219
lines changed

README.md

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ The recommended method to install _Elasticsearch-PHP_ is through [Composer](http
8888

8989
$client = ClientBuilder::create()->build();
9090
```
91+
9192
You can find out more on how to install Composer, configure autoloading, and other best-practices for defining dependencies at [getcomposer.org](http://getcomposer.org).
9293

9394
PHP Version Requirement
9495
----
95-
Version 7.0 of this library requires at least PHP version 7.1 to function. In addition, it requires the native JSON
96+
Version 7.0 of this library requires at least PHP version 7.1. In addition, it requires the native JSON
9697
extension to be version 1.3.7 or higher.
9798

9899
| Elasticsearch-PHP Branch | PHP Version |
@@ -110,16 +111,15 @@ Quickstart
110111

111112
### Index a document
112113

113-
In elasticsearch-php, almost everything is configured by associative arrays. The REST endpoint, document and optional parameters - everything is an associative array.
114+
In elasticsearch-php, almost everything is configured by associative arrays. The REST endpoint, document and optional parameters - everything is an associative array.
114115

115-
To index a document, we need to specify four pieces of information: index, type, id and a document body. This is done by
116+
To index a document, we need to specify four pieces of information: index, id and a document body. This is done by
116117
constructing an associative array of key:value pairs. The request body is itself an associative array with key:value pairs
117118
corresponding to the data in your document:
118119

119120
```php
120121
$params = [
121122
'index' => 'my_index',
122-
'type' => 'my_type',
123123
'id' => 'my_id',
124124
'body' => ['testField' => 'abc']
125125
];
@@ -135,12 +135,20 @@ associative array containing a decoded version of the JSON that Elasticsearch re
135135
Array
136136
(
137137
[_index] => my_index
138-
[_type] => my_type
138+
[_type] => _doc
139139
[_id] => my_id
140140
[_version] => 1
141-
[created] => 1
142-
)
141+
[result] => created
142+
[_shards] => Array
143+
(
144+
[total] => 1
145+
[successful] => 1
146+
[failed] => 0
147+
)
143148

149+
[_seq_no] => 0
150+
[_primary_term] => 1
151+
)
144152
```
145153

146154
### Get a document
@@ -150,24 +158,25 @@ Let's get the document that we just indexed. This will simply return the docume
150158
```php
151159
$params = [
152160
'index' => 'my_index',
153-
'type' => 'my_type',
154161
'id' => 'my_id'
155162
];
156163

157164
$response = $client->get($params);
158165
print_r($response);
159166
```
160167

161-
The response contains some metadata (index, type, etc.) as well as a `_source` field...this is the original document
168+
The response contains some metadata (index, version, etc.) as well as a `_source` field, this is the original document
162169
that you sent to Elasticsearch.
163170

164171
```php
165172
Array
166173
(
167174
[_index] => my_index
168-
[_type] => my_type
175+
[_type] => _doc
169176
[_id] => my_id
170177
[_version] => 1
178+
[_seq_no] => 0
179+
[_primary_term] => 1
171180
[found] => 1
172181
[_source] => Array
173182
(
@@ -182,12 +191,20 @@ If you want to retrieve the `_source` field directly, there is the `getSource` m
182191
```php
183192
$params = [
184193
'index' => 'my_index',
185-
'type' => 'my_type',
186194
'id' => 'my_id'
187195
];
188196

189197
$source = $client->getSource($params);
190-
doSomething($source);
198+
print_r($source);
199+
```
200+
201+
The response will be just the `_source` value:
202+
203+
```php
204+
Array
205+
(
206+
[testField] => abc
207+
)
191208
```
192209

193210
### Search for a document
@@ -197,7 +214,6 @@ Searching is a hallmark of Elasticsearch, so let's perform a search. We are goi
197214
```php
198215
$params = [
199216
'index' => 'my_index',
200-
'type' => 'my_type',
201217
'body' => [
202218
'query' => [
203219
'match' => [
@@ -218,34 +234,44 @@ individual search results:
218234
```php
219235
Array
220236
(
221-
[took] => 1
237+
[took] => 33
222238
[timed_out] =>
223239
[_shards] => Array
224240
(
225-
[total] => 5
226-
[successful] => 5
241+
[total] => 1
242+
[successful] => 1
243+
[skipped] => 0
227244
[failed] => 0
228245
)
229246

230247
[hits] => Array
231248
(
232-
[total] => 1
233-
[max_score] => 0.30685282
249+
[total] => Array
250+
(
251+
[value] => 1
252+
[relation] => eq
253+
)
254+
255+
[max_score] => 0.2876821
234256
[hits] => Array
235257
(
236258
[0] => Array
237259
(
238260
[_index] => my_index
239-
[_type] => my_type
261+
[_type] => _doc
240262
[_id] => my_id
241-
[_score] => 0.30685282
263+
[_score] => 0.2876821
242264
[_source] => Array
243265
(
244266
[testField] => abc
245267
)
268+
246269
)
270+
247271
)
272+
248273
)
274+
249275
)
250276
```
251277

@@ -256,7 +282,6 @@ Alright, let's go ahead and delete the document that we added previously:
256282
```php
257283
$params = [
258284
'index' => 'my_index',
259-
'type' => 'my_type',
260285
'id' => 'my_id'
261286
];
262287

@@ -270,11 +295,20 @@ You'll notice this is identical syntax to the `get` syntax. The only difference
270295
```php
271296
Array
272297
(
273-
[found] => 1
274298
[_index] => my_index
275-
[_type] => my_type
299+
[_type] => _doc
276300
[_id] => my_id
277301
[_version] => 2
302+
[result] => deleted
303+
[_shards] => Array
304+
(
305+
[total] => 1
306+
[successful] => 1
307+
[failed] => 0
308+
)
309+
310+
[_seq_no] => 1
311+
[_primary_term] => 1
278312
)
279313
```
280314

docs/crud.asciidoc

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
[[indexing_documents]]
22
== Indexing Documents
33

4+
IMPORTANT: Please note that mapping types will disappear from Elasticsearch, read more https://www.elastic.co/guide/en/elasticsearch/reference/7.x/removal-of-types.html[here]. If you migrated types from Elasticsearch 6 to 7, you can address these with the `type` param.
5+
46
When you add documents to Elasticsearch, you index JSON documents. This maps naturally to PHP associative arrays, since
57
they can easily be encoded in JSON. Therefore, in Elasticsearch-PHP you create and pass associative arrays to the client
6-
for indexing. There are several methods of ingesting data into Elasticsearch, which we will cover here
8+
for indexing. There are several methods of ingesting data into Elasticsearch, which we will cover here.
79

810
=== Single document indexing
911

@@ -16,12 +18,11 @@ When indexing a document, you can either provide an ID or let elasticsearch gene
1618
----
1719
$params = [
1820
'index' => 'my_index',
19-
'type' => 'my_type',
20-
'id' => 'my_id',
21-
'body' => [ 'testField' => 'abc']
21+
'id' => 'my_id',
22+
'body' => [ 'testField' => 'abc']
2223
];
2324
24-
// Document will be indexed to my_index/my_type/my_id
25+
// Document will be indexed to my_index/_doc/my_id
2526
$response = $client->index($params);
2627
----
2728
{zwsp} +
@@ -31,28 +32,25 @@ $response = $client->index($params);
3132
----
3233
$params = [
3334
'index' => 'my_index',
34-
'type' => 'my_type',
35-
'body' => [ 'testField' => 'abc']
35+
'body' => [ 'testField' => 'abc']
3636
];
3737
38-
// Document will be indexed to my_index/my_type/<autogenerated ID>
38+
// Document will be indexed to my_index/_doc/<autogenerated ID>
3939
$response = $client->index($params);
4040
----
4141
{zwsp} +
4242

43-
If you need to set other parameters, such as a `routing` value, you specify those in the array alongside the `index`,
44-
`type`, etc. For example, let's set the routing and timestamp of this new document:
43+
If you need to set other parameters, such as a `routing` value, you specify those in the array alongside the `index`, etc. For example, let's set the routing and timestamp of this new document:
4544

4645
.Additional parameters
4746
[source,php]
4847
----
4948
$params = [
50-
'index' => 'my_index',
51-
'type' => 'my_type',
52-
'id' => 'my_id',
53-
'routing' => 'company_xyz',
49+
'index' => 'my_index',
50+
'id' => 'my_id',
51+
'routing' => 'company_xyz',
5452
'timestamp' => strtotime("-1d"),
55-
'body' => [ 'testField' => 'abc']
53+
'body' => [ 'testField' => 'abc']
5654
];
5755
5856
@@ -75,12 +73,11 @@ for($i = 0; $i < 100; $i++) {
7573
$params['body'][] = [
7674
'index' => [
7775
'_index' => 'my_index',
78-
'_type' => 'my_type',
79-
]
76+
]
8077
];
8178
8279
$params['body'][] = [
83-
'my_field' => 'my_value',
80+
'my_field' => 'my_value',
8481
'second_field' => 'some more values'
8582
];
8683
}
@@ -101,13 +98,12 @@ for ($i = 1; $i <= 1234567; $i++) {
10198
$params['body'][] = [
10299
'index' => [
103100
'_index' => 'my_index',
104-
'_type' => 'my_type',
105-
'_id' => $i
101+
'_id' => $i
106102
]
107103
];
108104
109105
$params['body'][] = [
110-
'my_field' => 'my_value',
106+
'my_field' => 'my_value',
111107
'second_field' => 'some more values'
112108
];
113109
@@ -140,11 +136,10 @@ performed by requesting a document by it's full `index/type/id` path:
140136
----
141137
$params = [
142138
'index' => 'my_index',
143-
'type' => 'my_type',
144-
'id' => 'my_id'
139+
'id' => 'my_id'
145140
];
146141
147-
// Get doc at /my_index/my_type/my_id
142+
// Get doc at /my_index/_doc/my_id
148143
$response = $client->get($params);
149144
----
150145
{zwsp} +
@@ -165,16 +160,15 @@ the `doc` in the `body` parameter. This will merge the fields in `doc` with the
165160
----
166161
$params = [
167162
'index' => 'my_index',
168-
'type' => 'my_type',
169-
'id' => 'my_id',
170-
'body' => [
163+
'id' => 'my_id',
164+
'body' => [
171165
'doc' => [
172166
'new_field' => 'abc'
173167
]
174168
]
175169
];
176170
177-
// Update doc at /my_index/my_type/my_id
171+
// Update doc at /my_index/_doc/my_id
178172
$response = $client->update($params);
179173
----
180174
{zwsp} +
@@ -188,9 +182,8 @@ To perform a scripted update, you need to provide a script and (usually) a set o
188182
----
189183
$params = [
190184
'index' => 'my_index',
191-
'type' => 'my_type',
192-
'id' => 'my_id',
193-
'body' => [
185+
'id' => 'my_id',
186+
'body' => [
194187
'script' => 'ctx._source.counter += count',
195188
'params' => [
196189
'count' => 4
@@ -211,9 +204,8 @@ does not exist (or the field you are trying to update doesn't exist), default va
211204
----
212205
$params = [
213206
'index' => 'my_index',
214-
'type' => 'my_type',
215-
'id' => 'my_id',
216-
'body' => [
207+
'id' => 'my_id',
208+
'body' => [
217209
'script' => [
218210
'source' => 'ctx._source.counter += params.count',
219211
'params' => [
@@ -233,17 +225,16 @@ $response = $client->update($params);
233225
[[deleting_documents]]
234226
== Deleting documents
235227

236-
Finally, you can delete documents by specifying their full `/index/type/id` path:
228+
Finally, you can delete documents by specifying their full `/index/_doc_/id` path:
237229

238230
[source,php]
239231
----
240232
$params = [
241233
'index' => 'my_index',
242-
'type' => 'my_type',
243-
'id' => 'my_id'
234+
'id' => 'my_id'
244235
];
245236
246-
// Delete doc at /my_index/my_type/my_id
237+
// Delete doc at /my_index/_doc_/my_id
247238
$response = $client->delete($params);
248239
----
249240
{zwsp} +

0 commit comments

Comments
 (0)