RSS

[zend_search_lucene][tutorial] How to use Zend_Search_lucene

30 Dec

With the help of Simon Harris and Thomas Klytta, i get to use Zend_Search_lucence for full text search. Following are the steps for using Zend_search_Lucence within an MVC based module:

Do the following steps in indexAction() of Controller

1) Create index within index/ directory

$index = Zend_Search_Lucene::create('index/');

2) In order to search record from any field of a particular table within the database, get the table records by calling model object

$mapper = new Application_Model_SampletestMapper();
$records = $mapper->fetchAll();

On printing $records you will get list of all the records.

3) Once you have all the records just loop them through; so that to get it documented km populated in the index file

foreach($records as $record)
{
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::text
('id',$record->id, 'UTF-8'));
$doc->addField(Zend_Search_Lucene_Field::text
('name',$record->name, 'UTF-8'));
$doc->addField(Zend_Search_Lucene_Field::text
('email',$record->email, 'UTF-8'));
$index->addDocument($doc);
}

4) Save the index with the updations

$index->commit();

Steps 1) to 4) did the Index Building, now we have to search query across this index file.

Before searching we have to code a simple form having search field and submit button.

5) Add following self explanatory code in controller file

public function getForm()
{
$form = new Zend_Form();
$form->setAction('index')
->setMethod('post');
// Create and configure search_field element:
$search_field = $form->createElement('text', 'search_field');
// Add elements to form:
$form->addElement($search_field)
->addElement('submit', 'search', array('label' => 'Search'));
return $form;
}

6) Call this form in the indexAction()

public function indexAction()
{
// render sampletest/form.phtml
$this->view->form = $this->getForm();
$this->render('form');
// remaining code here …
}

7) For displaying the form add following code in sampletest/form.phtml

<h2>Search:</h2>
<?php echo $this->form; ?>

8) Now come back to the controller file for getting the details of search results. In the indexAction add the following code:

if(isset($_POST['search_field']))
{
$index   = Zend_Search_Lucene::open('index/');
$results = $index->find($_POST['search_field']);
$this->view->index = $results;
$this->view->query = $query;
}

9) Now display the search results through view file; sampletest/form.phtml

<?php
$results = $this->index;
if(count($results) == 0 && isset($this->query))
{
echo "No search results found for the desired Search".
"(i.e. $this->query)";
}
else
{
foreach($results as $result)
{
echo "<br> id: ".$result->id;
echo "<br> name: ".$result->name;
echo "<br> email: ".$result->email;
}
}
?>

C1) Complete code to be added in SampletestController.php:

public function getForm()
{
$form = new Zend_Form();
$form->setAction('index')
->setMethod('post');
// Create and configure search_field element:
$search_field = $form->createElement('text', 'search_field');
// Add elements to form:
$form->addElement($search_field)
->addElement('submit', 'search', array('label' => 'Search'));
return $form;
}
public function indexAction()
{
// render sampletest/form.phtml
$this->view->form = $this->getForm();
$this->render('form');
// building index file
$index = Zend_Search_Lucene::create('index/');
$mapper = new Application_Model_SampletestMapper();
$records = $mapper->fetchAll();
foreach($records as $record)
{
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::text
('id',$record->id, 'UTF-8'));
$doc->addField(Zend_Search_Lucene_Field::text
('name',$record->name, 'UTF-8'));
$doc->addField(Zend_Search_Lucene_Field::text
('email',$record->email, 'UTF-8'));
$index->addDocument($doc);
}
$index->commit();
//searching the desired query string in the index file
if(isset($_POST['search_field']))
{
$index   = Zend_Search_Lucene::open('index/');
$results = $index->find($_POST['search_field']);
$this->view->index = $results;
$this->view->query = $query;
}
}

C2) Complete code to be added in sampletest/form.phtml:

<h2>Search:</h2>
<?php echo $this->form; ?>

<?php
$results = $this->index;
if(count($results) == 0 && isset($this->query))
{
echo "No search results found for the desired Search".
"(i.e. $this->query)";
}
else
{
foreach($results as $result)
{
echo "<br> id: ".$result->id;
echo "<br> name: ".$result->name;
echo "<br> email: ".$result->email;
}
}
?>

In the same pattern add the data of all tables in the index/ directory so that to ensure full text search across whole database instead of single field search of a single table.

Great Thanks to Simon Harris and Thomas Klytta

Advertisements
 
2 Comments

Posted by on December 30, 2011 in zendframework

 

Tags: , , ,

2 responses to “[zend_search_lucene][tutorial] How to use Zend_Search_lucene

  1. derivatives-litigation.blogspot.co.uk

    April 21, 2013 at 2:14 am

    Fantastic post however I was wondering if you could write
    a litte more on this subject? I’d be very grateful if you could elaborate a little bit further. Bless you!

     
    • Sidra Sultana

      April 21, 2013 at 4:35 pm

      Thanks. Ya, I will surely share if got more findings on the topic in case of content inavailability on web.

       

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

 
%d bloggers like this: