|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Elasticsearch\Product; |
|
4
|
|
|
|
|
5
|
|
|
use OpenSearch\Client; |
|
6
|
|
|
use Shopware\Core\Content\Product\ProductDefinition; |
|
7
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult; |
|
8
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent; |
|
9
|
|
|
use Shopware\Core\Framework\Feature; |
|
10
|
|
|
use Shopware\Core\Framework\Log\Package; |
|
11
|
|
|
use Shopware\Elasticsearch\Framework\ElasticsearchHelper; |
|
12
|
|
|
use Shopware\Elasticsearch\Framework\ElasticsearchRegistry; |
|
13
|
|
|
use Shopware\Elasticsearch\Framework\Indexing\ElasticsearchLanguageIndexIteratorMessage; |
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
15
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @internal |
|
19
|
|
|
* When an language is created, we need to trigger an indexing for that |
|
20
|
|
|
*/ |
|
21
|
|
|
#[Package('core')] |
|
22
|
|
|
class LanguageSubscriber implements EventSubscriberInterface |
|
23
|
|
|
{ |
|
24
|
|
|
public function __construct( |
|
25
|
|
|
private readonly ElasticsearchHelper $elasticsearchHelper, |
|
26
|
|
|
private readonly ElasticsearchRegistry $registry, |
|
27
|
|
|
private readonly Client $client, |
|
28
|
|
|
private readonly ProductDefinition $productDefinition, |
|
29
|
|
|
private readonly MessageBusInterface $bus |
|
30
|
|
|
) { |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function getSubscribedEvents(): array |
|
34
|
|
|
{ |
|
35
|
|
|
if (Feature::isActive('ES_MULTILINGUAL_INDEX')) { |
|
36
|
|
|
return [ |
|
37
|
|
|
'language.written' => 'onLanguageWritten', |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return [ |
|
42
|
|
|
'sales_channel_language.written' => 'onSalesChannelWritten', |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @deprecated tag:v6.6.0 - reason:remove-subscriber - method will be removed |
|
48
|
|
|
*/ |
|
49
|
|
|
public function onSalesChannelWritten(EntityWrittenEvent $event): void |
|
50
|
|
|
{ |
|
51
|
|
|
if (Feature::isActive('ES_MULTILINGUAL_INDEX')) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (!$this->elasticsearchHelper->allowIndexing()) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
foreach ($event->getWriteResults() as $writeResult) { |
|
60
|
|
|
if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_INSERT) { |
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$languageId = $writeResult->getProperty('languageId'); |
|
65
|
|
|
|
|
66
|
|
|
$esIndex = $this->elasticsearchHelper->getIndexName($this->productDefinition, $languageId); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
// index exists, don't need to do anything |
|
69
|
|
|
if ($this->client->indices()->exists(['index' => $esIndex])) { |
|
70
|
|
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->bus->dispatch(new ElasticsearchLanguageIndexIteratorMessage($languageId)); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function onLanguageWritten(EntityWrittenEvent $event): void |
|
78
|
|
|
{ |
|
79
|
|
|
if (!$this->elasticsearchHelper->allowIndexing()) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$context = $event->getContext(); |
|
84
|
|
|
|
|
85
|
|
|
foreach ($event->getWriteResults() as $writeResult) { |
|
86
|
|
|
if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_INSERT) { |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
foreach ($this->registry->getDefinitions() as $definition) { |
|
91
|
|
|
$indexName = $this->elasticsearchHelper->getIndexName($definition->getEntityDefinition()); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
// index doesn't exist, don't need to do anything |
|
94
|
|
|
if (!$this->client->indices()->exists(['index' => $indexName])) { |
|
95
|
|
|
continue; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->client->indices()->putMapping([ |
|
99
|
|
|
'index' => $indexName, |
|
100
|
|
|
'body' => [ |
|
101
|
|
|
'properties' => $definition->getMapping($context)['properties'], |
|
102
|
|
|
], |
|
103
|
|
|
]); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.