Zend Lucene Search – part3 – retrieving the indexed data


From ganeshhs.com

Once the index is created, we are ready use zend lucene search to search the website. In the following example, php is the search keyword used to fetch the relevant search results in the already indexed data.

  1. <?php
  2. require_once ‘Zend/Search/Lucene.php’;$query = “php”;
  3. $index = Zend_Search_Lucene::open(“/var/www/lucene-data/blog-index”);
  4. $results = $index->find($query);
  5. echo “Index contains “.$index->count().” documents.\n\n”;
  6. if($index->count())
  7. {
  8. $count = 0;
  9. foreach ($results as $result)
  10. {
  11. $data[$count][“article_url”]         = $result->url;
  12. $data[$count][“article_title”]        = $query->highlightMatches($result->title);
  13. $data[$count][“article_description”]        = $query->highlightMatches($result->contents);
  14. $data[$count][“article_created_date_time”]    = $result->postedDateTime;
  15. $data[$count][“article_id”]             = $result->articleId;
  16. $count++;
  17. }
  18. }
  19. print_R($data);
  20. ?>

To retrieve the index data, first thing we need to do is to open the indexed path.

  1. $index = Zend_Search_Lucene::open(“/var/www/lucene-data/blog-index”);

Suppose if user search input is –

  1. $query = “php”;

We have to use the find method of zend search lucene –

  1. $results = $index->find($query);

To retrieve the total records resulted in the search result, we have to use count method of zend lucene search

  1. echo “Index contains “.$index->count().” documents.\n\n”;

To limit the search result count we have to use setResultSetLimit of zend lucene search

  1. $index->setResultSetLimit(10);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s