Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product stock status change does not reindex Catalog Search index in Update by Schedule mode #3445

Closed
zakdma opened this issue Nov 21, 2024 · 5 comments
Assignees

Comments

@zakdma
Copy link

zakdma commented Nov 21, 2024

Product stock status change does not reindex Catalog Search index in Update by Schedule mode

Preconditions

Install Magento 2.4.7-p1 version

Magento Version :
2.4.7-p1

ElasticSuite Version :
2.11.6

Environment :
Developer mode

Third party modules :
No any

Steps to reproduce

  1. Install Magento from scratch
  2. Put all indexers to Update by Schedule mode
  3. Configure Magento to use OpenSearch for catalog search
  4. Create simple product In Stock with qty: 100
  5. Reindex all
  6. See all indexes are green and 0 in backlog
  7. Edit product and put it to Out of Stock and save
  8. See multiple indexers has 1 in backlog (including Catalog Search indexer) https://prnt.sc/SCWuNnm8ealQ That means on next cron run Catalog Search index will be recalculated for the changed product
  9. Instal composer require smile/elasticsuite 2.11.6
  10. Configure it to use the same OpenSearch service as previously
  11. Edit product and put it to In Stock and save
  12. Reindex all
  13. See all indexes are green and 0 in backlog
  14. Edit product and put it to Out of Stock and save
  15. See only two indexers has 1 in backlog (Stock and Inventory). Catalog Search indexer has 0 in backlog. Screenshot: https://prnt.sc/TSPEt3Bdfj8i That means that on the next cron run Catalog Search index WILL NOT be recalculated for the changed product

Expected result

Catalog Search indexer has 1 in backlog

Actual result

Catalog Search indexer has 0 in backlog
After cron run there is old stock status data in opensearch document so it displays incorrectly
Screenshot: https://prnt.sc/TSPEt3Bdfj8i

@vahonc
Copy link
Collaborator

vahonc commented Nov 22, 2024

Hello @zakdma,

  1. Configure Magento to use OpenSearch for catalog search
  2. Configure it to use the same OpenSearch service as previously

Didn't quite understand these steps.

Did you mean this setting:
Screenshot from 2024-11-22 14-41-29

BR,
Vadym

@zakdma
Copy link
Author

zakdma commented Nov 22, 2024

Hello @zakdma,

  1. Configure Magento to use OpenSearch for catalog search
  2. Configure it to use the same OpenSearch service as previously

Didn't quite understand these steps.

Did you mean this setting: Screenshot from 2024-11-22 14-41-29

BR, Vadym

That means configure elasticsuite to use OpenSearch service the same as previously.
In general add this to env.php

'system' => [
        'default' => [
            'smile_elasticsuite_core_base_settings' => [
                'es_client' => [
                    'servers' => 'localhost:9200',
                    'enable_https_mode' => 0,
                    'http_auth_user' => '',
                    'http_auth_pwd' => '',
                    'enable_http_auth' => false
                ]
            ],
            'catalog' => [
                'search' => [
                    'engine' => 'elasticsuite'
                ]
            ]
        ]
    ],

@zakdma
Copy link
Author

zakdma commented Nov 22, 2024

Never mind, guys.
After some investigation, I found it's is Magento bug.
Looks like they are going to fix it in 2.4.8 but for now I just added plugin to add changed product id to the Catalog Search index backlog.

It looks like this

<?php
declare(strict_types=1);

namespace Tsg\Inventory\Plugin;

use Magento\CatalogInventory\Model\ResourceModel\Stock\Item as ItemResourceModel;
use Magento\Framework\Model\AbstractModel;
use Magento\CatalogSearch\Model\Indexer\Fulltext\Processor as FulltextIndexProcessor;
use Magento\InventoryIndexer\Model\ResourceModel\GetProductIdsBySourceItemIds;

/**
 * Add changed stock item product ID to the Catalog Search indexer backlog to make it reindex
 * Add only in case Catalog Search index has mode Update on Schedule
 */
class ReindexSearchIndexAfterProductStockItemChangedPlugin
{
    public function __construct(
        private readonly FulltextIndexProcessor $fulltextIndexProcessor
    ) {
    }

    public function afterSave(
        ItemResourceModel $subject,
        $result,
        AbstractModel $legacyStockItem
    ) {
        if (!$this->fulltextIndexProcessor->isIndexerScheduled()) {
            // handled via index propagation based on indexers dependencies
            return $result;
        }
        /** @var \Magento\CatalogInventory\Model\Stock\Item $legacyStockItem */
        $ids = [$legacyStockItem->getProductId()];
        $this->fulltextIndexProcessor->getIndexer()->getView()->getChangelog()->addList($ids);

        return $result;
    }
}

@zakdma zakdma closed this as completed Nov 22, 2024
@romainruaud
Copy link
Collaborator

Hi @zakdma do you have a link towards the bug in the Magento2 github repository ?

@zakdma
Copy link
Author

zakdma commented Nov 22, 2024

Hi @zakdma do you have a link towards the bug in the Magento2 github repository ?

Hey, @romainruaud

I have no particular link but I found this bug magento/magento2#38196 and investigating further found that they are working around this module
https://github.com/magento/inventory/blob/1.2.8-beta1/InventoryCatalogSearch/Model/UpdateFulltextIndexOnProductSalabilityChange.php
I didn't tried to check these changes because of lack of time, so created some workaround and planning to return to it next Magento update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants