Skip to content

Commit bee74a3

Browse files
committed
Scout 6/7 fixes + rebranding.
1 parent 54022d6 commit bee74a3

File tree

8 files changed

+127
-77
lines changed

8 files changed

+127
-77
lines changed

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: php
2+
3+
php:
4+
- 7.0
5+
- 7.1
6+
- 7.2
7+
8+
sudo: false
9+
10+
install: travis_retry composer install --no-interaction --prefer-dist
11+
12+
before_script:
13+
- sleep 10
14+
15+
script: vendor/bin/phpunit --verbose

README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
# Laravel Scout Elasticsearch Driver
22

33
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
4+
[![Build Status](https://travis-ci.com/boxed-code/laravel-scout-elasticsearch.svg?branch=master)](https://travis-ci.com/boxed-code/laravel-scout-elasticsearch)
45

5-
This package makes is the [Elasticsearch](https://www.elastic.co/products/elasticsearch) driver for Laravel Scout.
6+
This is a basic [elastic](https://www.elastic.co/products/elasticsearch) search backed driver [for Laravel Scout](https://laravel.com/docs/5.8/scout).
7+
8+
This driver has a simple configuration, requiring you to only publish the configuration and set the hostname of your elasticsearch node.
9+
10+
Based on the original work of [ErickTamayo](https://github.com/ErickTamayo/laravel-scout-elastic).
11+
12+
**Requires Scout 5.*, 6.* or 7.*, Laravel >=5.4, PHP >=7.0**
613

714
## Contents
815

@@ -16,10 +23,10 @@ This package makes is the [Elasticsearch](https://www.elastic.co/products/elasti
1623
You can install the package via composer:
1724

1825
``` bash
19-
composer require tamayo/laravel-scout-elastic
26+
composer require boxed-code/laravel-scout-elasticsearch
2027
```
2128

22-
You must add the Scout service provider and the package service provider in your app.php config:
29+
If you're using Laravel 5.4, you must add the Scout service provider and the package service provider in your app.php config:
2330

2431
```php
2532
// config/app.php
@@ -31,12 +38,9 @@ You must add the Scout service provider and the package service provider in your
3138
],
3239
```
3340

34-
### Setting up Elasticsearch configuration
35-
You must have a Elasticsearch server up and running with the index you want to use created
41+
### Add the elastic search configuration
3642

37-
If you need help with this please refer to the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)
38-
39-
After you've published the Laravel Scout package configuration:
43+
Add the following to your scout configuration:
4044

4145
```php
4246
// config/scout.php
@@ -45,7 +49,6 @@ After you've published the Laravel Scout package configuration:
4549

4650
...
4751
'elasticsearch' => [
48-
'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
4952
'hosts' => [
5053
env('ELASTICSEARCH_HOST', 'http://localhost'),
5154
],
@@ -55,9 +58,11 @@ After you've published the Laravel Scout package configuration:
5558

5659
## Usage
5760

58-
Now you can use Laravel Scout as described in the [official documentation](https://laravel.com/docs/5.3/scout)
61+
Now you can use Laravel Scout as described in the [official documentation](https://laravel.com/docs/5.8/scout)
62+
5963
## Credits
6064

65+
- [Oliver Green](https://github.com/olsgreen)
6166
- [Erick Tamayo](https://github.com/ericktamayo)
6267
- [All Contributors](../../contributors)
6368

composer.json

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
{
2-
"name": "tamayo/laravel-scout-elastic",
3-
"description": "Elastic Driver for Laravel Scout",
4-
"keywords": ["laravel", "scout", "elasticsearch", "elastic"],
2+
"name": "boxed-code/laravel-scout-elastic",
3+
"description": "Elasticsearch Driver for Laravel Scout",
4+
"keywords": ["laravel", "scout", "elasticsearch", "elastic", "aws", "elasticsearch service"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Oliver Green",
9+
"email": "oliver@boxedcode.co.uk"
10+
}
11+
],
512
"require": {
6-
"php": ">=5.6.4",
7-
"laravel/scout": "^5.0",
13+
"php": ">=7.0",
14+
"laravel/scout": "^5.0|^6.0|^7.0",
815
"elasticsearch/elasticsearch": "^5.0"
916
},
1017
"require-dev": {
11-
"fzaninotto/faker": "~1.4",
12-
"mockery/mockery": "0.9.*",
13-
"phpunit/phpunit": "~5.0"
18+
"mockery/mockery": "~1.0",
19+
"phpunit/phpunit": "~6.0"
1420
},
1521
"autoload": {
1622
"psr-4": {
17-
"ScoutEngines\\Elasticsearch\\": "src/"
23+
"BoxedCode\\Laravel\\Scout\\": "src/"
1824
}
1925
},
2026
"autoload-dev": {
2127
"psr-4": {
22-
"ScoutEngines\\Elasticsearch\\Test\\": "tests"
28+
"Tests\\": "tests"
2329
}
2430
},
2531
"suggest": {
@@ -28,7 +34,7 @@
2834
"extra": {
2935
"laravel": {
3036
"providers": [
31-
"ScoutEngines\\Elasticsearch\\ElasticsearchProvider"
37+
"BoxedCode\\Laravel\\Scout\\ElasticsearchServiceProvider"
3238
]
3339
}
3440
},

src/ElasticsearchEngine.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<?php
22

3-
namespace ScoutEngines\Elasticsearch;
3+
namespace BoxedCode\Laravel\Scout;
44

55
use Laravel\Scout\Builder;
66
use Laravel\Scout\Engines\Engine;
77
use Elasticsearch\Client as Elastic;
88
use Illuminate\Database\Eloquent\Collection;
9-
use Illuminate\Support\Collection as BaseCollection;
109

1110
class ElasticsearchEngine extends Engine
1211
{
@@ -212,23 +211,29 @@ public function mapIds($results)
212211
public function map(Builder $builder, $results, $model)
213212
{
214213
if ($results['hits']['total'] === 0) {
215-
return Collection::make();
214+
return $model->newCollection();
216215
}
217-
218-
$keys = collect($results['hits']['hits'])
219-
->pluck('_id')->values()->all();
220-
221-
$models = $model->getScoutModelsByIds(
222-
$builder, $keys
223-
)->keyBy(function ($model) {
224-
return $model->getScoutKey();
225-
});
226-
227-
return collect($results['hits']['hits'])->map(function ($hit) use ($model, $models) {
228-
return isset($models[$hit['_id']]) ? $models[$hit['_id']] : null;
229-
})->filter()->values();
216+
$keys = collect($results['hits']['hits'])->pluck('_id')->values()->all();
217+
return $model->getScoutModelsByIds(
218+
$builder, $keys
219+
)->filter(function ($model) use ($keys) {
220+
return in_array($model->getScoutKey(), $keys);
221+
});
230222
}
231223

224+
/**
225+
* Flush all of the model's records from the engine.
226+
*
227+
* @param \Illuminate\Database\Eloquent\Model $model
228+
* @return void
229+
*/
230+
public function flush($model)
231+
{
232+
$model->newQuery()
233+
->orderBy($model->getKeyName())
234+
->unsearchable();
235+
}
236+
232237
/**
233238
* Get the total count from a raw result returned by the engine.
234239
*
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
namespace ScoutEngines\Elasticsearch;
3+
namespace BoxedCode\Laravel\Scout;
44

55
use Laravel\Scout\EngineManager;
66
use Illuminate\Support\ServiceProvider;
77
use Elasticsearch\ClientBuilder as ElasticBuilder;
88

9-
class ElasticsearchProvider extends ServiceProvider
9+
class ElasticsearchServiceProvider extends ServiceProvider
1010
{
1111
/**
1212
* Bootstrap the application services.

tests/AbstactTestCase.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Mockery;
6+
use PHPUnit\Framework\TestCase;
7+
8+
abstract class AbstractTestCase extends TestCase
9+
{
10+
protected function tearDown()
11+
{
12+
Mockery::close();
13+
14+
parent::tearDown();
15+
}
16+
}

tests/ElasticsearchEngineTest.php

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?php
22

3+
namespace Tests;
4+
5+
use BoxedCode\Laravel\Scout\ElasticsearchEngine;
36
use Illuminate\Database\Eloquent\Collection;
47
use Laravel\Scout\Builder;
5-
use ScoutEngines\Elasticsearch\ElasticsearchEngine;
8+
use Mockery;
9+
use Tests\Fixtures\TestModel;
610

7-
class ElasticsearchEngineTest extends PHPUnit_Framework_TestCase
11+
class ElasticsearchEngineTest extends AbstractTestCase
812
{
9-
public function tearDown()
10-
{
11-
Mockery::close();
12-
}
13-
1413
public function test_update_adds_objects_to_index()
1514
{
1615
$client = Mockery::mock('Elasticsearch\Client');
@@ -31,7 +30,7 @@ public function test_update_adds_objects_to_index()
3130
]);
3231

3332
$engine = new ElasticsearchEngine($client, 'scout');
34-
$engine->update(Collection::make([new ElasticsearchEngineTestModel]));
33+
$engine->update(Collection::make([new TestModel]));
3534
}
3635

3736
public function test_delete_removes_objects_to_index()
@@ -50,7 +49,7 @@ public function test_delete_removes_objects_to_index()
5049
]);
5150

5251
$engine = new ElasticsearchEngine($client, 'scout');
53-
$engine->delete(Collection::make([new ElasticsearchEngineTestModel]));
52+
$engine->delete(Collection::make([new TestModel]));
5453
}
5554

5655
public function test_search_sends_correct_parameters_to_elasticsearch()
@@ -76,7 +75,7 @@ public function test_search_sends_correct_parameters_to_elasticsearch()
7675
]);
7776

7877
$engine = new ElasticsearchEngine($client, 'scout');
79-
$builder = new Laravel\Scout\Builder(new ElasticsearchEngineTestModel, 'zonda');
78+
$builder = new \Laravel\Scout\Builder(new TestModel, 'zonda');
8079
$builder->where('foo', 1);
8180
$builder->where('bar', [1, 3]);
8281
$builder->orderBy('id', 'desc');
@@ -90,8 +89,8 @@ public function test_builder_callback_can_manipulate_search_parameters_to_elasti
9089
$client->shouldReceive('search')->with('modified_by_callback');
9190

9291
$engine = new ElasticsearchEngine($client, 'scout');
93-
$builder = new Laravel\Scout\Builder(
94-
new ElasticsearchEngineTestModel(),
92+
$builder = new \Laravel\Scout\Builder(
93+
new TestModel(),
9594
'huayra',
9695
function (\Elasticsearch\Client $client, $query, $params) {
9796
$this->assertNotEmpty($params);
@@ -114,8 +113,9 @@ public function test_map_correctly_maps_results_to_models()
114113

115114
$model = Mockery::mock('Illuminate\Database\Eloquent\Model');
116115
$model->shouldReceive('getScoutKey')->andReturn('1');
117-
$model->shouldReceive('getScoutModelsByIds')->once()->with($builder, ['1'])->andReturn(Collection::make([$model]));
118-
116+
$model->shouldReceive('getScoutModelsByIds')->once()->with($builder, ['1'])->andReturn($models = Collection::make([$model]));
117+
$model->shouldReceive('newCollection')->andReturn($models);
118+
119119
$results = $engine->map($builder, [
120120
'hits' => [
121121
'total' => '1',
@@ -129,27 +129,4 @@ public function test_map_correctly_maps_results_to_models()
129129

130130
$this->assertEquals(1, count($results));
131131
}
132-
}
133-
134-
class ElasticsearchEngineTestModel extends \Illuminate\Database\Eloquent\Model
135-
{
136-
public function getIdAttribute()
137-
{
138-
return 1;
139-
}
140-
141-
public function searchableAs()
142-
{
143-
return 'table';
144-
}
145-
146-
public function getKey()
147-
{
148-
return '1';
149-
}
150-
151-
public function toSearchableArray()
152-
{
153-
return ['id' => 1];
154-
}
155-
}
132+
}

tests/Fixtures/TestModel.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Tests\Fixtures;
4+
5+
class TestModel extends \Illuminate\Database\Eloquent\Model
6+
{
7+
public function getIdAttribute()
8+
{
9+
return 1;
10+
}
11+
12+
public function searchableAs()
13+
{
14+
return 'table';
15+
}
16+
17+
public function getKey()
18+
{
19+
return '1';
20+
}
21+
22+
public function toSearchableArray()
23+
{
24+
return ['id' => 1];
25+
}
26+
}

0 commit comments

Comments
 (0)