Skip to content

Instantly share code, notes, and snippets.

@sjardim
Created May 31, 2022 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjardim/54708e8774c66cea5a7c95ff0b515a58 to your computer and use it in GitHub Desktop.
Save sjardim/54708e8774c66cea5a7c95ff0b515a58 to your computer and use it in GitHub Desktop.
Laravel command to update Meilisearch's index and filterable attributes
<?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