Multi-language indexes with Laravel Scout and Algolia

那年仲夏 提交于 2019-12-09 11:20:26

问题


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.

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


回答1:


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.

Search

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 :)



来源:https://stackoverflow.com/questions/44071033/multi-language-indexes-with-laravel-scout-and-algolia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!