【问题标题】:Laravel-scout : ElasticSearch with Translatable Entities (astrotomic/laravel-translatable)Laravel-scout : 带有可翻译实体的 ElasticSearch (astrotomic/laravel-translatable)
【发布时间】:2021-02-24 09:50:59
【问题描述】:

我正在尝试将“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'
      ...
   ];

最好的问候

【问题讨论】:

    标签: laravel elasticsearch laravel-scout


    【解决方案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'
                ],
                ...
            ]
        ];
    

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      相关资源
      最近更新 更多