Skip to content

Commit

Permalink
Implement index managing for engines
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Apr 8, 2021
1 parent 733bb0d commit db9e7b2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 12 deletions.
24 changes: 13 additions & 11 deletions src/Console/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Laravel\Scout\Console;

use Exception;
use Illuminate\Console\Command;
use MeiliSearch\Client;
use MeiliSearch\Exceptions\ApiException;
use Laravel\Scout\EngineManager;

class IndexCommand extends Command
{
Expand All @@ -14,9 +14,9 @@ class IndexCommand extends Command
* @var string
*/
protected $signature = 'scout:index
{name : The name of the index}
{--d|delete : Delete an existing index}
{--k|key= : The name of primary key}
{name : The name of the index}';
{--k|key= : The name of primary key}';

/**
* The console command description.
Expand All @@ -28,16 +28,18 @@ class IndexCommand extends Command
/**
* Execute the console command.
*
* @param \MeiliSearch\Client $client
* @param \Laravel\Scout\EngineManager $manager
* @return void
*/
public function handle(Client $client)
public function handle(EngineManager $manager)
{
$engine = $manager->engine();

try {
if ($this->option('delete')) {
$client->deleteIndex($this->argument('name'));
$engine->deleteIndex($this->argument('name'));

$this->info('Index "'.$this->argument('name').'" deleted.');
$this->info('Index "' . $this->argument('name') . '" deleted.');

return;
}
Expand All @@ -48,10 +50,10 @@ public function handle(Client $client)
$options = ['primaryKey' => $this->option('key')];
}

$client->createIndex($this->argument('name'), $options);
$engine->createIndex($this->argument('name'), $options);

$this->info('Index "'.$this->argument('name').'" created.');
} catch (ApiException $exception) {
$this->info('Index "' . $this->argument('name') . '" created.');
} catch (Exception $exception) {
$this->error($exception->getMessage());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/EngineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class EngineManager extends Manager
* Get a driver instance.
*
* @param string|null $name
* @return mixed
* @return \Laravel\Scout\Engines\Engine
*/
public function engine($name = null)
{
Expand Down
26 changes: 26 additions & 0 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Scout\Engines;

use Algolia\AlgoliaSearch\SearchClient as Algolia;
use Exception;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Builder;

Expand Down Expand Up @@ -232,6 +233,31 @@ public function flush($model)
$index->clearObjects();
}

/**
* Create a search index.
*
* @param string $name
* @param array $options
* @return mixed
*
* @throws \Exception
*/
public function createIndex($name, array $options = [])
{
throw new Exception('Algolia indexes are created automatically upon adding objects.');
}

/**
* Delete a search index.
*
* @param string $name
* @return mixed
*/
public function deleteIndex($name)
{
return $this->algolia->initIndex($name)->delete();
}

/**
* Determine if the given model uses soft deletes.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Engines/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ abstract public function getTotalCount($results);
*/
abstract public function flush($model);

/**
* Create a search index.
*
* @param string $name
* @param array $options
* @return mixed
*/
abstract public function createIndex($name, array $options = []);

/**
* Delete a search index.
*
* @param string $name
* @return mixed
*/
abstract public function deleteIndex($name);

/**
* Get the results of the query as a Collection of primary keys.
*
Expand Down
27 changes: 27 additions & 0 deletions src/Engines/MeiliSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,33 @@ public function flush($model)
$index->deleteAllDocuments();
}

/**
* Create a search index.
*
* @param string $name
* @param array $options
* @return mixed
*
* @throws \MeiliSearch\Exceptions\ApiException
*/
public function createIndex($name, array $options = [])
{
return $this->meilisearch->createIndex($name, $options);
}

/**
* Delete a search index.
*
* @param string $name
* @return mixed
*
* @throws \MeiliSearch\Exceptions\ApiException
*/
public function deleteIndex($name)
{
return $this->meilisearch->deleteIndex($name);
}

/**
* Determine if the given model uses soft deletes.
*
Expand Down

0 comments on commit db9e7b2

Please sign in to comment.