Skip to content

Commit

Permalink
Merge pull request Smile-SA#73 from Elastic-Suite/feat-ESP-236-Best-p…
Browse files Browse the repository at this point in the history
…erforming-children

 #ESP-236 fix  get best performing children feature
  • Loading branch information
romainruaud authored Sep 22, 2021
2 parents 12a2eaa + 6b54d12 commit a44830e
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Smile\ElasticsuiteCore\Search\Request\Builder;
use Smile\ElasticsuiteCore\Search\RequestInterface;
use Smile\ElasticsuiteConfigurableProduct\Helper\Data as EsCatalogHelper;
use Psr\Log\LoggerInterface;

/**
* Class BestPerformingChildren.
Expand Down Expand Up @@ -48,21 +49,31 @@ class BestPerformingChildrenProvider
*/
protected $esCatalogHelper;

/**
* Logger
*
* @var LoggerInterface
*/
protected $logger;

/**
* Constructor.
*
* @param Builder $searchRequestBuilder Search request builder.
* @param SearchEngineInterface $searchEngine Search engine.
* @param EsCatalogHelper $esCatalogHelper Elastisuite catalog Helper.
* @param LoggerInterface $logger Logger.
*/
public function __construct(
Builder $searchRequestBuilder,
SearchEngineInterface $searchEngine,
EsCatalogHelper $esCatalogHelper
EsCatalogHelper $esCatalogHelper,
LoggerInterface $logger
) {
$this->searchRequestBuilder = $searchRequestBuilder;
$this->searchEngine = $searchEngine;
$this->esCatalogHelper = $esCatalogHelper;
$this->logger = $logger;
}

/**
Expand All @@ -80,9 +91,15 @@ public function getBestPerformingChildren(int $storeId, array $configurableIds):
return $bestPerformingChildren;
}

$searchRequest = $this->getSearchRequest($storeId, $configurableIds);
$searchResponse = $this->searchEngine->search($searchRequest);
if ($searchResponse->getAggregations()->getBucket('configurables') !== null) {
try {
$searchRequest = $this->getSearchRequest($storeId, $configurableIds);
$searchResponse = $this->searchEngine->search($searchRequest);
} catch (\Exception $e) {
$this->logger->critical('Error on best performing children query', ['exception' => $e]);
$searchResponse = null;
}

if ($searchResponse !== null && $searchResponse->getAggregations()->getBucket('configurables') !== null) {
foreach ($searchResponse->getAggregations()->getBucket('configurables')->getValues() as $configurable) {
$metrics = $configurable->getMetrics();
if (isset($metrics['max_qty_sku_keys']) && is_array($metrics['max_qty_sku_keys'])) {
Expand Down

0 comments on commit a44830e

Please sign in to comment.