Multilingual indexes with Laravel Scout and Algolia

How do I manage multilingual indexes (for example: page / page_translations models should become page_en / page_fr ). I am using the 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'm not sure how to achieve this with Laravel Scout .

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

Algolia

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 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();

+6
1

, , 1 , ::search()

toSearchableArray() . (, ), 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"
  }
}

, Algolia 10KB . - . , . , .

Algolia

fr en searchableAttributes.

searchableAttributes ,

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

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

- . , - , , :

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

, , Laravel Scout .

, , :)

+10

Source: https://habr.com/ru/post/1017387/


All Articles