Skip to content

Commit 161cb5b

Browse files
committed
Added support for dynamic indices
1 parent 2b3d301 commit 161cb5b

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

src/DSL/Bridge.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ public function processSave($data, $refresh): Results
196196
$id = $data['_id'];
197197
unset($data['_id']);
198198
}
199-
// $data = $this->cleanData($data);
199+
if (isset($data['_index'])) {
200+
unset($data['_index']);
201+
}
200202
$params = [
201203
'index' => $this->index,
202204
'body' => $data,
@@ -620,6 +622,7 @@ private function _sanitizeSearchResponse($response, $params, $queryTag)
620622
foreach ($response['hits']['hits'] as $hit) {
621623
$datum = [];
622624
$datum['_id'] = $hit['_id'];
625+
$datum['_index'] = $hit['_index'];
623626
if (!empty($hit['_source'])) {
624627
foreach ($hit['_source'] as $key => $value) {
625628
$datum[$key] = $value;

src/DSL/QueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private static function _ninQueryString($key, $values): string
174174
private static function _parseParams($key, $value): string
175175
{
176176

177-
if ($key == 'and' || $key == 'or') {
177+
if ($key === 'and' || $key === 'or') {
178178
return self::{'_'.$key.'QueryString'}($value);
179179
}
180180
if (is_array($value)) {

src/Eloquent/Builder.php

Lines changed: 25 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
@@ -358,4 +360,27 @@ public function fields(array $fields)
358360
return $this;
359361
}
360362

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

src/Eloquent/Model.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,48 @@ abstract class Model extends BaseModel
2121

2222
protected $index;
2323

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

2628
protected $keyType = 'string';
2729

2830
protected $parentRelation;
2931

32+
protected static $primitiveCastTypes = [
33+
'array',
34+
'bool',
35+
'boolean',
36+
'collection',
37+
'custom_datetime',
38+
'date',
39+
'datetime',
40+
'decimal',
41+
'double',
42+
'float',
43+
'int',
44+
'integer',
45+
'json',
46+
'object',
47+
'real',
48+
'string',
49+
'timestamp',
50+
];
3051

3152
public function __construct(array $attributes = [])
3253
{
3354
parent::__construct($attributes);
3455
$this->setIndex();
56+
$this->setRecordIndex();
3557
$this->forcePrimaryKey();
3658
}
3759

3860

39-
public function setIndex()
61+
public function setIndex($index = null)
4062
{
63+
if ($index) {
64+
return $this->index = $index;
65+
}
4166
$this->index = $this->index ?? $this->getTable();
4267
unset($this->table);
4368
}
@@ -50,6 +75,19 @@ public function setTable($index)
5075
return $this;
5176
}
5277

78+
public function setRecordIndex($recordIndex = null)
79+
{
80+
if ($recordIndex) {
81+
return $this->recordIndex = $recordIndex;
82+
}
83+
84+
return $this->recordIndex = $this->index;
85+
}
86+
87+
public function getRecordIndex()
88+
{
89+
return $this->recordIndex;
90+
}
5391

5492
public function forcePrimaryKey()
5593
{
@@ -123,6 +161,11 @@ public function freshTimestamp()
123161
* @inheritdoc
124162
*/
125163
public function getTable()
164+
{
165+
return $this->getIndex();
166+
}
167+
168+
public function getIndex()
126169
{
127170
return $this->index ? : parent::getTable();
128171
}
@@ -205,8 +248,7 @@ public function originalIsEquivalent($key, $current)
205248
}
206249

207250
if ($this->hasCast($key, static::$primitiveCastTypes)) {
208-
return $this->castAttribute($key, $attribute) ===
209-
$this->castAttribute($key, $original);
251+
return $this->castAttribute($key, $attribute) === $this->castAttribute($key, $original);
210252
}
211253

212254
return is_numeric($attribute) && is_numeric($original) && strcmp((string)$attribute, (string)$original) === 0;

0 commit comments

Comments
 (0)