<?php
declare(strict_types=1);
namespace App\Platform\EventListener;
use Elastica\Index\Settings;
use FOS\ElasticaBundle\Event\PostIndexPopulateEvent;
use FOS\ElasticaBundle\Event\PreIndexPopulateEvent;
use FOS\ElasticaBundle\Index\IndexManager;
class PopulateEventListener
{
public function __construct(
private readonly IndexManager $indexManager,
) {
}
public function preIndexPopulate(PreIndexPopulateEvent $event): void
{
$index = $this->indexManager->getIndex($event->getIndex());
$settings = $index->getSettings();
$settings->setRefreshInterval('-1');
}
public function postIndexPopulate(PostIndexPopulateEvent $event): void
{
$index = $this->indexManager->getIndex($event->getIndex());
$index->getClient()->request('_forcemerge?max_num_segments=5', 'POST');
$index->getSettings()->setRefreshInterval(Settings::DEFAULT_REFRESH_INTERVAL);
}
}