Skip to content

Commit

Permalink
Refactor categories to be indexed by batches (#696)
Browse files Browse the repository at this point in the history
* Refactor categories to be indexed by batches
* Refactor "booleans" to Exceptions
* Refactor Category indexer to be more readable
  • Loading branch information
Jan Petr authored Feb 28, 2019
1 parent 706ab9d commit f443e40
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 205 deletions.
8 changes: 8 additions & 0 deletions Exception/CategoryEmptyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Algolia\AlgoliaSearch\Exception;

class CategoryEmptyException extends CategoryReindexingException
{
//
}
8 changes: 8 additions & 0 deletions Exception/CategoryNotActiveException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Algolia\AlgoliaSearch\Exception;

class CategoryNotActiveException extends CategoryReindexingException
{
//
}
8 changes: 8 additions & 0 deletions Exception/CategoryNotIncludedInMenuException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Algolia\AlgoliaSearch\Exception;

class CategoryNotIncludedInMenuException extends CategoryReindexingException
{
//
}
8 changes: 8 additions & 0 deletions Exception/CategoryReindexingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Algolia\AlgoliaSearch\Exception;

abstract class CategoryReindexingException extends \RuntimeException
{
//
}
106 changes: 89 additions & 17 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Algolia\AlgoliaSearch\Helper;

use Algolia\AlgoliaSearch\Exception\CategoryReindexingException;
use Algolia\AlgoliaSearch\Exception\ProductReindexingException;
use Algolia\AlgoliaSearch\Helper\Entity\AdditionalSectionHelper;
use Algolia\AlgoliaSearch\Helper\Entity\CategoryHelper;
Expand Down Expand Up @@ -203,7 +204,8 @@ public function rebuildStoreCategoryIndex($storeId, $categoryIds = null)
$storeId,
$collection,
$page,
$this->configHelper->getNumberOfElementByPage()
$this->configHelper->getNumberOfElementByPage(),
$categoryIds
);

$page++;
Expand Down Expand Up @@ -331,6 +333,16 @@ public function rebuildProductIndex($storeId, $productIds, $page, $pageSize, $us
$this->rebuildStoreProductIndexPage($storeId, $collection, $page, $pageSize, null, $productIds, $useTmpIndex);
}

public function rebuildCategoryIndex($storeId, $page, $pageSize)
{
if ($this->isIndexingEnabled($storeId) === false) {
return;
}

$collection = $this->categoryHelper->getCategoryCollectionQuery($storeId, null);
$this->rebuildStoreCategoryIndexPage($storeId, $collection, $page, $pageSize);
}

public function rebuildStoreSuggestionIndexPage($storeId, $collectionDefault, $page, $pageSize)
{
if ($this->isIndexingEnabled($storeId) === false) {
Expand Down Expand Up @@ -369,7 +381,7 @@ public function rebuildStoreSuggestionIndexPage($storeId, $collectionDefault, $p
unset($collection);
}

public function rebuildStoreCategoryIndexPage($storeId, $collectionDefault, $page, $pageSize)
public function rebuildStoreCategoryIndexPage($storeId, $collectionDefault, $page, $pageSize, $categoryIds = null)
{
if ($this->isIndexingEnabled($storeId) === false) {
return;
Expand All @@ -382,26 +394,28 @@ public function rebuildStoreCategoryIndexPage($storeId, $collectionDefault, $pag

$indexName = $this->getIndexName($this->categoryHelper->getIndexNameSuffix(), $storeId);

$indexData = [];
$indexData = $this->getCategoryRecords($storeId, $collection, $categoryIds);

/** @var Category $category */
foreach ($collection as $category) {
if (!$this->categoryHelper->isCategoryActive($category->getId(), $storeId)) {
continue;
}

$category->setStoreId($storeId);
if ($indexData['toIndex'] && $indexData['toIndex'] !== []) {
$this->logger->start('ADD/UPDATE TO ALGOLIA');

$categoryObject = $this->categoryHelper->getObject($category);
$this->algoliaHelper->addObjects($indexData['toIndex'], $indexName);

if ($this->configHelper->shouldIndexEmptyCategories($storeId) === true
|| $categoryObject['product_count'] > 0) {
array_push($indexData, $categoryObject);
}
$this->logger->log('Product IDs: ' . implode(', ', array_keys($indexData['toIndex'])));
$this->logger->stop('ADD/UPDATE TO ALGOLIA');
}

if (count($indexData) > 0) {
$this->algoliaHelper->addObjects($indexData, $indexName);
if ($indexData['toRemove'] && $indexData['toRemove'] !== []) {
$toRealRemove = $this->getIdsToRealRemove($indexName, $indexData['toRemove']);

if ($toRealRemove && $toRealRemove !== []) {
$this->logger->start('REMOVE FROM ALGOLIA');

$this->algoliaHelper->deleteObjects($toRealRemove, $indexName);

$this->logger->log('Category IDs: ' . implode(', ', $toRealRemove));
$this->logger->stop('REMOVE FROM ALGOLIA');
}
}

unset($indexData);
Expand Down Expand Up @@ -474,6 +488,64 @@ private function getProductsRecords($storeId, $collection, $potentiallyDeletedPr
];
}

/**
* @param int $storeId
* @param \Magento\Catalog\Model\ResourceModel\Category\Collection $collection
* @param array|null $potentiallyDeletedCategoriesIds
*
* @throws \Magento\Framework\Exception\NoSuchEntityException
*
* @return array
*/
private function getCategoryRecords($storeId, $collection, $potentiallyDeletedCategoriesIds = null)
{
$categoriesToIndex = [];
$categoriesToRemove = [];

// In $potentiallyDeletedCategoriesIds there might be IDs of deleted products which will not be in a collection
if (is_array($potentiallyDeletedCategoriesIds)) {
$potentiallyDeletedCategoriesIds = array_combine(
$potentiallyDeletedCategoriesIds,
$potentiallyDeletedCategoriesIds
);
}

/** @var Category $category */
foreach ($collection as $category) {
$category->setStoreId($storeId);

$categoryId = $category->getId();

// If $categoryId is in the collection, remove it from $potentiallyDeletedProductsIds
// so it's not removed without check
if (isset($potentiallyDeletedCategoriesIds[$categoryId])) {
unset($potentiallyDeletedCategoriesIds[$categoryId]);
}

if (isset($categorysToIndex[$categoryId]) || isset($categorysToRemove[$categoryId])) {
continue;
}

try {
$this->categoryHelper->canCategoryBeReindexed($category, $storeId);
} catch (CategoryReindexingException $e) {
$categoriesToRemove[$categoryId] = $categoryId;
continue;
}

$categoriesToIndex[$categoryId] = $this->categoryHelper->getObject($category);
}

if (is_array($potentiallyDeletedCategoriesIds)) {
$categoriesToRemove = array_merge($categoriesToRemove, $potentiallyDeletedCategoriesIds);
}

return [
'toIndex' => $categoriesToIndex,
'toRemove' => array_unique($categoriesToRemove),
];
}

public function rebuildStoreProductIndexPage(
$storeId,
$collectionDefault,
Expand Down
Loading

0 comments on commit f443e40

Please sign in to comment.