From 7c55811a43ae0a9d80cc3c6544c42088bfe0d790 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Tue, 12 Mar 2019 17:22:27 +0530 Subject: [PATCH 1/3] Resolve Issue : Search REST API returns wrong total_count --- .../Search/Adapter/Mysql/Adapter.php | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Search/Adapter/Mysql/Adapter.php b/lib/internal/Magento/Framework/Search/Adapter/Mysql/Adapter.php index f4d83ece134cf..501136f76752f 100644 --- a/lib/internal/Magento/Framework/Search/Adapter/Mysql/Adapter.php +++ b/lib/internal/Magento/Framework/Search/Adapter/Mysql/Adapter.php @@ -49,6 +49,16 @@ class Adapter implements AdapterInterface */ private $temporaryStorageFactory; + /** + * Query Select Parts to be skipped when prepare query for count + * + * @var array + */ + private $countSqlSkipParts = [ + \Magento\Framework\DB\Select::LIMIT_COUNT => true, + \Magento\Framework\DB\Select::LIMIT_OFFSET => true, + ]; + /** * @param Mapper $mapper * @param ResponseFactory $responseFactory @@ -86,7 +96,7 @@ public function query(RequestInterface $request) $response = [ 'documents' => $documents, 'aggregations' => $aggregations, - 'total' => count($documents) + 'total' => $this->getSize($query) ]; return $this->responseFactory->create($response); } @@ -115,4 +125,36 @@ private function getConnection() { return $this->resource->getConnection(); } + + /** + * Get rows size + * + * @return int + */ + private function getSize($query) + { + $sql = $this->getSelectCountSql($query); + $parentSelect = $this->getConnection()->select(); + $parentSelect->from(['core_select' => $sql]); + $parentSelect->reset(\Magento\Framework\DB\Select::COLUMNS); + $parentSelect->columns('COUNT(*)'); + $totalRecords = $this->getConnection()->fetchOne($parentSelect); + return intval($totalRecords); + } + + /** + * Reset limit and offset + * + * @return Select + */ + private function getSelectCountSql($query) + { + foreach ($this->countSqlSkipParts as $part => $toSkip) { + if ($toSkip) { + $query->reset($part); + } + } + return $query; + } + } From a546e142fa738eeb09e09499bd577e40749cc57c Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Wed, 13 Mar 2019 13:13:55 +0200 Subject: [PATCH 2/3] ENGCOM-4481: Static test fix. --- .../Magento/Framework/Search/Adapter/Mysql/Adapter.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Search/Adapter/Mysql/Adapter.php b/lib/internal/Magento/Framework/Search/Adapter/Mysql/Adapter.php index 501136f76752f..e1b423d738a20 100644 --- a/lib/internal/Magento/Framework/Search/Adapter/Mysql/Adapter.php +++ b/lib/internal/Magento/Framework/Search/Adapter/Mysql/Adapter.php @@ -7,6 +7,7 @@ use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\Ddl\Table; +use Magento\Framework\DB\Select; use Magento\Framework\Search\Adapter\Mysql\Aggregation\Builder as AggregationBuilder; use Magento\Framework\Search\AdapterInterface; use Magento\Framework\Search\RequestInterface; @@ -129,9 +130,10 @@ private function getConnection() /** * Get rows size * + * @param Select $query * @return int */ - private function getSize($query) + private function getSize(Select $query): int { $sql = $this->getSelectCountSql($query); $parentSelect = $this->getConnection()->select(); @@ -139,22 +141,24 @@ private function getSize($query) $parentSelect->reset(\Magento\Framework\DB\Select::COLUMNS); $parentSelect->columns('COUNT(*)'); $totalRecords = $this->getConnection()->fetchOne($parentSelect); + return intval($totalRecords); } /** * Reset limit and offset * + * @param Select $query * @return Select */ - private function getSelectCountSql($query) + private function getSelectCountSql(Select $query): Select { foreach ($this->countSqlSkipParts as $part => $toSkip) { if ($toSkip) { $query->reset($part); } } + return $query; } - } From 9dc26ccc47495eb33809cbdf180c848373251d9f Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Wed, 13 Mar 2019 14:35:16 +0200 Subject: [PATCH 3/3] ENGCOM-4481: Unit test fix. --- .../Search/Test/Unit/Adapter/Mysql/AdapterTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Search/Test/Unit/Adapter/Mysql/AdapterTest.php b/lib/internal/Magento/Framework/Search/Test/Unit/Adapter/Mysql/AdapterTest.php index a35e1fd8b6151..fbb56361bfe71 100644 --- a/lib/internal/Magento/Framework/Search/Test/Unit/Adapter/Mysql/AdapterTest.php +++ b/lib/internal/Magento/Framework/Search/Test/Unit/Adapter/Mysql/AdapterTest.php @@ -161,10 +161,16 @@ public function testQuery() $select = $this->getMockBuilder(\Magento\Framework\DB\Select::class) ->disableOriginalConstructor() ->getMock(); - $this->connectionAdapter->expects($this->once()) + + $this->connectionAdapter->expects($this->exactly(2)) ->method('select') ->willReturn($select); + $this->connectionAdapter->expects($this->once()) + ->method('fetchOne') + ->with($select) + ->willReturn($selectResult['total']); + $table = $this->getMockBuilder(\Magento\Framework\DB\Ddl\Table::class) ->disableOriginalConstructor() ->getMock();