亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel Elasticsearch babenkoivan

Laravel Elasticsearch babenkoivan

PHP
慕哥9229398 2023-10-22 20:34:00
我希望在具有模型關系的項目上使用彈性搜索?,F在彈性搜索正在工作,我遵循了這個文檔,他解釋了如何從這個包開始: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 關注
  • 167 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號