@@ -190,14 +190,50 @@ protected function __create(array $data, $index, $type, $id = null, array $param
190
190
'index ' => $ index ,
191
191
'type ' => $ type ,
192
192
'body ' => $ data ] + ($ id ? ['id ' => $ id ] : []) + $ parameters );
193
- if ($ result ['_shards ' ]['failed ' ] > 0 ) {
193
+ if (array_key_exists ( ' _shards ' , $ result ) && array_key_exists ( ' failed ' , $ result [ ' _shards ' ]) && $ result ['_shards ' ]['failed ' ] > 0 ) {
194
194
throw new Exception ('Failed to create the document. Serialized results: ' + json_encode ($ result ));
195
195
} else {
196
196
$ result = $ this ->__find ($ result ['_index ' ], $ result ['_type ' ], $ result ['_id ' ]);
197
197
}
198
198
return $ result ;
199
199
}
200
200
201
+ /**
202
+ * Index a new document
203
+ *
204
+ * @param array $data the document data
205
+ * @param string $type
206
+ * @param string|number $id
207
+ * @param array $parameters
208
+ * @return array
209
+ */
210
+ protected function _index (array $ data , $ type , $ id = null , array $ parameters = []) {
211
+ return $ this ->__index ($ data , $ this ->index (), $ type , $ id , $ parameters );
212
+ }
213
+
214
+ /**
215
+ * Index a new document
216
+ *
217
+ * @param array $data the document data
218
+ * @param string $index
219
+ * @param string $type
220
+ * @param string|number $id
221
+ * @param array $parameters
222
+ * @return array
223
+ */
224
+ protected function __index (array $ data , $ index , $ type , $ id = null , array $ parameters = []) {
225
+ $ result = $ this ->client ->index ([
226
+ 'index ' => $ index ,
227
+ 'type ' => $ type ,
228
+ 'body ' => $ data ] + ($ id ? ['id ' => $ id ] : []) + $ parameters );
229
+ if (array_key_exists ('_shards ' , $ result ) && array_key_exists ('failed ' , $ result ['_shards ' ]) && $ result ['_shards ' ]['failed ' ] > 0 ) {
230
+ throw new Exception ('Failed to index the document. Serialized results: ' + json_encode ($ result ));
231
+ } else {
232
+ $ result = $ this ->__find ($ result ['_index ' ], $ result ['_type ' ], $ result ['_id ' ]);
233
+ }
234
+ return $ result ;
235
+ }
236
+
201
237
/**
202
238
* Deletes a document
203
239
*
@@ -428,6 +464,7 @@ protected static function _allowPublicAccess() {
428
464
'find ' ,
429
465
'meta ' ,
430
466
'create ' ,
467
+ 'index ' ,
431
468
'delete ' ,
432
469
'builder ' ,
433
470
'metaSettings ' ,
0 commit comments