Laravel Scout和Algolia的多语言索引

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

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

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.

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

Page model: id, status_id, created_at, updated_at

PageTranslation模型:id,page_id,语言环境,标题,子词,正文

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

阿尔及利亚为此提供了支持( https: //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,标题=法国标题",locale_id ="2"

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

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

也许是更好的方法?也许要分别为页面/page_translations 建立索引并在两者中进行搜索?

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

我想实现以下目标:

pages_zh_CN 索引:objectID = 1,title =英文标题",等等.

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

pages_fr 索引:objectID = 2,title =法国标题",等等.

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

$pages = App\Page::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" } }

请注意,阿尔及利亚每条记录的大小上限为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.

请让我知道您的想法:)

Please let me know what you think :)

更多推荐

Laravel Scout和Algolia的多语言索引

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

发布评论

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

>www.elefans.com

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