- Documentation >
- Search >
- Extend search >
- Manipulate Elasticsearch query
Manipulate Elasticsearch query
You can customize the search query before it's executed.
To do it, subscribe to Ibexa\Contracts\ElasticSearchEngine\Query\Event\QueryFilterEvent
.
The following example shows how to add an additional Search Criterion to all queries.
Depending on your configuration, this might impact all search queries, including those used for search and content tree in the back office.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 | <?php
declare(strict_types=1);
namespace App\EventSubscriber;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LogicalAnd;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ObjectStateIdentifier;
use Ibexa\Contracts\ElasticSearch\Query\Event\QueryFilterEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class CustomQueryFilterSubscriber implements EventSubscriberInterface
{
public function onQueryFilter(QueryFilterEvent $event): void
{
$query = $event->getQuery();
$additionalCriteria = new ObjectStateIdentifier('locked');
if ($query->filter !== null) {
$query->filter = $additionalCriteria;
} else {
// Append Criterion to existing filter
$query->filter = new LogicalAnd([
$query->filter,
$additionalCriteria
]);
}
}
public static function getSubscribedEvents(): array
{
return [
QueryFilterEvent::class => 'onQueryFilter'
];
}
}
|
Remember to register the subscriber as a service:
| services:
App\EventSubscriber\CustomQueryFilterSubscriber:
tags:
- { name: kernel.event_subscriber }
|