diff -Nuar a/vendor/magento/module-catalog-search/Model/ResourceModel/Advanced/Collection.php b/vendor/magento/module-catalog-search/Model/ResourceModel/Advanced/Collection.php index 5c2813682ea..70bb642beaa 100644 --- a/vendor/magento/module-catalog-search/Model/ResourceModel/Advanced/Collection.php +++ b/vendor/magento/module-catalog-search/Model/ResourceModel/Advanced/Collection.php @@ -460,7 +460,9 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection 'collection' => $this, 'searchResult' => $searchResult, /** This variable sets by serOrder method, but doesn't have a getter method. */ - 'orders' => $this->_orders + 'orders' => $this->_orders, + 'size' => $this->getPageSize(), + 'currentPage' => (int)$this->_curPage, ] ); } diff -Nuar a/vendor/magento/module-catalog-search/Model/ResourceModel/Fulltext/Collection.php b/vendor/magento/module-catalog-search/Model/ResourceModel/Fulltext/Collection.php index 8e295448868..7996b9ab4e5 100644 --- a/vendor/magento/module-catalog-search/Model/ResourceModel/Fulltext/Collection.php +++ b/vendor/magento/module-catalog-search/Model/ResourceModel/Fulltext/Collection.php @@ -576,6 +576,8 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection 'searchResult' => $searchResult, /** This variable sets by serOrder method, but doesn't have a getter method. */ 'orders' => $this->_orders, + 'size' => $this->getPageSize(), + 'currentPage' => (int)$this->_curPage, ] ); } diff -Nuar a/vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php b/vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php index 0d9dc266742..6e99d75152a 100644 --- a/vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php +++ b/vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php @@ -25,16 +25,32 @@ class SearchResultApplier implements SearchResultApplierInterface */ private $searchResult; + /** + * @var int + */ + private $size; + + /** + * @var int + */ + private $currentPage; + /** * @param Collection $collection * @param SearchResultInterface $searchResult + * @param int $size + * @param int $currentPage */ public function __construct( Collection $collection, - SearchResultInterface $searchResult + SearchResultInterface $searchResult, + int $size, + int $currentPage ) { $this->collection = $collection; $this->searchResult = $searchResult; + $this->size = $size; + $this->currentPage = $currentPage; } /** @@ -46,13 +62,39 @@ class SearchResultApplier implements SearchResultApplierInterface $this->collection->getSelect()->where('NULL'); return; } + + $items = $this->sliceItems($this->searchResult->getItems(), $this->size, $this->currentPage); $ids = []; - foreach ($this->searchResult->getItems() as $item) { + foreach ($items as $item) { $ids[] = (int)$item->getId(); } + $this->collection->setPageSize(null); $this->collection->getSelect()->where('e.entity_id IN (?)', $ids); $orderList = join(',', $ids); $this->collection->getSelect()->reset(\Magento\Framework\DB\Select::ORDER); $this->collection->getSelect()->order(new \Zend_Db_Expr("FIELD(e.entity_id,$orderList)")); } + + /** + * Slice current items + * + * @param array $items + * @param int $size + * @param int $currentPage + * @return array + */ + private function sliceItems(array $items, int $size, int $currentPage): array + { + if ($size !== 0) { + $totalPages = (int) ceil(count($items)/$size); + $currentPage = min($currentPage, $totalPages); + $offset = ($currentPage - 1) * $size; + if ($offset < 0) { + $offset = 0; + } + $items = array_slice($items, $offset, $this->size); + } + + return $items; + } }