Skip to content

[Backport] Fixed Issue : Search REST API returns wrong total_count #21801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/internal/Magento/Framework/Search/Adapter/Mysql/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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 @@ -85,6 +95,7 @@ public function query(RequestInterface $request)
$response = [
'documents' => $documents,
'aggregations' => $aggregations,
'total' => $this->getSize($query)
];
return $this->responseFactory->create($response);
}
Expand All @@ -105,10 +116,47 @@ private function getDocuments(Table $table)
}

/**
* Get connection.
*
* @return false|\Magento\Framework\DB\Adapter\AdapterInterface
*/
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 (int) $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 @@ -65,7 +65,8 @@ public function create($rawResponse)
\Magento\Framework\Search\Response\QueryResponse::class,
[
'documents' => $documents,
'aggregations' => $aggregations
'aggregations' => $aggregations,
'total' => $rawResponse['total']
]
);
}
Expand Down
17 changes: 16 additions & 1 deletion lib/internal/Magento/Framework/Search/Response/QueryResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ class QueryResponse implements ResponseInterface
*/
protected $aggregations;

/**
* @var int
*/
private $total;

/**
* @param Document[] $documents
* @param AggregationInterface $aggregations
* @param int $total
*/
public function __construct(array $documents, AggregationInterface $aggregations)
public function __construct(array $documents, AggregationInterface $aggregations, int $total = 0)
{
$this->documents = $documents;
$this->aggregations = $aggregations;
$this->total = $total;
}

/**
Expand Down Expand Up @@ -65,4 +72,12 @@ public function getAggregations()
{
return $this->aggregations;
}

/**
* @inheritdoc
*/
public function getTotal(): int
{
return $this->total;
}
}
7 changes: 7 additions & 0 deletions lib/internal/Magento/Framework/Search/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ interface ResponseInterface extends \IteratorAggregate, \Countable
* @return \Magento\Framework\Api\Search\AggregationInterface
*/
public function getAggregations();

/**
* Return total count of items.
*
* @return int
*/
public function getTotal(): int;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Magento\Framework\Api\Search\DocumentFactory;
use Magento\Framework\Api\Search\SearchResultFactory;

/**
* Builder for search response.
*/
class SearchResponseBuilder
{
/**
Expand All @@ -35,6 +38,8 @@ public function __construct(
}

/**
* Build search result by search response.
*
* @param ResponseInterface $response
* @return SearchResultInterface
*/
Expand All @@ -46,7 +51,7 @@ public function build(ResponseInterface $response)
$documents = iterator_to_array($response);
$searchResult->setItems($documents);
$searchResult->setAggregations($response->getAggregations());
$searchResult->setTotalCount(count($documents));
$searchResult->setTotalCount($response->getTotal());

return $searchResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,22 @@ public function testQuery()
'aggregation2' => [2, 4],
],
],
'total' => 1
];

$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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function testCreate()
['title' => 'twoTitle', 'description' => 'twoDescription'],
],
'aggregations' => [],
'total' => 2
];

$this->documentFactory->expects($this->at(0))->method('create')
Expand All @@ -61,7 +62,7 @@ public function testCreate()
$this->objectManager->expects($this->once())->method('create')
->with(
$this->equalTo(\Magento\Framework\Search\Response\QueryResponse::class),
$this->equalTo(['documents' => ['document1', 'document2'], 'aggregations' => null])
$this->equalTo(['documents' => ['document1', 'document2'], 'aggregations' => null, 'total' => 2])
)
->will($this->returnValue('QueryResponseObject'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected function setUp()
[
'documents' => $this->documents,
'aggregations' => $this->aggregations,
'total' => 1
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testBuild()

/** @var QueryResponse|\PHPUnit_Framework_MockObject_MockObject $response */
$response = $this->getMockBuilder(\Magento\Framework\Search\Response\QueryResponse::class)
->setMethods(['getIterator', 'getAggregations'])
->setMethods(['getIterator', 'getAggregations', 'getTotal'])
->disableOriginalConstructor()
->getMockForAbstractClass();
$response->expects($this->any())
Expand All @@ -76,6 +76,9 @@ public function testBuild()
$response->expects($this->once())
->method('getAggregations')
->willReturn($aggregations);
$response->expects($this->any())
->method('getTotal')
->willReturn(1);

$result = $this->model->build($response);

Expand Down