Laravel Scout 和 Algolia 的多语言索引

编程入门 行业动态 更新时间:2024-10-27 02:30:26
本文介绍了Laravel Scout 和 Algolia 的多语言索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我应该如何管理多语言索引(例如:page/page_translations 模型应该变成 page_en/page_fr 索引).我正在使用DimsavTranslatable"包.

How should I manage the multi-language indexes (For example: page / page_translations models should become page_en / page_fr indexes). I am using "DimsavTranslatable" package.

页面模型:id、status_id、created_at、updated_at

Page model: id, status_id, created_at, updated_at

PageTranslation 模型:id、page_id、locale、title、slug、body

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

Algolia 对此提供支持 (www.algolia/doc/guides/search/multilingual-search/) 但我不确定如何使用 Laravel Scout 实现这一点.

Algolia offers support for this (www.algolia/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='英文标题', locale_id='1'

objectID=1, title='English title', locale_id='1'

objectID=2, title='Franch title', locale_id='2'

objectID=2, title='Franch title', locale_id='2'

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

或者也许是更好的方法?也许要分别索引 page/page_translations 并在两者中搜索?

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

我想实现以下目标:

pages_en 索引:objectID=1、title='英文标题'等

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

pages_fr 索引:objectID=2、title='Franch title' 等

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

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

推荐答案

我想了很多,我认为最好的方法是为每个模型使用 1 个索引并利用可以传递给 的回调::search()

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

首先你需要使用toSearchableArray()来准备数据.我会取消所有不必要的属性(如日期),然后在其 ISO 下嵌套内容.

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" } }

请注意,Algolia 对每条记录的限制为 10KB.处理这个问题的最好方法是截断你最大的属性.别担心,它不会影响相关性.如果您错过了文章的后半部分,通常所有相关内容都已在前半部分.

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.

然后前往您的仪表板并将 fr 和 en 添加到 searchableAttributes.

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

您可以在查询时使用传递给搜索的回调来限制 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);

经过深思熟虑,我真的认为这是在限制条件下使用 Laravel Scout 的最简单的方法.

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

请告诉我你的想法:)

更多推荐

Laravel Scout 和 Algolia 的多语言索引

本文发布于:2023-11-28 19:50:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多语言   索引   Laravel   Scout   Algolia

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!