Categories

Archives

Did You Know?

My first PC was a Commodore PC10-II. It had an 8086 CPU at 4.77MHz, 640KB RAM, a 20MB hard drive, and a hercules graphics card that my Dad bought me for $50. It was so slow that when I was looking for a file, I'd type "dir", hit enter, watch the list of files scroll by, and when I saw the one I wanted I'd hit CTRL+C to stop the command.

Recent Comments

Tags

asp audio browser bug business coalesce code crash Database db debian extension framework imap internet legions linux metaverse mysql obscurity patch PHP postgresql properties release scp Second Life second life security session social media sound sql ssh subversion tables tortoisesvn tribes ubuntu virtual world web windows zend zend framework zf

Paginating Zend_Search_Lucene results

This short entry was inspired by a snippet of inefficient code I encountered, which involved iterating over an array with a loop and breaking out of it once enough results were fetched.

Zend_Search_Lucene does not paginate results. It simply returns an array. While it does allow you to specify to only return the first N results (using Zend_Search_Lucene::setResultSetLimit($limit)), this is not really all too useful.

$lucene = Zend_Search_Lucene::open('index');
$hits = $lucene->find('author:"mark twain"');
$page = 1;
$perpage = 10;
return array_slice($hits, $page * $perpage - $perpage, $perpage);

The key element here, of course, is the use of array_slice(), which can be used with any array. So this isn't specific to Zend_Search_Lucene in any way.

Write a comment