diff --git a/src/ScoutEngine.php b/src/ScoutEngine.php index 1965fb6..824a06e 100755 --- a/src/ScoutEngine.php +++ b/src/ScoutEngine.php @@ -41,7 +41,7 @@ public function update($models) $params['body'][] = [ 'update' => [ '_id' => $model->getKey(), - '_index' => $this->index, + '_index' => $this->getIndex($model), '_type' => $model->searchableAs(), ] ]; @@ -70,7 +70,7 @@ public function delete($models) $params['body'][] = [ 'delete' => [ '_id' => $model->getKey(), - '_index' => $this->index, + '_index' => $this->getIndex($model), '_type' => $model->searchableAs(), ] ]; @@ -121,7 +121,7 @@ public function paginate(Builder $builder, $perPage, $page) protected function performSearch(Builder $builder, array $options = []) { $params = [ - 'index' => $this->index, + 'index' => $this->getIndex($builder->model), 'type' => $builder->model->searchableAs(), 'body' => [ 'query' => [ @@ -195,4 +195,19 @@ public function getTotalCount($results) { return $results['hits']['total']; } + + /** + * Get the `index` name for the model. + * @param \Illuminate\Database\Eloquent\Model $model + * @return string [description] + */ + private function getIndex($model = null) + { + $method = 'getElasticsearchIndexName'; + if (!$model || !method_exists($model, $method)) { + return $this->index; + } + + return $model->$method(); + } }