首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >/弹性适配器模型关系

/弹性适配器模型关系
EN

Stack Overflow用户
提问于 2020-08-20 09:20:08
回答 3查看 742关注 0票数 0

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

就目前而言,弹性搜索是有效的,我跟踪了这个文档,他解释了如何从这个包开始:

  • 弹性搜索/弹性搜索
  • babenkoivan/弹性迁移
  • babenkoivan/弹性适配器
  • 巴门科万/弹性童子军

问题是,我需要能够通过关系进行搜索。

这是我的复合弹性迁移:

代码语言:javascript
复制
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'    
                ]
            ]
        ]);
    });

在这里,我的范畴是弹性迁移:

代码语言:javascript
复制
Index::create('categorie', function(Mapping $mapping, Settings $settings){
        $mapping->keyword('nom');

        $settings->analysis([
            'analyzer' => [
                'nom' => [
                    'type' => 'custom',
                    'tokenizer' => 'whitespace'    
                ]
            ]
        ]);
    });

我的合成模型:

代码语言:javascript
复制
public function categorie()
{
    return $this->belongsTo('App\Model\Categorie');
}

public function toSearchableArray()
{
    return [
        'reference' => $this->reference,
        'designation' => $this->designation,
        'categorie' => $this->categorie(),
    ];
}

我的分类模型:

代码语言:javascript
复制
public function toSearchableArray()
{
    return [
        'nom' => $this->nom,
    ];
}

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

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-02-04 09:00:11

好的,我找到了解决方案,问题是在迁移过程中,您必须使用对象来索引belongsToMany关系

代码语言:javascript
复制
Index::create('stages', function (Mapping $mapping, Settings $settings) {
            $mapping->text('intitule_stage');
            $mapping->text('objectifs');
            $mapping->text('contenu');
            $mapping->object('mots_cles');
        });

在你的模型中:

代码语言:javascript
复制
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(),
        ];
    }

结果和现在预期的一样

票数 1
EN

Stack Overflow用户

发布于 2020-09-23 02:41:18

如果您想获得类别的"nom“,请用复合模型编写

代码语言:javascript
复制
'categorie' => $this->categorie->nom ?? null,

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

票数 0
EN

Stack Overflow用户

发布于 2021-02-02 22:24:19

belontoMany关系也有同样的问题,为了获得嵌套对象的关系,我做了同样的事情,但是当我试图填充我的索引字段"mots_cles“时,我不明白为什么。

以下是迁移:

代码语言:javascript
复制
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',
                    ],
                ],
            ]);
        });

模式:

代码语言:javascript
复制
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);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63502028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档