Php laravel elasticsearch babenkoivan/弹性适配器模型关系
我希望在一个具有模型关系的项目上使用弹性搜索 目前,弹性搜索正在发挥作用,我已经按照下面的说明开始使用此软件包:Php laravel elasticsearch babenkoivan/弹性适配器模型关系,php,laravel,
elasticsearch,eloquent,laravel-7,Php,Laravel,
elasticsearch,Eloquent,Laravel 7,我希望在一个具有模型关系的项目上使用弹性搜索 目前,弹性搜索正在发挥作用,我已经按照下面的说明开始使用此软件包: 弹性搜索/弹性搜索 babenkoivan/弹性迁移 babenkoivan/弹性接头 babenkoivan/弹性童子军司机 问题是我需要能够通过关系进行搜索 这是我的复合弹性迁移: Index::create('composant', function(Mapping $mapping, Settings $settings){ $mapping->te
- 弹性搜索/弹性搜索
- babenkoivan/弹性迁移
- babenkoivan/弹性接头
- babenkoivan/弹性童子军司机
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'
]
]
]);
});
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',
],
],
]);
});
以下是我的分类弹性迁移:
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'
]
]
]);
});
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',
],
],
]);
});
我的复合模型:
以及我的分类模型:
因此,如果查看composant关系,可以看到连接映射返回category关系。我现在不知道我是否做对了,但我知道elasticsearch与我正在寻找的对象没有任何关系
我没有找到任何关于如何使用包的连接映射方法的文档。如果您想获得categorie的“nom”,请将其写在composant模型中
'categorie' => $this->categorie->nom ?? null,
$this->categorie()返回的是关系,而不是对象。belontoMany关系也有同样的问题,我做了同样的事情,以便将关系作为嵌套对象来获取,但当我尝试填充索引时,“mots_cles”字段保持为空,我不明白为什么 以下是迁移:
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'
]
]
]);
});
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);
}
好的,我找到了解决方案,问题是在迁移过程中,您必须使用object,以便像这样索引belongstomy关系
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(),
];
}
现在的结果和预期的一样