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

Commit

Permalink
Compatibility with Scout 5 (#106)
Browse files Browse the repository at this point in the history
* Compatibility with Scout 5

No "cross-installation" possible due to changed method signature

Pass around new $builder var

Also: Use getScoutModelsByIds/getScoutKey

Inspired by laravel/scout@4.0...5.0#diff-f2faa85f27580a9b67d73a8a273bfef1 / laravel/scout@9f06b9c

* Remove PHP5 as laravel/scout^5 / laravel5.6 also removed it
  • Loading branch information
kronthto authored and ErickTamayo committed Oct 15, 2018
1 parent a8e5be7 commit fb5b843
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php
php:
- 7.0
- 5.6.6

before_script:
- sleep 10
Expand Down
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": "^3.0|^4.0",
"laravel/scout": "^5.0",
"elasticsearch/elasticsearch": "^5.0"
},
"require-dev": {
Expand Down
11 changes: 7 additions & 4 deletions src/ElasticsearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,12 @@ public function mapIds($results)
/**
* Map the given results to instances of the given model.
*
* @param \Laravel\Scout\Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @return Collection
*/
public function map($results, $model)
public function map(Builder $builder, $results, $model)
{
if ($results['hits']['total'] === 0) {
return Collection::make();
Expand All @@ -217,9 +218,11 @@ public function map($results, $model)
$keys = collect($results['hits']['hits'])
->pluck('_id')->values()->all();

$models = $model->whereIn(
$model->getKeyName(), $keys
)->get()->keyBy($model->getKeyName());
$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;
Expand Down
10 changes: 6 additions & 4 deletions tests/ElasticsearchEngineTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Database\Eloquent\Collection;
use Laravel\Scout\Builder;
use ScoutEngines\Elasticsearch\ElasticsearchEngine;

class ElasticsearchEngineTest extends PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -109,12 +110,13 @@ public function test_map_correctly_maps_results_to_models()
$client = Mockery::mock('Elasticsearch\Client');
$engine = new ElasticsearchEngine($client, 'scout');

$builder = Mockery::mock(Builder::class);

$model = Mockery::mock('Illuminate\Database\Eloquent\Model');
$model->shouldReceive('getKeyName')->andReturn('id');
$model->shouldReceive('whereIn')->once()->with('id', ['1'])->andReturn($model);
$model->shouldReceive('get')->once()->andReturn(Collection::make([new ElasticsearchEngineTestModel]));
$model->shouldReceive('getScoutKey')->andReturn('1');
$model->shouldReceive('getScoutModelsByIds')->once()->with($builder, ['1'])->andReturn(Collection::make([$model]));

$results = $engine->map([
$results = $engine->map($builder, [
'hits' => [
'total' => '1',
'hits' => [
Expand Down

0 comments on commit fb5b843

Please sign in to comment.