Skip to content

Commit ad59fb7

Browse files
committed
Move Elasticsearch subscribers to code_samples/ PHP files
1 parent 3f7c13c commit ad59fb7

File tree

4 files changed

+85
-79
lines changed

4 files changed

+85
-79
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\EventSubscriber;
6+
7+
use Ibexa\Contracts\Core\Search\Field;
8+
use Ibexa\Contracts\Core\Search\FieldType\StringField;
9+
use Ibexa\Contracts\Elasticsearch\Mapping\Event\ContentIndexCreateEvent;
10+
use Ibexa\Contracts\Elasticsearch\Mapping\Event\LocationIndexCreateEvent;
11+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12+
13+
final class CustomIndexDataSubscriber implements EventSubscriberInterface
14+
{
15+
public function onContentDocumentCreate(ContentIndexCreateEvent $event): void
16+
{
17+
$document = $event->getDocument();
18+
$document->fields[] = new Field(
19+
'custom_field',
20+
'Custom field value',
21+
new StringField()
22+
);
23+
}
24+
25+
public function onLocationDocumentCreate(LocationIndexCreateEvent $event): void
26+
{
27+
$document = $event->getDocument();
28+
$document->fields[] = new Field(
29+
'custom_field',
30+
'Custom field value',
31+
new StringField()
32+
);
33+
}
34+
35+
public static function getSubscribedEvents(): array
36+
{
37+
return [
38+
ContentIndexCreateEvent::class => 'onContentDocumentCreate',
39+
LocationIndexCreateEvent::class => 'onLocationDocumentCreate'
40+
];
41+
}
42+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\EventSubscriber;
6+
7+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LogicalAnd;
8+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ObjectStateIdentifier;
9+
use Ibexa\Contracts\ElasticSearch\Query\Event\QueryFilterEvent;
10+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11+
12+
final class CustomQueryFilterSubscriber implements EventSubscriberInterface
13+
{
14+
public function onQueryFilter(QueryFilterEvent $event): void
15+
{
16+
$query = $event->getQuery();
17+
18+
$additionalCriteria = new ObjectStateIdentifier('locked');
19+
20+
if ($query->filter !== null) {
21+
$query->filter = $additionalCriteria;
22+
} else {
23+
// Append Criterion to existing filter
24+
$query->filter = new LogicalAnd([
25+
$query->filter,
26+
$additionalCriteria
27+
]);
28+
}
29+
}
30+
31+
public static function getSubscribedEvents(): array
32+
{
33+
return [
34+
QueryFilterEvent::class => 'onQueryFilter'
35+
];
36+
}
37+
}

docs/search/extensibility/index_custom_elasticsearch_data.md

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,9 @@ You can pass the event to a subscriber which gives you access to the document th
1919
In the following example, when an index in created for a content or a location document, the event subscriber adds a `custom_field` of the type [`StringField`](../../api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Search-FieldType-StringField.html) to the index:
2020

2121
``` php hl_lines="19 20 21"
22-
<?php
23-
24-
declare(strict_types=1);
25-
26-
namespace App\EventSubscriber;
27-
28-
use Ibexa\Contracts\Core\Search\Field;
29-
use Ibexa\Contracts\Core\Search\FieldType\StringField;
30-
use Ibexa\Contracts\Elasticsearch\Mapping\Event\ContentIndexCreateEvent;
31-
use Ibexa\Contracts\Elasticsearch\Mapping\Event\LocationIndexCreateEvent;
32-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
33-
34-
final class CustomIndexDataSubscriber implements EventSubscriberInterface
35-
{
36-
public function onContentDocumentCreate(ContentIndexCreateEvent $event): void
37-
{
38-
$document = $event->getDocument();
39-
$document->fields[] = new Field(
40-
'custom_field',
41-
'Custom field value',
42-
new StringField()
43-
);
44-
}
45-
46-
public function onLocationDocumentCreate(LocationIndexCreateEvent $event): void
47-
{
48-
$document = $event->getDocument();
49-
$document->fields[] = new Field(
50-
'custom_field',
51-
'Custom field value',
52-
new StringField()
53-
);
54-
}
55-
56-
public static function getSubscribedEvents(): array
57-
{
58-
return [
59-
ContentIndexCreateEvent::class => 'onContentDocumentCreate',
60-
LocationIndexCreateEvent::class => 'onLocationDocumentCreate'
61-
];
62-
}
63-
}
22+
--8<--
23+
code_samples/search/custom/src/EventSubscriber/CustomIndexDataSubscriber.php
24+
--8<--
6425
```
6526

6627
If your subscriber is not automatically properly configured, register it as a service:

docs/search/extensibility/manipulate_elasticsearch_query.md

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,9 @@ The following example shows how to add a Search Criterion to all queries.
1212
Depending on your configuration, this might impact all search queries, including those used for search and content tree in the back office.
1313

1414
``` php hl_lines="34"
15-
<?php
16-
17-
declare(strict_types=1);
18-
19-
namespace App\EventSubscriber;
20-
21-
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LogicalAnd;
22-
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ObjectStateIdentifier;
23-
use Ibexa\Contracts\ElasticSearch\Query\Event\QueryFilterEvent;
24-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
25-
26-
final class CustomQueryFilterSubscriber implements EventSubscriberInterface
27-
{
28-
public function onQueryFilter(QueryFilterEvent $event): void
29-
{
30-
$query = $event->getQuery();
31-
32-
$additionalCriteria = new ObjectStateIdentifier('locked');
33-
34-
if ($query->filter !== null) {
35-
$query->filter = $additionalCriteria;
36-
} else {
37-
// Append Criterion to existing filter
38-
$query->filter = new LogicalAnd([
39-
$query->filter,
40-
$additionalCriteria
41-
]);
42-
}
43-
}
44-
45-
public static function getSubscribedEvents(): array
46-
{
47-
return [
48-
QueryFilterEvent::class => 'onQueryFilter'
49-
];
50-
}
51-
}
15+
--8<--
16+
code_samples/search/custom/src/EventSubscriber/CustomQueryFilterSubscriber.php
17+
--8<--
5218
```
5319

5420
If your subscriber is not automatically properly configured, register it as a service:

0 commit comments

Comments
 (0)