Home » Php » php – Multi-language indexes with Laravel Scout and Algolia

php – Multi-language indexes with Laravel Scout and Algolia

Posted by: admin July 12, 2020 Leave a comment


How should I manage the multi-language indexes (For example: page / page_translations models should become page_en / page_fr indexes). I am using “Dimsav\Translatable” package.

Page model: id, status_id, created_at, updated_at

PageTranslation model: id, page_id, locale, title, slug, body

Algolia offers support for this (https://www.algolia.com/doc/guides/search/multilingual-search/) but I am not sure how to achieve this with Laravel Scout.

The only solution that comes in my mind is to index both language rows (from the translations model) in the same index storing the locale and applying a condition on search.


objectID=1, title=’English title’, locale_id=’1′

objectID=2, title=’Franch title’, locale_id=’2′

$pages = App\PageTranslation::search('Star Trek')->where('locale_id', 1)->get();

Or maybe a better approach? Maybe to index page / page_translations separately and search in both?

I would like to achieve something like:

pages_en index : objectID=1, title=’English title’, etc.

pages_fr index : objectID=2, title=’Franch title’, etc.

$pages = App\Page::search('Star Trek')->where('locale', 'en')->get();

How to&Answers:

I thought about it a lot and I think the best way would be to use 1 index per model and take advandate of the callback you can pass to ::search()

Indexing data

First you need to use toSearchableArray() to prepare the data. I would unset every unnecessary attributes (like dates) then nest content under its ISO.

  objectID: 1,
  en: {
    title: "Title in english",
    body: "trucated body in english"
  fr: {
    title: "Titre en français",
    body: "contenu tronqué en français"

Please note that Algolia has a limit of 10KB per records. The best way to handle this is to truncate your biggest attributes. Don’t worry, it doesn’t impact relevance. If you miss the second half of your article, usually all the relevant content is already in the first haft.

Setup Algolia config in dashboard

Then head to your dashboard and add fr and en to the searchableAttributes.


You can restrict searchableAttributes at query time with a callback passed to the search

$lang = 'en';
Model::search($query, function ($algolia, $query, $options) use ($lang) {
    $options = array_merge($options, [
        'restrictSearchableAttributes' => [$lang],

    return $algolia->search($query, $options);

I created a trait to achieve something similar. Maybe you can do something similar, in order to have a easy-to-use syntax like:

Model::searchLang($lang, $query);

After all the thinking, I really think it’s the least hacky way to use Laravel Scout with your constraints.

Please let me know what you think 🙂