laravel elasticsearch babenkoivan/elastic-adapter 模型关系
laravel elasticsearch babenkoivan/elastic-adapter model relation
我希望在具有模型关系的项目上使用弹性搜索。
目前 elastic search 正在运行,我已经关注了这个 doc,他解释了如何从这个包开始:
- elasticsearch/elasticsearch
- babenkoivan/elastic-migrations
- babenkoivan/elastic-adapter
- 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,
];
}
因此,如果您查看组合关系,您可以看到连接映射 return 类别关系。我现在不知道我是否做对了,但我知道 elasticsearch 与我正在寻找的对象没有任何关系。
而且我没有找到任何关于如何使用包的连接映射方法的文档。
如果你想获得类别的“nom”,请将其写在 composant 模型中
'categorie' => $this->categorie->nom ?? null,
$this->categorie() return 关系,不是对象。
与 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);
}
好的,我找到了解决方案,问题出在迁移中,您必须使用 object 才能像这样索引 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(),
];
}
现在的结果和预期的一样
我希望在具有模型关系的项目上使用弹性搜索。
目前 elastic search 正在运行,我已经关注了这个 doc,他解释了如何从这个包开始:
- elasticsearch/elasticsearch
- babenkoivan/elastic-migrations
- babenkoivan/elastic-adapter
- 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,
];
}
因此,如果您查看组合关系,您可以看到连接映射 return 类别关系。我现在不知道我是否做对了,但我知道 elasticsearch 与我正在寻找的对象没有任何关系。
而且我没有找到任何关于如何使用包的连接映射方法的文档。
如果你想获得类别的“nom”,请将其写在 composant 模型中
'categorie' => $this->categorie->nom ?? null,
$this->categorie() return 关系,不是对象。
与 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);
}
好的,我找到了解决方案,问题出在迁移中,您必须使用 object 才能像这样索引 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(),
];
}
现在的结果和预期的一样