2022-03-23 16:04:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
|
|
use App\Models\Media;
|
2022-04-20 15:23:31 +02:00
|
|
|
use Illuminate\Console\Command;
|
2022-03-23 16:04:24 +01:00
|
|
|
|
|
|
|
|
class ReindexElasticMedia extends Command
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The name and signature of the console command.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2022-06-20 14:57:58 +02:00
|
|
|
protected $signature = 'media:reindex';
|
2022-03-23 16:04:24 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The console command description.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $description = 'Reindex la base médias dans Elasticsearch';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the console command.
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
$this->info('Reindexation de la base médias dans Elasticsearch...');
|
2022-06-20 14:57:58 +02:00
|
|
|
|
|
|
|
|
$this->info('ES: Test présence index...');
|
2022-03-23 16:04:24 +01:00
|
|
|
if (Media::indexExists()) {
|
2022-06-20 14:57:58 +02:00
|
|
|
$this->info('ES: Suppression index...');
|
2022-03-23 16:04:24 +01:00
|
|
|
Media::deleteIndex();
|
|
|
|
|
}
|
2022-06-20 14:57:58 +02:00
|
|
|
|
|
|
|
|
$this->info('ES: Création index...');
|
2022-03-23 16:04:24 +01:00
|
|
|
Media::createIndex();
|
|
|
|
|
|
|
|
|
|
$pageNumber = 0;
|
|
|
|
|
$perPage = 200;
|
|
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \Illuminate\Pagination\LengthAwarePaginator $mediaCollection */
|
|
|
|
|
$mediaCollection = Media::paginate($perPage);
|
|
|
|
|
|
|
|
|
|
if (!$mediaCollection->total()) {
|
|
|
|
|
$this->info('Pas de medias');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bar = $this->output->createProgressBar(count($mediaCollection));
|
2022-03-23 16:04:24 +01:00
|
|
|
|
2022-06-20 14:57:58 +02:00
|
|
|
$bar->start($mediaCollection->total());
|
2022-03-23 16:04:24 +01:00
|
|
|
|
|
|
|
|
// * LOOP
|
|
|
|
|
do {
|
|
|
|
|
if ($pageNumber > 0) {
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \Illuminate\Pagination\LengthAwarePaginator $mediaCollection */
|
|
|
|
|
$mediaCollection = Media::paginate($perPage, ['*'], 'page', $pageNumber);
|
2022-03-23 16:04:24 +01:00
|
|
|
}
|
2022-06-20 14:57:58 +02:00
|
|
|
/** @var \App\Models\Media $media */
|
|
|
|
|
foreach ($mediaCollection->getCollection() as $media) {
|
|
|
|
|
/** @throws \RuntimeException */
|
|
|
|
|
$media->addToElasticSearch();
|
2022-03-23 16:04:24 +01:00
|
|
|
$bar->advance();
|
|
|
|
|
}
|
|
|
|
|
++$pageNumber;
|
2022-06-20 14:57:58 +02:00
|
|
|
} while ($mediaCollection->hasMorePages());
|
2022-03-23 16:04:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
$bar->finish();
|
2022-06-20 14:57:58 +02:00
|
|
|
$this->newLine();
|
2022-03-23 16:04:24 +01:00
|
|
|
$this->info('Fin du traitement');
|
|
|
|
|
}
|
|
|
|
|
}
|