0

I'm trying to use "babenkoivan/scout-elasticsearch-driver" with "astrotomic/laravel-translatable", but i don't understand how I could index the translated words.

My Model looks like :

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

Best regards

Noweh
  • 583
  • 5
  • 16

1 Answers1

0

I found a solution with the override of the method public function toSearchableArray() with something like:

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

The purpose of $mapping=[] is only to define the structure of data. Something like that is expected:

    protected $mapping = [
        'properties' => [
            'title_en' => [
                'type' => 'string'
            ],
            'title_fr' => [
                'type' => 'string'
            ],
            ...
        ]
    ];
Noweh
  • 583
  • 5
  • 16