Search results are fetched twice

Xon

Well-known member
Affected version
2.2.10 Patch 1
\XF\Search\Searcher::search calls \XF\Search\Search::executeSearch will load the results, and puts it into a \XF\ResultSet to-do visibility checks, and then returns an array. Then the call to \XF\ResultSet::limitResults() will do that same again, loading the data and doing another view check.

This also applies to the main search, but since this is hidden behind a redirect it isn't immediately obvious.

PHP:
protected function executeSearch(
   Query\Query $query,
   $maxResults,
   \Closure $resultBuilder,
   $applyVisitorPermissions = true
)
{
...

   $resultSet = $this->getResultSet($results)->limitResults($maxResults, $applyVisitorPermissions);
   return $resultSet->getResults();
}

public function actionRecentContent(ParameterBag $params)
{
...
   $resultSet = $searcher->getResultSet($searcher->search($query));
   $resultSet->limitResults(15);

I think the best fix would be for \XF\Search\Search::getResultSetData to check the entity cache for each id before invoking the search content type handler
 
Last edited:
My conclusion too after looking at it a few months back. The difference between the two queries is that it knows the number of pages and what to display where. But I agree it could be made more efficient.
 
Top Bottom