我希望在一个有模型关系的项目上使用弹性搜索。
就目前而言,弹性搜索是有效的,我跟踪了这个文档,他解释了如何从这个包开始:
问题是,我需要能够通过关系进行搜索。
这是我的复合弹性迁移:
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在我正在寻找的对象中没有任何关系。
我没有找到任何关于如何使用包的联接映射方法的文档。
发布于 2021-02-04 09:00:11
好的,我找到了解决方案,问题是在迁移过程中,您必须使用对象来索引belongsToMany关系
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(),
];
}结果和现在预期的一样

发布于 2020-09-23 02:41:18
如果您想获得类别的"nom“,请用复合模型编写
'categorie' => $this->categorie->nom ?? null,$this->categorie()返回关系,而不是对象。
发布于 2021-02-02 22:24:19
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);
}https://stackoverflow.com/questions/63502028
复制相似问题