diff --git a/vendor/magento/module-catalog/Model/Indexer/Category/Product/AbstractAction.php b/vendor/magento/module-catalog/Model/Indexer/Category/Product/AbstractAction.php
index 020c19578f75..211f31688741 100644
--- a/vendor/magento/module-catalog/Model/Indexer/Category/Product/AbstractAction.php
+++ b/vendor/magento/module-catalog/Model/Indexer/Category/Product/AbstractAction.php
@@ -13,6 +13,7 @@
 use Magento\Framework\DB\Query\Generator as QueryGenerator;
 use Magento\Framework\DB\Select;
 use Magento\Framework\EntityManager\MetadataPool;
+use Magento\Store\Api\Data\StoreInterface;
 use Magento\Store\Model\Store;

 // phpcs:disable Magento2.Classes.AbstractApi
@@ -126,9 +127,9 @@ abstract class AbstractAction
     private $queryGenerator;

     /**
-     * @var int
+     * @var StoreInterface
      */
-    private $currentStoreId = 0;
+    private $currentStore;

     /**
      * @param ResourceConnection $resource
@@ -171,7 +172,7 @@ protected function reindex()
     {
         foreach ($this->storeManager->getStores() as $store) {
             if ($this->getPathFromCategoryId($store->getRootCategoryId())) {
-                $this->currentStoreId = $store->getId();
+                $this->setCurrentStore($store);
                 $this->reindexRootCategory($store);
                 $this->reindexAnchorCategories($store);
                 $this->reindexNonAnchorCategories($store);
@@ -179,6 +180,28 @@ protected function reindex()
         }
     }

+    /**
+     * Set current store
+     *
+     * @param StoreInterface $store
+     * @return $this
+     */
+    private function setCurrentStore(StoreInterface $store): self
+    {
+        $this->currentStore = $store;
+        return $this;
+    }
+
+    /**
+     * Get current store
+     *
+     * @return StoreInterface
+     */
+    private function getCurrentStore(): StoreInterface
+    {
+        return $this->currentStore;
+    }
+
     /**
      * Return validated table name
      *
@@ -379,13 +402,13 @@ private function addFilteringByChildProductsToSelect(Select $select, Store $stor
             []
         )->joinLeft(
             ['child_cpsd' => $this->getTable('catalog_product_entity_int')],
-            'child_cpsd.' . $linkField . ' = '. 'relation_product_entity.' . $linkField
+            'child_cpsd.' . $linkField . ' = ' . 'relation_product_entity.' . $linkField
             . ' AND child_cpsd.store_id = 0'
             . ' AND child_cpsd.attribute_id = ' . $statusAttributeId,
             []
         )->joinLeft(
             ['child_cpss' => $this->getTable('catalog_product_entity_int')],
-            'child_cpss.' . $linkField . ' = '. 'relation_product_entity.' . $linkField . ''
+            'child_cpss.' . $linkField . ' = ' . 'relation_product_entity.' . $linkField . ''
             . ' AND child_cpss.attribute_id = child_cpsd.attribute_id'
             . ' AND child_cpss.store_id = ' . $store->getId(),
             []
@@ -484,6 +507,7 @@ protected function hasAnchorSelect(Store $store)
      */
     protected function createAnchorSelect(Store $store)
     {
+        $this->setCurrentStore($store);
         $isAnchorAttributeId = $this->config->getAttribute(
             \Magento\Catalog\Model\Category::ENTITY,
             'is_anchor'
@@ -690,7 +714,7 @@ protected function fillTempCategoryTreeIndex($temporaryName)
                     ['ccacs' => $this->getTable('catalog_category_entity_int')],
                     'ccacs.' . $categoryLinkField . ' = c.' . $categoryLinkField
                     . ' AND ccacs.attribute_id = ccacd.attribute_id AND ccacs.store_id = ' .
-                    $this->currentStoreId,
+                    $this->getCurrentStore()->getId(),
                     []
                 )->where(
                     $this->connection->getIfNullSql('ccacs.value', 'ccacd.value') . ' = ?',
@@ -702,8 +726,14 @@ protected function fillTempCategoryTreeIndex($temporaryName)
         foreach ($selects as $select) {
             $values = [];

-            foreach ($this->connection->fetchAll($select) as $category) {
-                foreach (explode('/', $category['path']) as $parentId) {
+            $categories = $this->connection->fetchAll($select);
+            foreach ($categories as $category) {
+                $categoriesTree = explode('/', $category['path']);
+                foreach ($categoriesTree as $parentId) {
+                    if (!in_array($this->getCurrentStore()->getRootCategoryId(), $categoriesTree, true)) {
+                        break;
+                    }
+
                     if ($parentId !== $category['entity_id']) {
                         $values[] = [$parentId, $category['entity_id']];
                     }
