From c300b9b265777479284df7ff50944247eab1d0ef Mon Sep 17 00:00:00 2001 From: Semyon Chetvertnyh Date: Tue, 14 May 2019 17:12:07 +0300 Subject: [PATCH] Added support for Laravel Scout 6.0 and 7.0, Laravel 5.8 (#119) --- composer.json | 2 +- src/ElasticsearchEngine.php | 33 +++++++++++++++++++------------ tests/ElasticsearchEngineTest.php | 3 ++- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 91c8f6f..6574a31 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "keywords": ["laravel", "scout", "elasticsearch", "elastic"], "require": { "php": ">=5.6.4", - "laravel/scout": "^5.0", + "laravel/scout": "^5.0|^6.0|^7.0", "elasticsearch/elasticsearch": "^5.0" }, "require-dev": { diff --git a/src/ElasticsearchEngine.php b/src/ElasticsearchEngine.php index 1a65944..68308b8 100644 --- a/src/ElasticsearchEngine.php +++ b/src/ElasticsearchEngine.php @@ -6,7 +6,6 @@ use Laravel\Scout\Engines\Engine; use Elasticsearch\Client as Elastic; use Illuminate\Database\Eloquent\Collection; -use Illuminate\Support\Collection as BaseCollection; class ElasticsearchEngine extends Engine { @@ -212,21 +211,16 @@ public function mapIds($results) public function map(Builder $builder, $results, $model) { if ($results['hits']['total'] === 0) { - return Collection::make(); + return $model->newCollection(); } - $keys = collect($results['hits']['hits']) - ->pluck('_id')->values()->all(); + $keys = collect($results['hits']['hits'])->pluck('_id')->values()->all(); - $models = $model->getScoutModelsByIds( - $builder, $keys - )->keyBy(function ($model) { - return $model->getScoutKey(); - }); - - return collect($results['hits']['hits'])->map(function ($hit) use ($model, $models) { - return isset($models[$hit['_id']]) ? $models[$hit['_id']] : null; - })->filter()->values(); + return $model->getScoutModelsByIds( + $builder, $keys + )->filter(function ($model) use ($keys) { + return in_array($model->getScoutKey(), $keys); + }); } /** @@ -240,6 +234,19 @@ public function getTotalCount($results) return $results['hits']['total']; } + /** + * Flush all of the model's records from the engine. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @return void + */ + public function flush($model) + { + $model->newQuery() + ->orderBy($model->getKeyName()) + ->unsearchable(); + } + /** * Generates the sort if theres any. * diff --git a/tests/ElasticsearchEngineTest.php b/tests/ElasticsearchEngineTest.php index 11b3bf1..f5e04a6 100644 --- a/tests/ElasticsearchEngineTest.php +++ b/tests/ElasticsearchEngineTest.php @@ -114,7 +114,8 @@ public function test_map_correctly_maps_results_to_models() $model = Mockery::mock('Illuminate\Database\Eloquent\Model'); $model->shouldReceive('getScoutKey')->andReturn('1'); - $model->shouldReceive('getScoutModelsByIds')->once()->with($builder, ['1'])->andReturn(Collection::make([$model])); + $model->shouldReceive('getScoutModelsByIds')->once()->with($builder, ['1'])->andReturn($models = Collection::make([$model])); + $model->shouldReceive('newCollection')->andReturn($models); $results = $engine->map($builder, [ 'hits' => [