src/ElasticSearch/Worker/AppElasticSearchWorker.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\ElasticSearch\Worker;
  3. use App\ElasticSearch\ProductList\AppElasticSearch;
  4. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
  5. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\ElasticSearch\DefaultElasticSearch7;
  6. /**
  7.  *  Use this for ES Version >= 6
  8.  */
  9. class AppElasticSearchWorker extends DefaultElasticSearch7
  10. {
  11.     /**
  12.      * returns product list implementation valid and configured for this worker/tenant
  13.      *
  14.      * @return ProductListInterface
  15.      */
  16.     public function getProductList()
  17.     {
  18.         return new AppElasticSearch($this->tenantConfig);
  19.     }
  20.     protected function doUpdateIndex($objectId$data null$metadata null)
  21.     {
  22.         //This method is overridden because of line 84 and 87
  23.         //Those lines had a param that was throwing errors
  24.         //Param was '_routing' and it was changed for 'routing'.
  25.         //Underscore was not needed
  26. //        $isLocked = $this->checkIndexLock(false);
  27. //
  28. //        if ($isLocked) {
  29. //            return;
  30. //        }
  31.         if (empty($data)) {
  32.             $dataEntry $this->db->fetchRow('SELECT data, metadata FROM ' $this->getStoreTableName() . ' WHERE o_id = ? AND tenant = ?', [$objectId$this->name]);
  33.             if ($dataEntry) {
  34.                 $data json_decode($dataEntry['data'], true);
  35.                 $metadata $dataEntry['metadata'];
  36.                 $jsonDecodeError json_last_error();
  37.                 if ($jsonDecodeError !== JSON_ERROR_NONE) {
  38.                     throw new \Exception("Could not decode store data for updating index - maybe there is invalid json data. Json decode error code was {$jsonDecodeError}, ObjectId was {$objectId}.");
  39.                 }
  40.             }
  41.         }
  42.         if ($data) {
  43.             $systemAttributeKeys $this->getSystemAttributes(true);
  44.             $indexSystemData = [];
  45.             $indexAttributeData = [];
  46.             $indexRelationData = [];
  47.             $data $this->doPreIndexDataModification($data);
  48.             //add system and index attributes
  49.             foreach ($data['data'] as $dataKey => $dataEntry) {
  50.                 if (array_key_exists($dataKey$systemAttributeKeys)) {
  51.                     //add this key to system attributes
  52.                     $indexSystemData[$dataKey] = $dataEntry;
  53.                 } else {
  54.                     //add this key to custom attributes
  55.                     $indexAttributeData[$dataKey] = $dataEntry;
  56.                 }
  57.             }
  58.             //fix categories to array
  59.             $indexSystemData['categoryIds'] = array_values(array_filter(explode(','$indexSystemData['categoryIds'])));
  60.             $indexSystemData['parentCategoryIds'] = array_values(array_filter(explode(','$indexSystemData['parentCategoryIds'])));
  61.             //add relation attributes
  62.             foreach ($data['relations'] as $relation) {
  63.                 $indexRelationData[$relation['fieldname']][] = $relation['dest'];
  64.             }
  65.             //check if parent should exist and if so, consider parent relation at indexing
  66.             $routingId $indexSystemData['o_type'] == ProductListInterface::PRODUCT_TYPE_VARIANT $indexSystemData['o_virtualProductId'] : $indexSystemData['o_id'];
  67.             if ($metadata !== null && $routingId != $metadata) {
  68.                 //routing has changed, need to delete old ES entry
  69.                 $this->bulkIndexData[] = ['delete' => ['_index' => $this->getIndexNameVersion(), '_type' => $this->getTenantConfig()->getElasticSearchClientParams()['indexType'], '_id' => $objectId'routing' => $metadata]];
  70.             }
  71.             $this->bulkIndexData[] = ['index' => ['_index' => $this->getIndexNameVersion(), '_type' => $this->getTenantConfig()->getElasticSearchClientParams()['indexType'], '_id' => $objectId'routing' => $routingId]];
  72.             $bulkIndexData array_filter(['system' => array_filter($indexSystemData), 'type' => $indexSystemData['o_type'], 'attributes' => array_filter($indexAttributeData, function ($value) {
  73.                 return $value !== null;
  74.             }), 'relations' => $indexRelationData'subtenants' => $data['subtenants']]);
  75.             if ($indexSystemData['o_type'] == ProductListInterface::PRODUCT_TYPE_VARIANT) {
  76.                 $bulkIndexData[self::RELATION_FIELD] = ['name' => $indexSystemData['o_type'], 'parent' => $indexSystemData['o_virtualProductId']];
  77.             } else {
  78.                 $bulkIndexData[self::RELATION_FIELD] = ['name' => $indexSystemData['o_type']];
  79.             }
  80.             $this->bulkIndexData[] = $bulkIndexData;
  81.             $this->indexStoreMetaData[$objectId] = $routingId;
  82.         }
  83.     }
  84. }