Laravel Elasticsearch babenkoivan

我希望在具有模型关系的项目上使用弹性搜索。

现在弹性搜索正在工作,我遵循了这个文档,他解释了如何从这个包开始:

  • Elasticsearch/elasticsearch

  • Babenkoivan/Elastic-migrations

  • Babenkoivan/弹性适配器

  • Babenkoivan/elastic-scout-driver

问题是我需要能够按关系搜索。

这是我的复合弹性迁移:

Index::create('composant', function(Mapping $mapping, Settings $settings){

        $mapping->text('reference');

        $mapping->keyword('designation');

        $mapping->join('categorie');


        $settings->analysis([

            'analyzer' => [

                'reference' => [

                    'type' => 'custom',

                    'tokenizer' => 'whitespace'    

                ],

                'designation' => [

                    'type' => 'custom',

                    'tokenizer' => 'whitespace'    

                ]

            ]

        ]);

    });

这是我的分类弹性迁移:


Index::create('categorie', function(Mapping $mapping, Settings $settings){

        $mapping->keyword('nom');


        $settings->analysis([

            'analyzer' => [

                'nom' => [

                    'type' => 'custom',

                    'tokenizer' => 'whitespace'    

                ]

            ]

        ]);

    });

我的作曲家模型:


public function categorie()

{

    return $this->belongsTo('App\Model\Categorie');

}


public function toSearchableArray()

{

    return [

        'reference' => $this->reference,

        'designation' => $this->designation,

        'categorie' => $this->categorie(),

    ];

}

和我的分类模型:


public function toSearchableArray()

{

    return [

        'nom' => $this->nom,

    ];

}

因此,如果您查看组合器关系,您可以看到联接映射返回分类关系。我现在不知道我做得对,但我知道的是 elasticsearch 在我正在寻找的对象中没有任何关系。


而且我没有找到任何有关如何使用包的连接映射方法的文档。


慕哥9229398
浏览 136回答 3
3回答

湖上湖

好的,我已经找到了解决方案,问题出在迁移中,您必须使用对象才能索引 belongToMany 这样的关系Index::create('stages', function (Mapping $mapping, Settings $settings) {            $mapping->text('intitule_stage');            $mapping->text('objectifs');            $mapping->text('contenu');            $mapping->object('mots_cles');        });在您的模型中:public function toSearchableArray()    {        return [            'intitule_stage' => $this->intitule_stage,            'objectifs' => $this->objectifs,            'contenu' => $this->contenu,            'n_stage' => $this->n_stage,            'mots_cles' => $this->motsCles()->get(),        ];    }结果现在正如预期的那样

Cats萌萌

与 belontoMany 关系存在相同的问题,并且为了将关系作为嵌套对象,我做了同样的事情,但是当我尝试填充我的索引时,字段“mots_cles”保持为空,我不明白为什么。这是迁移:Index::create('stages', function (Mapping $mapping, Settings $settings) {            $mapping->text('intitule_stage');            $mapping->text('objectifs');            $mapping->text('contenu');            $mapping->nested('motsCles', [                'properties' => [                    'mot_cle' => [                        'type' => 'keyword',                    ],                ],            ]);        });模型:public function toSearchableArray()    {        return [            'intitule_stage' => $this->intitule_stage,            'objectifs' => $this->objectifs,            'contenu' => $this->contenu,            'n_stage' => $this->n_stage,            'mots_cles' => $this->motsCles(),        ];    }public function motsCles()    {        return $this->belongsToMany(MotsCle::class);    }

DIEA

如果你想得到分类的“nom”,把它写在 composant Model 中'categorie' => $this->categorie->nom ?? null,$this->categorie() 返回关系,而不是对象。
打开App,查看更多内容
随时随地看视频慕课网APP