Skip to content

Commit 586f138

Browse files
committed
Merge pull request #23 from mhh1422/master
elastic index method
2 parents dc0e6c0 + 6f47c6c commit 586f138

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/Query.php

+38-1
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,50 @@ protected function __create(array $data, $index, $type, $id = null, array $param
190190
'index' => $index,
191191
'type' => $type,
192192
'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) {
194194
throw new Exception('Failed to create the document. Serialized results: ' + json_encode($result));
195195
} else {
196196
$result = $this->__find($result['_index'], $result['_type'], $result['_id']);
197197
}
198198
return $result;
199199
}
200200

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+
201237
/**
202238
* Deletes a document
203239
*
@@ -428,6 +464,7 @@ protected static function _allowPublicAccess() {
428464
'find',
429465
'meta',
430466
'create',
467+
'index',
431468
'delete',
432469
'builder',
433470
'metaSettings',

src/TypeQueryTrait.php

+11
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ protected function _create(array $data, $id = null, array $parameters = []) {
6767
return parent::_create($data, $this->type(), $id, $parameters);
6868
}
6969

70+
/**
71+
* Overrides the parent class _index method to omit the type in parameters.
72+
* @see Query::_index for details.
73+
*
74+
* @param mixed $id
75+
* @return Result|Model[]
76+
*/
77+
protected function _index(array $data, $id = null, array $parameters = []) {
78+
return parent::_index($data, $this->type(), $id, $parameters);
79+
}
80+
7081
/**
7182
* Creates the full model class name
7283
*

0 commit comments

Comments
 (0)