I am also interested in a solution
Bump, no scout / meili users here?
Any idea? It' s just a simple line of code (simple HTTP request to meilisearch API to update the list of the filterableAttributes and sortableAttributes.
Where to place call?
You have to use the MeiliSearch API by getting a client instance, getting the index, and then applying the modifications, see this Github issue for an example: https://github.com/laravel/scout/issues/611#issuecomment-1087620688
There may be scope for a PR to laravel/scout that can pass the index instance to an optional model class method, e.g. configureMeiliSearchIndex(\MeiliSearch\Endpoints\Indexes $index), where one can set filterable/sortable/searchable attributes or any other modifications.
I'm still brainstorming myself.
I've built a command to help update Meilisearch's index whenever I need to add a new filter. Maybe it can help you :)
<?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...');
}
}
Just run it as part of migration.
Think this is the most suitable place since it's related to the database somehow.
Please sign in or create an account to participate in this conversation.