PhpRiot
Follow phpriot on Twitter
Sponsored Link
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Free iPad/iPhone App
Available on the App Store

  • PHP manual
  • Zend Framework manual
  • Smarty manual
  • PHP articles
  • PHP training

Search result pagination

As mentioned above, search result hit objects use lazy loading for stored document fields. When any stored field is accessed, the complete document is loaded.

Do not retrieve all documents if you actually need to work only with some portion of them. Go through the search results and store document IDs (and optionally the score) somewhere to retrive documents from the index during the next script execution.

Example 27. Search result pagination example

$cacheId = md5($query);

if (!$resultSet = $cache->load($cacheId)) {
    $hits = $index->find($query);
    $resultSet = array();
    foreach ($hits as $hit) {
        $resultSetEntry          = array();
        $resultSetEntry['id']    = $hit->id;
        $resultSetEntry['score'] = $hit->score;

        $resultSet[] = $resultSetEntry;
    }

    $cache->save($resultSet, $cacheId);
}

$publishedResultSet = array();
for ($resultId = $startId; $resultId < $endId; $resultId++) {
    $publishedResultSet[$resultId] = array(
        'id'    => $resultSet[$resultId]['id'],
        'score' => $resultSet[$resultId]['score'],
        'doc'   => $index->getDocument($resultSet[$resultId]['id']),
    );
}

Zend Framework