In my laravel 5.7/mysql app I use elasticsearch based on https://michaelstivala.com/learning-elasticsearch-with-laravel/ article. That is clear for me how to save/delete/search data which are based on one table. But I have tables :
CREATE TABLE `votes` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`description` MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_unicode_ci',
with searching on name and description fields
but I also have a table
CREATE TABLE `vote_items` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`vote_id` INT(10) UNSIGNED NULL DEFAULT NULL,
`name` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
and I want to make search also on name field.
That seems somewhat tricky. In my vote model I defined methods to sync data with elasticsearch:
<?php
namespace App;
use DB;
class Vote extends Model
{
protected $table = 'votes';
protected $elasticsearch_type = 'vote';
protected $primaryKey = 'id';
...
public function getElasticsearchType(): string
{
return $this->elasticsearch_type;
}
protected static function boot() {
parent::boot();
static::saved(function ($vote) {
$elastic = app(\App\Elastic\Elastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);
if ($vote->status == 'A') { // only active votes must be saved in elasticsearch
$elastic->index([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
'body' => [
'id' => $vote->id,
'slug' => $vote->slug,
'name' => $vote->name,
'description' => $vote->description,
]
]);
} // if ($vote->status == 'A') { // only active votes must be saved in elasticsearch
});
static::deleted(function ($vote ) {
$elastic = app(\App\Elastic\Elastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);
});
}
public static function bulkVotesToElastic()
{
$elastic = app(\App\Elastic\Elastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
Vote::chunk(100, function ($Votes) use ($elastic, $elasticsearch_root_index, $elasticsearch_type) {
foreach ($Votes as $nextVote) {
if ($nextVote->status!= 'A') continue; // only active votes must be saved in elasticsearch
$elastic->index([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $nextVote->id,
'body' => [
'id' => $nextVote->id,
'slug' => $nextVote->slug,
'name' => $nextVote->name,
'description' => $nextVote->description,
]
]);
}
});
}
}
I suppose that I could make similar methods for vote_items model and save all data with different type, but I think that is not valid way... Which way is valid? Are there some examples how this could be implemented ?
Thanks!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire