Skip to content

Commit 05955c1

Browse files
committed
Added support for dynamic indices
1 parent 179ae76 commit 05955c1

File tree

5 files changed

+108
-5
lines changed

5 files changed

+108
-5
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,53 @@ Queues
12131213
_[Coming]_
12141214

12151215

1216+
Dynamic Indies
1217+
========
1218+
In some cases you will need to split a model into different indices. There are limits to this to keep within reasonable Laravel ORM bounds, but if you keep the index prefix consistent then the plugin can manage the rest.
1219+
1220+
For example, let's imagine we're tracking page hits, the `PageHit.php` model could be
1221+
1222+
```php
1223+
<?php
1224+
1225+
namespace App\Models;
1226+
1227+
use PDPhilip\Elasticsearch\Eloquent\Model as Eloquent;
1228+
1229+
class PageHit extends Eloquent
1230+
{
1231+
protected $connection = 'elasticsearch';
1232+
protected $index = 'page_hits_*'; //Dynamic index
1233+
1234+
}
1235+
```
1236+
1237+
If you set a dynamic index you can read/search across all the indices that match the prefix `page_hits_`
1238+
1239+
```php
1240+
$pageHits = PageHit::where('page_id',1)->get();
1241+
```
1242+
1243+
You will need to set the record's actual index when creating a new record, with `setIndex('value')`
1244+
1245+
Create example:
1246+
```php
1247+
$pageHit = new PageHit
1248+
$pageHit->page_id = 4;
1249+
$pageHit->ip = $someIp;
1250+
$pageHit->setIndex('page_hits_'.date('Y-m-d'));
1251+
$pageHit->save();
1252+
1253+
```
1254+
1255+
Each eloquent model will have the current record's index embedded into it, to retrieve it simply call `getRecordIndex()`
1256+
```php
1257+
$pageHit->getRecordIndex(); //returns page_hits_2021-01-01
1258+
```
1259+
1260+
1261+
```
1262+
12161263
RAW DSL
12171264
========
12181265

src/DSL/Bridge.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ public function processSave($data, $refresh): Results
194194
$id = $data['_id'];
195195
unset($data['_id']);
196196
}
197+
if (isset($data['_index'])) {
198+
unset($data['_index']);
199+
}
197200
// $data = $this->cleanData($data);
198201
$params = [
199202
'index' => $this->index,
@@ -209,7 +212,6 @@ public function processSave($data, $refresh): Results
209212

210213
try {
211214
$response = $this->client->index($params);
212-
213215
$savedData = ['_id' => $response['_id']] + $data;
214216

215217
return $this->_return($savedData, $response, $params, $this->_queryTag(__FUNCTION__));
@@ -613,10 +615,12 @@ private function _sanitizeSearchResponse($response, $params, $queryTag)
613615
$meta['timed_out'] = $response['timed_out'];
614616
$meta['total'] = $response['hits']['total']['value'] ?? 0;
615617
$meta['max_score'] = $response['hits']['max_score'] ?? 0;
618+
616619
$data = [];
617620
if (!empty($response['hits']['hits'])) {
618621
foreach ($response['hits']['hits'] as $hit) {
619622
$datum = [];
623+
$datum['_index'] = $hit['_index'];
620624
$datum['_id'] = $hit['_id'];
621625
if (!empty($hit['_source'])) {
622626
foreach ($hit['_source'] as $key => $value) {
@@ -632,7 +636,6 @@ private function _sanitizeSearchResponse($response, $params, $queryTag)
632636

633637
private function _return($data, $meta, $params, $queryTag): Results
634638
{
635-
636639
unset($meta['_source']);
637640

638641
$results = new Results($data, $meta, $params, $queryTag);

src/DSL/QueryBuilder.php

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

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

src/Eloquent/Builder.php

Lines changed: 29 additions & 1 deletion
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
@@ -77,7 +79,6 @@ public function getModels($columns = ['*'])
7779

7880
$data = $this->query->get($columns);
7981
$results = $this->model->hydrate($data->all())->all();
80-
8182
return ['results' => $results];
8283

8384
}
@@ -343,4 +344,31 @@ public function fields(array $fields)
343344
return $this;
344345
}
345346

347+
348+
public function hydrate(array $items)
349+
{
350+
$instance = $this->newModelInstance();
351+
return $instance->newCollection(array_map(function ($item) use ($items, $instance) {
352+
$recordIndex = null;
353+
if (is_array($item)){
354+
$recordIndex = !empty($item['_index']) ? $item['_index'] : null;
355+
if ($recordIndex){
356+
unset($item['_index']);
357+
}
358+
}
359+
360+
$model = $instance->newFromBuilder($item);
361+
if ($recordIndex){
362+
$model->setRecordIndex($recordIndex);
363+
$model->setIndex($recordIndex);
364+
365+
}
366+
if (count($items) > 1) {
367+
$model->preventsLazyLoading = Model::preventsLazyLoading();
368+
}
369+
370+
return $model;
371+
}, $items));
372+
}
373+
346374
}

src/Eloquent/Model.php

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

2323
protected $index;
2424

25+
protected $recordIndex;
26+
2527
protected $primaryKey = '_id';
2628

2729
protected $keyType = 'string';
@@ -33,12 +35,16 @@ public function __construct(array $attributes = [])
3335
{
3436
parent::__construct($attributes);
3537
$this->setIndex();
38+
$this->setRecordIndex();
3639
$this->forcePrimaryKey();
3740
}
3841

3942

40-
public function setIndex()
43+
public function setIndex($index = null)
4144
{
45+
if ($index){
46+
return $this->index = $index;
47+
}
4248
$this->index = $this->index ?? $this->getTable();
4349
unset($this->table);
4450
}
@@ -51,6 +57,20 @@ public function setTable($index)
5157
return $this;
5258
}
5359

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

5575
public function forcePrimaryKey()
5676
{
@@ -119,6 +139,11 @@ public function freshTimestamp()
119139
* @inheritdoc
120140
*/
121141
public function getTable()
142+
{
143+
return $this->getIndex();
144+
}
145+
146+
public function getIndex()
122147
{
123148
return $this->index ? : parent::getTable();
124149
}

0 commit comments

Comments
 (0)