Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

t0berius's avatar

laravel scout (meilisearch) add filterableAttributes / sortableAttributes upon import

Running meilisearch 0.21 there's a breaking change, you need to specify all filterableAttributes and sortableAttributes to a list, see:

https://docs.meilisearch.com/reference/features/sorting.html#configuring-meilisearch-for-sorting-at-search-time

Any idea how to include this to the import process of scout itself, so there is no need to specify these attributes and run curl manually?

0 likes
9 replies
t0berius's avatar

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?

tanerkay's avatar

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.

sjardim's avatar

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...');
    }
}
4 likes
t0berius's avatar

Just run it as part of migration. Think this is the most suitable place since it's related to the database somehow.

Please or to participate in this conversation.