apply below solution to solve elastic search issue.
<type name="Magento\Elasticsearch7\Model\Client\Elasticsearch"> <plugin name="elastcisearch_filter_plugin" type="Namespace\Modulename\Model\Plugin\ElasticLayerPlugin" /> </type>
namespace Namespace\Modulename\Model\Plugin; class ElasticLayerPlugin { protected $helperObj; public function __construct( \Namespace\Modulename\Helper\Data $helperObj ) { $this->helperObj = $helperObj; } public function beforeQuery($subject,$query) { $productIds = $this->helperObj->filterCollectionIds(); // Add the product ids to filter the Elasticsearch product collection $query['body']['query']['bool']['filter'] = ['ids' => [ 'values' => $productIds]]; return [$query]; } }
Please update the code of the following helper file
\Namespace\Modulename\Helper\Data $helperObj
Thank you
app/code/Vendor/Module/Helper/Data.php
<?php namespace Vendor\Module\Helper; class Data extends \Magento\Framework\App\Helper\AbstractHelper { public $listProductBlock; public function __construct( Context $context, \Magento\Catalog\Block\Product\ListProduct $listProductBlock ) { $this->listProductBlock = $listProductBlock; parent::__construct($context); } public function filterCollectionIds(){ $productCollection = $this->listProductBlock->getLoadedProductCollection(); $productCollection->addAttributeToFilter('custom_attribute', array('eq' => "No")); $filteredIds = array(); foreach($productCollection as $product){ $productId = $product->getId(); $filteredIds[] = $productId; } return $filteredIds; } }