By mparker17 on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.0.x
Introduced in version:
8.0.0-alpha7
Issue links:
Description:
There is now an \Drupal\elasticsearch_connector\Event\AlterSearchQueryParamsEvent to modify the parameters generated by QueryParamBuilder just before they're sent to Elasticsearch.
Elasticsearch Connector's 8.x-7.x release series' \Drupal\Component\EventDispatcher\Event\BuildSearchParamsEvent runs at a similar time and performs similar functions.
For example...
namespace Drupal\YOUR_MODULE\EventSubscriber;
use Drupal\elasticsearch_connector\Event\AlterSearchQueryParamsEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SearchQueryEventSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents(): array {
return [
AlterSearchQueryParamsEvent::class => 'onSearch',
];
}
public function onSearch(AlterSearchQueryParamsEvent $event): void {
// Get the parameters we want to modify from the event.
$params = $event->getParams();
// Example: Turn off "track total hits" option.
$params['track_total_hits'] = FALSE;
// Example: Substitute one query for another on a particular view.
if ($event->getQuery()->getSearchId() === 'views_page:YOUR_VIEW__page_1'
&& isset($params['body']['query']['query_string']['query'])
&& $params['body']['query']['query_string']['query'] === 'One search query'
) {
$params['body']['query']['query_string']['query'] = 'A different search query';
}
// Re-set the parameters in the event to our modified version.
$event->setParams($params);
}
}
Impacts:
Module developers