Created
May 31, 2022 18:10
-
-
Save sjardim/54708e8774c66cea5a7c95ff0b515a58 to your computer and use it in GitHub Desktop.
Laravel command to update Meilisearch's index and filterable attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use MeiliSearch\Client; | |
use function PHPUnit\Framework\throwException; | |
class UpdateMeilisearchIndex extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'meilisearch:update'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Update Meilisearch\'s index and filterable attributes'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
$client = new Client(config('scout.meilisearch.host')); | |
$this->updateSortableAttributes($client); | |
$this->updateFilterableAttributes($client); | |
return Command::SUCCESS; | |
} | |
protected function updateSortableAttributes(Client $client):void | |
{ | |
$client->index('Resources')->updateSortableAttributes([ | |
'title', | |
'date' | |
]); | |
$this->info('Updated sortable attributes...'); | |
} | |
protected function updateFilterableAttributes(Client $client): void | |
{ | |
$client->index('Resources')->updateFilterableAttributes([ | |
'date', | |
'type', | |
'topics', | |
'contributors' | |
]); | |
$this->info('Updated filterable attributes...'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment