Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Commit

Permalink
Added support for Laravel Scout 6.0 and 7.0, Laravel 5.8 (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
semyonchetvertnyh authored and ErickTamayo committed May 14, 2019
1 parent fb5b843 commit c300b9b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
33 changes: 20 additions & 13 deletions src/ElasticsearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
});
}

/**
Expand All @@ -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.
*
Expand Down
3 changes: 2 additions & 1 deletion tests/ElasticsearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down

0 comments on commit c300b9b

Please sign in to comment.