Skip to content

Commit

Permalink
Merge pull request #2384 from plentymarkets/fix/performance_external_…
Browse files Browse the repository at this point in the history
…search

execute only one search and not for every item
  • Loading branch information
stentrop authored Sep 28, 2020
2 parents db47a24 + 636fb8d commit ad9de7a
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions src/Contexts/ItemListContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait ItemListContext
public $query;
public $suggestionString;

public $itemList;
public $itemList = [];
public $facets;

/** @var SearchOptions */
Expand All @@ -42,36 +42,41 @@ protected function initItemList($defaultSearchFactories, $options, $scope = Sear
if (ExternalSearch::hasExternalSearch()) {
/** @var ExternalSearch $externalSearch */
$externalSearch = pluginApp(ExternalSearch::class);
$externalSearch->page = $this->currentPage;
$externalSearch->page = $this->currentPage;
$externalSearch->itemsPerPage = $this->itemsPerPage;
$externalSearch->searchString = $options['query'];
$externalSearch->categoryId = $options['categoryId'];
$externalSearch->sorting = $this->itemSorting;
$externalSearch->categoryId = $options['categoryId'];
$externalSearch->sorting = $this->itemSorting;

// emit event to perform external search
ExternalSearch::getExternalResults($externalSearch);

if ($externalSearch->hasResults()) {
$resultVariationIds = $externalSearch->getResults();
$externalSearchFactories = [];
foreach ($resultVariationIds as $variationId) {
$externalSearchFactories[$variationId] = VariationList::getSearchFactory(
[
'variationIds' => [$variationId],
'excludeFromCache' => $scope === SearchOptions::SCOPE_SEARCH
]
);
}

$searchResults = $itemSearchService->getResults($externalSearchFactories);
$variationIds = $externalSearch->getResults();

foreach ($resultVariationIds as $variationId) {
if (isset($searchResults[$variationId]['documents']) && count($searchResults[$variationId]['documents'])) {
$this->itemList[] = $searchResults[$variationId]['documents'][0];
$externalSearchFactory = VariationList::getSearchFactory(
[
'variationIds' => $variationIds,
'excludeFromCache' => $scope === SearchOptions::SCOPE_SEARCH
]
);
$searchResults = $itemSearchService->getResults($externalSearchFactory);
if (isset($searchResults['documents']) && count(
$searchResults['documents']
)) {

foreach ($variationIds as $variationId) {
$variation = array_filter($searchResults['documents'], function($document) use ($variationId) {
return $document['id'] == $variationId;
});

if(count($variation) == 1) {
$this->itemList[] = array_pop($variation);
}
}
}
$this->pageMax = ceil($externalSearch->getCountTotal() / $options['itemsPerPage']);
$this->itemCountPage = count($resultVariationIds);
$this->itemCountPage = count($variationIds);
$this->itemCountTotal = $externalSearch->getCountTotal();
$this->facets = [];

Expand All @@ -82,7 +87,7 @@ protected function initItemList($defaultSearchFactories, $options, $scope = Sear
$searchResults = $itemSearchService->getResults($defaultSearchFactories);

//try to get result for the "did you mean?" search if there is no result for the original search string
if($scope === SearchOptions::SCOPE_SEARCH && (int)$searchResults['itemList']['total'] === 0) {
if ($scope === SearchOptions::SCOPE_SEARCH && (int)$searchResults['itemList']['total'] === 0) {
$originalSearchString = $options['query'];
/** @var ItemSearchAutocompleteService $itemSearchAutocompleteService */
$itemSearchAutocompleteService = pluginApp(ItemSearchAutocompleteService::class);
Expand All @@ -96,7 +101,7 @@ protected function initItemList($defaultSearchFactories, $options, $scope = Sear
$searchResults = $itemSearchService->getResults(
[
'itemList' => SearchItems::getSearchFactory($options),
'facets' => Facets::getSearchFactory($options)
'facets' => Facets::getSearchFactory($options)
]
);
}
Expand All @@ -105,9 +110,9 @@ protected function initItemList($defaultSearchFactories, $options, $scope = Sear
$this->itemCountTotal = $searchResults['itemList']['total'];
$this->itemCountTotal = $this->itemCountTotal > 10000 ? 10000 : $this->itemCountTotal;

$this->pageMax = ceil($this->itemCountTotal / $options['itemsPerPage']);
$this->pageMax = ceil($this->itemCountTotal / $options['itemsPerPage']);
$this->itemCountPage = count($searchResults['itemList']['documents']);
$this->itemList = $searchResults['itemList']['documents'];
$this->facets = $searchResults['facets'];
$this->itemList = $searchResults['itemList']['documents'];
$this->facets = $searchResults['facets'];
}
}

0 comments on commit ad9de7a

Please sign in to comment.