Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #6 from lkmadushan/support-72
Browse files Browse the repository at this point in the history
Supports PHP 7.2 and added missing doc comments
  • Loading branch information
shokme authored Apr 20, 2020
2 parents cd92536 + 23ac561 commit b1bf509
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 34 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": "^7.4",
"php": "^7.2.5",
"laravel/scout": "^8.0",
"meilisearch/meilisearch-php": "^0.9.0"
},
Expand Down
2 changes: 1 addition & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

return [
'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
'key' => env('MEILISEARCH_KEY', null)
'key' => env('MEILISEARCH_KEY', null),
];
23 changes: 20 additions & 3 deletions src/Console/IndexMeilisearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,37 @@

namespace Meilisearch\Scout\Console;

use Illuminate\Console\Command;
use MeiliSearch\Client;
use Illuminate\Console\Command;
use MeiliSearch\Exceptions\HTTPRequestException;
use Meilisearch\Engines\MeilisearchEngine;

class IndexMeilisearch extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scout:index {--d|delete : Delete an existing index} {--k|key : The name of primary key} {name : The name of the index}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create or delete an index';

/**
* Execute the console command.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
*
* @return void
*/
public function handle()
{
$client = new Client(config('meilisearch.host'), config('meilisearch.key'));

try {
if ($this->option('delete')) {
$client->deleteIndex($this->argument('name'));
Expand All @@ -28,7 +45,7 @@ public function handle()
if ($this->option('key')) {
$index = [
'uid' => $this->argument('name'),
'primaryKey' => $this->option('key')
'primaryKey' => $this->option('key'),
];
}
$client->createIndex($index);
Expand Down
49 changes: 36 additions & 13 deletions src/Engines/MeilisearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

namespace Meilisearch\Scout\Engines;

use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
use MeiliSearch\Client as Meilisearch;

class MeilisearchEngine extends Engine
{
protected Meilisearch $meilisearch;
protected bool $softDelete;
/**
* The Meilisearch client.
*
* @var Meilisearch
*/
protected $meilisearch;

/**
* Determines if soft deletes for Scout are enabled or not.
*
* @var bool
*/
protected $softDelete;

public function __construct(Meilisearch $meilisearch, bool $softDelete = false)
{
Expand All @@ -20,6 +32,7 @@ public function __construct(Meilisearch $meilisearch, bool $softDelete = false)
* Update the given model in the index.
*
* @param \Illuminate\Database\Eloquent\Collection $models
*
* @return void
*/
public function update($models)
Expand All @@ -42,7 +55,7 @@ public function update($models)
return array_merge($searchableData, $model->scoutMetadata());
})->filter()->values()->all();

if (!empty($objects)) {
if (! empty($objects)) {
$index->addDocuments($objects, $models->first()->getKeyName());
}
}
Expand All @@ -51,14 +64,15 @@ public function update($models)
* Remove the given model from the index.
*
* @param \Illuminate\Database\Eloquent\Collection $models
*
* @return void
*/
public function delete($models)
{
$index = $this->meilisearch->getIndex($models->first()->searchableAs());

$index->deleteDocuments(
$models->map(fn($model) => $model->getScoutKey())
$models->map->getScoutKey()
->values()
->all()
);
Expand All @@ -67,25 +81,27 @@ public function delete($models)
/**
* Perform the given search on the engine.
*
* @param \Laravel\Scout\Builder $builder
* @param Builder $builder
*
* @return mixed
*/
public function search(\Laravel\Scout\Builder $builder)
public function search(Builder $builder)
{
return $this->performSearch($builder, array_filter([
'limit' => $builder->limit
'limit' => $builder->limit,
]));
}

/**
* Perform the given search on the engine.
*
* @param \Laravel\Scout\Builder $builder
* @param Builder $builder
* @param int $perPage
* @param int $page
*
* @return mixed
*/
public function paginate(\Laravel\Scout\Builder $builder, $perPage, $page)
public function paginate(Builder $builder, $perPage, $page)
{
return $this->performSearch($builder, array_filter([
'limit' => $perPage,
Expand All @@ -95,11 +111,12 @@ public function paginate(\Laravel\Scout\Builder $builder, $perPage, $page)
/**
* Perform the given search on the engine.
*
* @param \Laravel\Scout\Builder $builder
* @param Builder $builder
* @param array $options
*
* @return mixed
*/
protected function performSearch(\Laravel\Scout\Builder $builder, array $options = [])
protected function performSearch(Builder $builder, array $options = [])
{
$meilisearch = $this->meilisearch->getIndex($builder->index ?: $builder->model->searchableAs());

Expand All @@ -119,6 +136,7 @@ protected function performSearch(\Laravel\Scout\Builder $builder, array $options
* Pluck and return the primary keys of the given results.
*
* @param mixed $results
*
* @return \Illuminate\Support\Collection
*/
public function mapIds($results)
Expand All @@ -132,12 +150,13 @@ public function mapIds($results)
/**
* Map the given results to instances of the given model.
*
* @param \Laravel\Scout\Builder $builder
* @param Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function map(\Laravel\Scout\Builder $builder, $results, $model)
public function map(Builder $builder, $results, $model)
{
if (is_null($results) || count($results['hits']) === 0) {
return $model->newCollection();
Expand All @@ -159,6 +178,7 @@ public function map(\Laravel\Scout\Builder $builder, $results, $model)
* Get the total count from a raw result returned by the engine.
*
* @param mixed $results
*
* @return int
*/
public function getTotalCount($results)
Expand All @@ -170,6 +190,7 @@ public function getTotalCount($results)
* Flush all of the model's records from the engine.
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
public function flush($model)
Expand All @@ -183,6 +204,7 @@ public function flush($model)
* Determine if the given model uses soft deletes.
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return bool
*/
protected function usesSoftDelete($model)
Expand All @@ -195,6 +217,7 @@ protected function usesSoftDelete($model)
*
* @param string $method
* @param array $parameters
*
* @return mixed
*/
public function __call($method, $parameters)
Expand Down
26 changes: 20 additions & 6 deletions src/MeilisearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,43 @@

namespace Meilisearch\Scout;

use Illuminate\Support\ServiceProvider;
use Laravel\Scout\EngineManager;
use MeiliSearch\Client;
use Laravel\Scout\EngineManager;
use Illuminate\Support\ServiceProvider;
use Meilisearch\Scout\Console\IndexMeilisearch;
use Meilisearch\Scout\Engines\MeilisearchEngine;

class MeilisearchServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'meilisearch');
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if($this->app->runningInConsole()) {
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('meilisearch.php'),
], 'config');
}

$this->commands([IndexMeilisearch::class]);
$this->commands([IndexMeilisearch::class]);
}

resolve(EngineManager::class)->extend('meilisearch', fn() => new MeilisearchEngine(new Client(config('meilisearch.host'), config('meilisearch.key'))));
resolve(EngineManager::class)->extend('meilisearch', function () {
return new MeilisearchEngine(
new Client(config('meilisearch.host'), config('meilisearch.key'))
);
});
}
}
2 changes: 1 addition & 1 deletion tests/Fixtures/SearchableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Meilisearch\Scout\Tests\Fixtures;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;

class SearchableModel extends Model
{
Expand Down
16 changes: 8 additions & 8 deletions tests/MeilisearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Meilisearch\Scout\Tests;

use stdClass;
use Mockery as m;
use MeiliSearch\Client;
use Illuminate\Database\Eloquent\Collection;
use Laravel\Scout\Builder;
use Illuminate\Database\Eloquent\Collection;
use Meilisearch\Scout\Engines\MeilisearchEngine;
use Meilisearch\Scout\Tests\Fixtures\SearchableModel;
use Mockery as m;
use stdClass;

class MeilisearchEngineTest extends TestCase
{
Expand All @@ -24,8 +24,8 @@ public function update_adds_objects_to_index()
$client->shouldReceive('getIndex')->with('table')->andReturn($index = m::mock(stdClass::class));
$index->shouldReceive('addDocuments')->with([
[
'id' => 1
]
'id' => 1,
],
]);

$engine = new MeilisearchEngine($client);
Expand Down Expand Up @@ -75,7 +75,7 @@ public function map_correctly_maps_results_to_models()
$results = $engine->map($builder, [
'nbHits' => 1, 'hits' => [
['id' => 1],
]
],
], $model);

$this->assertEquals(1, count($results));
Expand All @@ -93,7 +93,7 @@ public function map_method_respects_order()
new SearchableModel(['id' => 1]),
new SearchableModel(['id' => 2]),
new SearchableModel(['id' => 3]),
new SearchableModel(['id' => 4])
new SearchableModel(['id' => 4]),
]));

$builder = m::mock(Builder::class);
Expand All @@ -104,7 +104,7 @@ public function map_method_respects_order()
['id' => 2],
['id' => 4],
['id' => 3],
]
],
], $model);

$this->assertEquals(4, count($results));
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function setUp(): void
protected function getPackageProviders($app)
{
return [
MeilisearchServiceProvider::class
MeilisearchServiceProvider::class,
];
}

Expand Down

0 comments on commit b1bf509

Please sign in to comment.