diff --git a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Fulltext/Collection.php b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Fulltext/Collection.php index 76a4c2fc3..25bf10225 100644 --- a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Fulltext/Collection.php +++ b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Fulltext/Collection.php @@ -94,6 +94,15 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection */ private $originalPageSize = false; + /** + * @var array + */ + private $countByAttributeSet; + /** + * @var array + */ + private $countByAttributeCode; + /** * Constructor. * @@ -182,7 +191,7 @@ public function __construct( public function getSize() { if ($this->_totalRecords === null) { - $this->load(); + $this->loadProductCounts(); } return $this->_totalRecords; @@ -463,6 +472,54 @@ protected function _afterLoad() return parent::_afterLoad(); } + /** + * Load product count : + * - collection size + * - number of products by attribute set (legacy) + * - number of products by attribute code + * + * @return void + */ + private function loadProductCounts(): void + { + $storeId = $this->getStoreId(); + $requestName = $this->searchRequestName; + $facets = [ + ['name' => 'attribute_set_id', 'type' => BucketInterface::TYPE_TERM, 'size' => 0], + ['name' => 'indexed_attributes', 'type' => BucketInterface::TYPE_TERM, 'size' => 0], + ]; + $searchRequest = $this->requestBuilder->create( + $storeId, + $requestName, + 0, + 0, + $this->query, + [], + $this->filters, + $this->queryFilters, + $facets + ); + $searchResponse = $this->searchEngine->search($searchRequest); + $this->_totalRecords = $searchResponse->count(); + $this->countByAttributeSet = []; + $this->countByAttributeCode = []; + $this->isSpellchecked = $searchRequest->isSpellchecked(); + $attributeSetIdBucket = $searchResponse->getAggregations()->getBucket('attribute_set_id'); + $attributeCodeBucket = $searchResponse->getAggregations()->getBucket('indexed_attributes'); + if ($attributeSetIdBucket) { + foreach ($attributeSetIdBucket->getValues() as $value) { + $metrics = $value->getMetrics(); + $this->countByAttributeSet[$value->getValue()] = $metrics['count']; + } + } + if ($attributeCodeBucket) { + foreach ($attributeCodeBucket->getValues() as $value) { + $metrics = $value->getMetrics(); + $this->countByAttributeCode[$value->getValue()] = $metrics['count']; + } + } + } + /** * Prepare the search request before it will be executed. *