Skip to content

Commit d0f6a34

Browse files
committed
Add getElasticsearchIndexName, different model different index :)
1 parent 2a8df1c commit d0f6a34

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/ScoutEngine.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function update($models)
4141
$params['body'][] = [
4242
'update' => [
4343
'_id' => $model->getKey(),
44-
'_index' => $this->index,
44+
'_index' => $this->getIndex($model),
4545
'_type' => $model->searchableAs(),
4646
]
4747
];
@@ -70,7 +70,7 @@ public function delete($models)
7070
$params['body'][] = [
7171
'delete' => [
7272
'_id' => $model->getKey(),
73-
'_index' => $this->index,
73+
'_index' => $this->getIndex($model),
7474
'_type' => $model->searchableAs(),
7575
]
7676
];
@@ -121,7 +121,7 @@ public function paginate(Builder $builder, $perPage, $page)
121121
protected function performSearch(Builder $builder, array $options = [])
122122
{
123123
$params = [
124-
'index' => $this->index,
124+
'index' => $this->getIndex($builder->model),
125125
'type' => $builder->model->searchableAs(),
126126
'body' => [
127127
'query' => [
@@ -195,4 +195,19 @@ public function getTotalCount($results)
195195
{
196196
return $results['hits']['total'];
197197
}
198+
199+
/**
200+
* Get the `index` name for the model.
201+
* @param \Illuminate\Database\Eloquent\Model $model
202+
* @return string [description]
203+
*/
204+
private function getIndex($model = null)
205+
{
206+
$method = 'getElasticsearchIndexName';
207+
if (!$model || !method_exists($model, $method)) {
208+
return $this->index;
209+
}
210+
211+
return $model->$method();
212+
}
198213
}

0 commit comments

Comments
 (0)