Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3 from cfpinto/multiple_indexes
Browse files Browse the repository at this point in the history
Added support for multiple indexes using searchableAs
  • Loading branch information
cfpinto authored Dec 12, 2018
2 parents 829a9a0 + 1d22a4b commit 876cb67
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/Engines/ElasticSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@


use Elasticsearch\Client;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
use Laravel\Scout\Searchable;

/**
* Class ElasticSearchEngine
Expand Down Expand Up @@ -55,10 +57,11 @@ public function update($models)
$params['body'] = [];
$models->each(
function ($model) use (&$params) {
/** @var Searchable $model */
$params['body'][] = [
'update' => [
'_id' => $this->getElasticKey($model),
'_index' => $this->index,
'_index' => $this->getIndex($model),
'_type' => $model->searchableAs(),
]
];
Expand All @@ -83,10 +86,11 @@ public function delete($models)
$params['body'] = [];
$models->each(
function ($model) use (&$params) {
/** @var Searchable $model */
$params['body'][] = [
'delete' => [
'_id' => $this->getElasticKey($model),
'_index' => $this->index,
'_index' => $this->getIndex($model),
'_type' => $model->searchableAs(),
]
];
Expand Down Expand Up @@ -197,15 +201,15 @@ public function getTotalCount($results)
protected function performSearch(Builder $builder, array $options = [])
{
$params = [
'index' => $this->index,
'index' => $this->getIndex($builder->model),
'type' => $builder->index ?: $builder->model->searchableAs(),
'body' => [
'query' => [
'bool' => [
'must' => [
[
'query_string' => [
'query' => "*{$builder->query}*"
'query' => !empty(trim($builder->query, '*\s')) ? "*{$builder->query}*" : "*"
]
]
]
Expand Down Expand Up @@ -270,7 +274,7 @@ function ($value, $key) {
}

/**
* Generates the sort if theres any.
* Generates the sort if there's any.
*
* @param Builder $builder
*
Expand All @@ -289,6 +293,11 @@ function ($order) {
)->toArray();
}

/**
* @param Searchable|Model $model
*
* @return mixed
*/
protected function getElasticKey($model)
{
if (method_exists($model, 'getScoutKey')) {
Expand All @@ -297,4 +306,14 @@ protected function getElasticKey($model)

return $model->getKey();
}

/**
* @param Searchable $model
*
* @return string
*/
protected function getIndex($model)
{
return $this->index . '_' . $model->searchableAs();
}
}

0 comments on commit 876cb67

Please sign in to comment.