platform/src/Elasticsearch/Product/CustomFieldUpdater.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Elasticsearch\Product;
  3. use Elasticsearch\Client;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  5. use Shopware\Core\System\CustomField\CustomFieldDefinition;
  6. use Shopware\Core\System\CustomField\CustomFieldTypes;
  7. use Shopware\Elasticsearch\Framework\ElasticsearchHelper;
  8. use Shopware\Elasticsearch\Framework\ElasticsearchOutdatedIndexDetector;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class CustomFieldUpdater implements EventSubscriberInterface
  11. {
  12.     private ElasticsearchOutdatedIndexDetector $indexDetector;
  13.     private Client $client;
  14.     private ElasticsearchHelper $elasticsearchHelper;
  15.     public function __construct(ElasticsearchOutdatedIndexDetector $indexDetectorClient $clientElasticsearchHelper $elasticsearchHelper)
  16.     {
  17.         $this->indexDetector $indexDetector;
  18.         $this->client $client;
  19.         $this->elasticsearchHelper $elasticsearchHelper;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             EntityWrittenContainerEvent::class => 'onNewCustomFieldCreated',
  25.         ];
  26.     }
  27.     public function onNewCustomFieldCreated(EntityWrittenContainerEvent $containerEvent): void
  28.     {
  29.         if (!$this->elasticsearchHelper->allowIndexing()) {
  30.             return;
  31.         }
  32.         $event $containerEvent->getEventByEntityName(CustomFieldDefinition::ENTITY_NAME);
  33.         if ($event === null) {
  34.             return;
  35.         }
  36.         $newCreatedFields = [];
  37.         foreach ($event->getWriteResults() as $writeResult) {
  38.             $existence $writeResult->getExistence();
  39.             if ($existence && $existence->exists()) {
  40.                 continue;
  41.             }
  42.             $esType self::getTypeFromCustomFieldType($writeResult->getProperty('type'));
  43.             if ($esType === null) {
  44.                 continue;
  45.             }
  46.             $newCreatedFields[$writeResult->getProperty('name')] = $esType;
  47.         }
  48.         if (\count($newCreatedFields) === 0) {
  49.             return;
  50.         }
  51.         $this->createNewFieldsInIndices($newCreatedFields);
  52.     }
  53.     public static function getTypeFromCustomFieldType(string $type): ?array
  54.     {
  55.         switch ($type) {
  56.             case CustomFieldTypes::INT:
  57.                 return [
  58.                     'type' => 'long',
  59.                 ];
  60.             case CustomFieldTypes::FLOAT:
  61.                 return [
  62.                     'type' => 'double',
  63.                 ];
  64.             case CustomFieldTypes::BOOL:
  65.                 return [
  66.                     'type' => 'boolean',
  67.                 ];
  68.             case CustomFieldTypes::DATETIME:
  69.                 return [
  70.                     'type' => 'date',
  71.                     'format' => 'yyyy-MM-dd HH:mm:ss.000',
  72.                     'ignore_malformed' => true,
  73.                 ];
  74.             case CustomFieldTypes::JSON:
  75.                 return [
  76.                     'type' => 'object',
  77.                     'dynamic' => true,
  78.                 ];
  79.             case CustomFieldTypes::COLORPICKER:
  80.             case CustomFieldTypes::ENTITY:
  81.             case CustomFieldTypes::HTML:
  82.             case CustomFieldTypes::MEDIA:
  83.             case CustomFieldTypes::SELECT:
  84.             case CustomFieldTypes::SWITCH:
  85.             case CustomFieldTypes::TEXT:
  86.             default:
  87.                 return [
  88.                     'type' => 'keyword',
  89.                 ];
  90.         }
  91.     }
  92.     private function createNewFieldsInIndices(array $newCreatedFields): void
  93.     {
  94.         $indices $this->indexDetector->getAllUsedIndices();
  95.         foreach ($indices as $indexName) {
  96.             $body = [
  97.                 'properties' => [
  98.                     'customFields' => [
  99.                         'properties' => $newCreatedFields,
  100.                     ],
  101.                 ],
  102.             ];
  103.             // For some reason, we need to include the includes to prevent merge conflicts.
  104.             // This error can happen for example after updating from version <6.4.
  105.             $current $this->client->indices()->get(['index' => $indexName]);
  106.             $includes $current[$indexName]['mappings']['_source']['includes'] ?? [];
  107.             if ($includes !== []) {
  108.                 $body['_source'] = [
  109.                     'includes' => $includes,
  110.                 ];
  111.             }
  112.             $this->client->indices()->putMapping([
  113.                 'index' => $indexName,
  114.                 'body' => $body,
  115.             ]);
  116.         }
  117.     }
  118. }