<?php
namespace App\ElasticSearch\Worker;
use App\ElasticSearch\ProductList\AppElasticSearch;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\ElasticSearch\DefaultElasticSearch7;
/**
* Use this for ES Version >= 6
*/
class AppElasticSearchWorker extends DefaultElasticSearch7
{
/**
* returns product list implementation valid and configured for this worker/tenant
*
* @return ProductListInterface
*/
public function getProductList()
{
return new AppElasticSearch($this->tenantConfig);
}
protected function doUpdateIndex($objectId, $data = null, $metadata = null)
{
//This method is overridden because of line 84 and 87
//Those lines had a param that was throwing errors
//Param was '_routing' and it was changed for 'routing'.
//Underscore was not needed
// $isLocked = $this->checkIndexLock(false);
//
// if ($isLocked) {
// return;
// }
if (empty($data)) {
$dataEntry = $this->db->fetchRow('SELECT data, metadata FROM ' . $this->getStoreTableName() . ' WHERE o_id = ? AND tenant = ?', [$objectId, $this->name]);
if ($dataEntry) {
$data = json_decode($dataEntry['data'], true);
$metadata = $dataEntry['metadata'];
$jsonDecodeError = json_last_error();
if ($jsonDecodeError !== JSON_ERROR_NONE) {
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}.");
}
}
}
if ($data) {
$systemAttributeKeys = $this->getSystemAttributes(true);
$indexSystemData = [];
$indexAttributeData = [];
$indexRelationData = [];
$data = $this->doPreIndexDataModification($data);
//add system and index attributes
foreach ($data['data'] as $dataKey => $dataEntry) {
if (array_key_exists($dataKey, $systemAttributeKeys)) {
//add this key to system attributes
$indexSystemData[$dataKey] = $dataEntry;
} else {
//add this key to custom attributes
$indexAttributeData[$dataKey] = $dataEntry;
}
}
//fix categories to array
$indexSystemData['categoryIds'] = array_values(array_filter(explode(',', $indexSystemData['categoryIds'])));
$indexSystemData['parentCategoryIds'] = array_values(array_filter(explode(',', $indexSystemData['parentCategoryIds'])));
//add relation attributes
foreach ($data['relations'] as $relation) {
$indexRelationData[$relation['fieldname']][] = $relation['dest'];
}
//check if parent should exist and if so, consider parent relation at indexing
$routingId = $indexSystemData['o_type'] == ProductListInterface::PRODUCT_TYPE_VARIANT ? $indexSystemData['o_virtualProductId'] : $indexSystemData['o_id'];
if ($metadata !== null && $routingId != $metadata) {
//routing has changed, need to delete old ES entry
$this->bulkIndexData[] = ['delete' => ['_index' => $this->getIndexNameVersion(), '_type' => $this->getTenantConfig()->getElasticSearchClientParams()['indexType'], '_id' => $objectId, 'routing' => $metadata]];
}
$this->bulkIndexData[] = ['index' => ['_index' => $this->getIndexNameVersion(), '_type' => $this->getTenantConfig()->getElasticSearchClientParams()['indexType'], '_id' => $objectId, 'routing' => $routingId]];
$bulkIndexData = array_filter(['system' => array_filter($indexSystemData), 'type' => $indexSystemData['o_type'], 'attributes' => array_filter($indexAttributeData, function ($value) {
return $value !== null;
}), 'relations' => $indexRelationData, 'subtenants' => $data['subtenants']]);
if ($indexSystemData['o_type'] == ProductListInterface::PRODUCT_TYPE_VARIANT) {
$bulkIndexData[self::RELATION_FIELD] = ['name' => $indexSystemData['o_type'], 'parent' => $indexSystemData['o_virtualProductId']];
} else {
$bulkIndexData[self::RELATION_FIELD] = ['name' => $indexSystemData['o_type']];
}
$this->bulkIndexData[] = $bulkIndexData;
$this->indexStoreMetaData[$objectId] = $routingId;
}
}
}