首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel-scout :带有可翻译实体的ElasticSearch (占星术/laravel-可翻译)

Laravel-scout :带有可翻译实体的ElasticSearch (占星术/laravel-可翻译)
EN

Stack Overflow用户
提问于 2021-02-24 17:51:00
回答 1查看 132关注 0票数 0

我正在尝试使用"babenkoivan/scout-elasticsearch-driver“和"astrotomic/laravel-translatable",但我不明白如何索引翻译后的单词。

我的模型看起来像:

代码语言:javascript
复制
namespace App\Models;

use Astrotomic\Translatable\Translatable;
use App\Models\Search\ShowIndexConfigurator;
use ScoutElastic\Searchable;
...

class Show extends BaseModel
{
    ...
    use Translatable;
    use Searchable;

    protected $indexConfigurator = ShowIndexConfigurator::class;

    protected $searchRules = [
        //
    ];

    protected $mapping = [
        'properties' => [
            // How to index localized translations ???
            'title' => [
                'type' => 'string'
            ],
        ]
    ];
   
   ....
   
   public $translatedAttributes = [
      ...,
      'title'
      ...
   ];

诚挚的问候

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-26 07:51:00

我找到了一个覆盖方法public function toSearchableArray()的解决方案,如下所示:

代码语言:javascript
复制
public function toSearchableArray(): array
    {
        $document = [];
        if ($this->published) {
            $document = [
                //...
            ];
            foreach ($this->translations()->get() as $translation)
            {
                if (!$translation->active) {
                    continue;
                }

                $locale = $translation->locale;
                $document['title_' . $locale] = $translation->title;
                $document['url_' . $locale] = $this->getLink($locale);
                $document['sub_title_' . $locale] = $translation->sub_title;
                $document['keywords_' . $locale] = "";
            }
        }

        return $document;
    }

$mapping=[]的目的仅仅是定义数据的结构。这样的事情是可以预料到的:

代码语言:javascript
复制
    protected $mapping = [
        'properties' => [
            'title_en' => [
                'type' => 'string'
            ],
            'title_fr' => [
                'type' => 'string'
            ],
            ...
        ]
    ];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66348404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档