Integrations / Frameworks / Laravel / Configure Index

Optimize the search experience

Generate index settings

Performance is important. On top of that, for a search to be successful, results need to be relevant to the user. Scout Extended provides a scout:optimize Artisan command that you may use to optimize the search experience based on information from the searchable class:

$
php artisan scout:optimize

The Artisan command scout:optimize tries to generate the settings of your searchable class index, but you may need to edit those settings in config/scout-articles.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
return [

    /*
    |--------------------------------------------------------------------------
    | Searchable Attributes
    |--------------------------------------------------------------------------
    |
    | Limits the scope of a search to the attributes listed in this setting. Defining
    | specific attributes as searchable is critical for relevance because it gives
    | you direct control over what information the search engine should look at.
    |
    | Supported: Null, Array
    | Example: ["name", "email", "unordered(city)"]
    |
    */

    'searchableAttributes' => ['subject', 'body', 'slug', 'author_name', 'author_email'],

    /*
    |--------------------------------------------------------------------------
    | Custom Ranking
    |--------------------------------------------------------------------------
    |
    | Custom Ranking is about leveraging business metrics to rank search
    | results - it's crucial for any successful search experience. Make sure that
    | only "numeric" attributes are used, such as the number of sales or views.
    |
    | Supported: Null, Array
    | Examples: ['desc(comments_count)', 'desc(views_count)']
    |
    */

    'customRanking' => ['asc(sales_count)', 'desc(views_count)', 'desc(created_at)'],

    // ...
];

Feel free to dig further into all Algolia settings to optimize the search experience for your end users.

Synchronize index settings

Once you have verified the settings file, all you need to do is synchronize the settings with Algolia using the scout:sync Artisan command:

$
php artisan scout:sync

You may also edit settings using the Algolia Dashboard. Make sure to apply these settings locally with the scout:sync Artisan command if you do this, or your changes will be lost.

Custom settings path

By default, all settings live in config/scout-{index-name}.php. You can change this by setting the settings_path in the config/scout.php configuration file:

1
2
3
4
5
'algolia' => [
    'id' => env('ALGOLIA_APP_ID', ''),
    'secret' => env('ALGOLIA_SECRET', ''),
    'settings_path' => config_path('customPath'),
],

Custom index name

To customize the index name, you should implement the searchableAs method in your searchable class.

By default, the searchableAs method concatenates the prefix configuration key defined in config/scout.php and the table name. As usual, you can change this behavior:

1
2
3
4
5
6
7
8
9
class Article extends Model
{
    use Searchable;

    public function searchableAs()
    {
        return config('scout.prefix').'my_custom_name';
    }
}

Per-environment index name

Scout reads the prefix from your environment variables. If you need different indices for each environment, you should define a different PREFIX in your .env file.

$
SCOUT_PREFIX=demo_PROD_

Did you find this page helpful?