Skip to content

Commit

Permalink
ENGCOM-4481: Resolve Issue : Search REST API returns wrong total_count
Browse files Browse the repository at this point in the history
  • Loading branch information
sidolov authored Mar 14, 2019
2 parents c7c7e3d + 19cf77f commit 03b4e15
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
48 changes: 47 additions & 1 deletion lib/internal/Magento/Framework/Search/Adapter/Mysql/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,6 +50,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
Expand Down Expand Up @@ -86,7 +97,7 @@ public function query(RequestInterface $request)
$response = [
'documents' => $documents,
'aggregations' => $aggregations,
'total' => count($documents)
'total' => $this->getSize($query)
];
return $this->responseFactory->create($response);
}
Expand Down Expand Up @@ -115,4 +126,39 @@ private function getConnection()
{
return $this->resource->getConnection();
}

/**
* Get rows size
*
* @param Select $query
* @return int
*/
private function getSize(Select $query): int
{
$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
*
* @param Select $query
* @return Select
*/
private function getSelectCountSql(Select $query): Select
{
foreach ($this->countSqlSkipParts as $part => $toSkip) {
if ($toSkip) {
$query->reset($part);
}
}

return $query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 03b4e15

Please sign in to comment.