Laravel-scout:带有可翻译实体的 ElasticSearch (astrotomic/laravel-translatable)

Laravel-scout : ElasticSearch with Translatable Entities (astrotomic/laravel-translatable)

我正在尝试将“babenkoivan/scout-elasticsearch-driver”与“astrotomic/laravel-translatable”一起使用,但我不明白如何为翻译后的词建立索引。

我的模型看起来像:

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'
      ...
   ];

此致

我找到了一个覆盖方法的解决方案 public function toSearchableArray() 类似:

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=[]的目的只是为了定义数据的结构。预计会有这样的事情:

    protected $mapping = [
        'properties' => [
            'title_en' => [
                'type' => 'string'
            ],
            'title_fr' => [
                'type' => 'string'
            ],
            ...
        ]
    ];