vendor/shopware/elasticsearch/Product/CustomFieldUpdater.php line 38

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