Skip to content
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

Fixed Issue #17295 : Search REST API returns wrong total_count #20253

Closed
wants to merge 2 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
42 changes: 42 additions & 0 deletions lib/internal/Magento/Framework/Search/Adapter/Mysql/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ class Adapter implements AdapterInterface
*/
private $temporaryStorageFactory;

/**
* Query Select Parts to be skipped when prepare query for count
*
* @var array
*/
protected $countSqlSkipParts = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the access modifier to private

\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,6 +96,7 @@ public function query(RequestInterface $request)
$response = [
'documents' => $documents,
'aggregations' => $aggregations,
'size' => $this->getSize($query)
];
return $this->responseFactory->create($response);
}
Expand Down Expand Up @@ -114,4 +125,35 @@ private function getConnection()
{
return $this->resource->getConnection();
}

/**
* Get rows size
*
* @return int
*/
public function getSize($query)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change method access modifier to private

{
$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
*
* @return Select
*/
protected function getSelectCountSql($query)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change method access modifier to private

{
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 @@ -68,7 +68,8 @@ public function create($rawResponse)
\Magento\Framework\Search\Response\QueryResponse::class,
[
'documents' => $documents,
'aggregations' => $aggregations
'aggregations' => $aggregations,
'size' => $rawResponse['size']
]
);
}
Expand Down
19 changes: 18 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,23 @@ class QueryResponse implements ResponseInterface
*/
protected $aggregations;

/**
* Total count of collection
*
* @var int
*/
protected $totalCount;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change property access modifier to private


/**
* @param Document[] $documents
* @param AggregationInterface $aggregations
* @param Total size int $size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct the phpdoc: @param int $size Total size

*/
public function __construct(array $documents, AggregationInterface $aggregations)
public function __construct(array $documents, AggregationInterface $aggregations, int $size = 0)
{
$this->documents = $documents;
$this->aggregations = $aggregations;
$this->totalCount = $size;
}

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

/**
* {@inheritdoc}
*/
public function getTotalCount()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@buskamuza @melnikovi can you please take a look if we can allow adding this method to the framework. It appears there is no other way to return the total count of the search result.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class has count method already, that seem to have similar purpose. Are there cases when count that it uses is not efficient?

Copy link
Member Author

@ronak2ram ronak2ram Mar 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@melnikovi Count method returns a total count of the search result items of the current page.
We make getTotalCount for the return all pages items count.

{
return $this->totalCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ public function build(ResponseInterface $response)
{
/** @var \Magento\Framework\Api\Search\SearchResult $searchResult */
$searchResult = $this->searchResultFactory->create();

$documents = iterator_to_array($response);
$searchResult->setItems($documents);
$searchResult->setAggregations($response->getAggregations());
$searchResult->setTotalCount(count($documents));
$searchResult->setTotalCount($response->getTotalCount());

return $searchResult;
}
Expand Down