Integrations / Frameworks / Laravel / Configure index
Feb. 07, 2019

Configure index

Optimize the search experience

Generate settings

Performance is important. However, 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 will do its best 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: Algolia Settings.

Synchronize 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. However, make sure you apply those settings locally running the scout:sync Artisan command.

Custom settings path

By default, all settings are created under config/scout-{index-name}.php. Horever, you are free to set the settings_path on 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 modify 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. Hence, if you need to have different indices for each environment, you only need to define a different PREFIX in your .env file.

$
SCOUT_PREFIX=demo_PROD_

Did you find this page helpful?