Laravel-scout: ElasticSearch с переводимыми сущностями (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'
      ...
   ];

С наилучшими пожеланиями


person Noweh    schedule 24.02.2021    source источник


Ответы (1)


Я нашел решение с переопределением метода 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'
            ],
            ...
        ]
    ];
person Noweh    schedule 25.02.2021