<?php declare(strict_types=1);
namespace Swag\EnterpriseSearch\Boosting;
use Shopware\Elasticsearch\Framework\DataAbstractionLayer\Event\ElasticsearchEntitySearcherSearchEvent;
use Swag\EnterpriseSearch\Common\SesExtension;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ElasticsearchEntitySearcherSubscriber implements EventSubscriberInterface
{
/**
* @var BoostingQueryBuilder
*/
private $boostingQueryBuilder;
public function __construct(
BoostingQueryBuilder $boostingQueryBuilder
) {
$this->boostingQueryBuilder = $boostingQueryBuilder;
}
public static function getSubscribedEvents(): array
{
return [
ElasticsearchEntitySearcherSearchEvent::class => 'enhanceSearch',
];
}
public function enhanceSearch(ElasticsearchEntitySearcherSearchEvent $event): void
{
$search = $event->getSearch();
$definition = $event->getDefinition();
$context = $event->getContext();
$criteria = $event->getCriteria();
$this->boostingQueryBuilder->addBoostingQuery($definition, $criteria, $search, $context);
if (SesExtension::isExplanation($context)) {
$search->setExplain(true);
}
}
}