Skip to content

Commit b265484

Browse files
committed
Added support for dynamic indices
1 parent f0f9f48 commit b265484

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

src/DSL/Bridge.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@ public function processGet($ids, $columns): Results
113113

114114
public function processFind($wheres, $options, $columns): Results
115115
{
116-
// if (count($wheres) == 1) {
117-
// if (!empty($wheres['_id'])) {
118-
// return $this->processGet($wheres['_id'], $columns);
119-
// }
120-
// }
121116
$params = $this->buildParams($this->index, $wheres, $options, $columns);
122117

123118
return $this->_returnSearch($params, __FUNCTION__);
@@ -196,7 +191,9 @@ public function processSave($data, $refresh): Results
196191
$id = $data['_id'];
197192
unset($data['_id']);
198193
}
199-
// $data = $this->cleanData($data);
194+
if (isset($data['_index'])) {
195+
unset($data['_index']);
196+
}
200197
$params = [
201198
'index' => $this->index,
202199
'body' => $data,
@@ -619,6 +616,7 @@ private function _sanitizeSearchResponse($response, $params, $queryTag)
619616
if (!empty($response['hits']['hits'])) {
620617
foreach ($response['hits']['hits'] as $hit) {
621618
$datum = [];
619+
$datum['_index'] = $hit['_index'];
622620
$datum['_id'] = $hit['_id'];
623621
if (!empty($hit['_source'])) {
624622
foreach ($hit['_source'] as $key => $value) {

src/DSL/QueryBuilder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ private static function _ninQueryString($key, $values): string
173173

174174
private static function _parseParams($key, $value): string
175175
{
176-
177-
if ($key == 'and' || $key == 'or') {
176+
if ($key === 'and' || $key === 'or') {
178177
return self::{'_'.$key.'QueryString'}($value);
179178
}
180179
if (is_array($value)) {

src/Eloquent/Builder.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace PDPhilip\Elasticsearch\Eloquent;
44

55
use Illuminate\Database\Eloquent\Builder as BaseEloquentBuilder;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Facades\DB;
68
use PDPhilip\Elasticsearch\Helpers\QueriesRelationships;
79

810
class Builder extends BaseEloquentBuilder
@@ -354,4 +356,29 @@ public function fields(array $fields)
354356
return $this;
355357
}
356358

359+
public function hydrate(array $items)
360+
{
361+
$instance = $this->newModelInstance();
362+
363+
return $instance->newCollection(array_map(function ($item) use ($instance) {
364+
$recordIndex = null;
365+
if (is_array($item)) {
366+
$recordIndex = !empty($item['_index']) ? $item['_index'] : null;
367+
if ($recordIndex) {
368+
unset($item['_index']);
369+
}
370+
}
371+
372+
$model = $instance->newFromBuilder($item);
373+
if ($recordIndex) {
374+
$model->setRecordIndex($recordIndex);
375+
$model->setIndex($recordIndex);
376+
377+
}
378+
379+
return $model;
380+
381+
}, $items));
382+
}
383+
357384
}

src/Eloquent/Model.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ abstract class Model extends BaseModel
2121

2222
protected $index;
2323

24+
protected $recordIndex;
25+
2426
protected $primaryKey = '_id';
2527

2628
protected $keyType = 'string';
@@ -32,12 +34,16 @@ public function __construct(array $attributes = [])
3234
{
3335
parent::__construct($attributes);
3436
$this->setIndex();
37+
$this->setRecordIndex();
3538
$this->forcePrimaryKey();
3639
}
3740

3841

39-
public function setIndex()
42+
public function setIndex($index = null)
4043
{
44+
if ($index) {
45+
return $this->index = $index;
46+
}
4147
$this->index = $this->index ?? $this->getTable();
4248
unset($this->table);
4349
}
@@ -50,6 +56,20 @@ public function setTable($index)
5056
return $this;
5157
}
5258

59+
public function setRecordIndex($recordIndex = null)
60+
{
61+
if ($recordIndex) {
62+
return $this->recordIndex = $recordIndex;
63+
}
64+
65+
return $this->recordIndex = $this->index;
66+
}
67+
68+
public function getRecordIndex()
69+
{
70+
return $this->recordIndex;
71+
}
72+
5373

5474
public function forcePrimaryKey()
5575
{
@@ -118,6 +138,11 @@ public function freshTimestamp()
118138
* @inheritdoc
119139
*/
120140
public function getTable()
141+
{
142+
return $this->getIndex();
143+
}
144+
145+
public function getIndex()
121146
{
122147
return $this->index ? : parent::getTable();
123148
}

0 commit comments

Comments
 (0)