为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel Elasticsearch babenkoivan

Laravel Elasticsearch babenkoivan

PHP
慕哥9229398 2023-10-22 20:34:00
我希望在具有模型关系的项目上使用弹性搜索。现在弹性搜索正在工作,我遵循了这个文档,他解释了如何从这个包开始:Elasticsearch/elasticsearchBabenkoivan/Elastic-migrationsBabenkoivan/弹性适配器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 在我正在寻找的对象中没有任何关系。而且我没有找到任何有关如何使用包的连接映射方法的文档。
查看完整描述

3 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

好的,我已经找到了解决方案,问题出在迁移中,您必须使用对象才能索引 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(),

        ];

    }

结果现在正如预期的那样

https://img1.sycdn.imooc.com/6535186d0001afb506550226.jpg

查看完整回答
反对 回复 2023-10-22
?
Cats萌萌

TA贡献1805条经验 获得超9个赞

与 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);

    }


查看完整回答
反对 回复 2023-10-22
?
DIEA

TA贡献1820条经验 获得超2个赞

如果你想得到分类的“nom”,把它写在 composant Model 中

'categorie' => $this->categorie->nom ?? null,

$this->categorie() 返回关系,而不是对象。


查看完整回答
反对 回复 2023-10-22
  • 3 回答
  • 0 关注
  • 80 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信