How to create a search engine based on Lucene in Zend Framework 2.5

As I promised, I have developed a very simple search engine based on Lucene. It works very well and you can see the code bellow.

This is my action:

public function searchAction()
{
// here I will require the Search form
$form = new SearchForm();
$form->get('submit')->setValue('search');
// here I will create a Lucene's index
$index = Lucene::create('/indexes');
$users = $this->getAlbumTable()->fetchAll(true);
foreach($users as $user)
{
$doc = new Document();
$doc->addField(Document\Field::Text('artist', $user->artist));
$doc->addField(Document\Field::Text('title',$user->title));
$index->addDocument($doc);
}
$index->commit();
// here I will search in the saved Lecene's index
$term = $this->params()->fromQuery('query', false);
if(!$term):
$term = "";
endif;
$index = Lucene::open('/indexes');
$results = $index->find($term);
return new ViewModel(array(
'results' => $results,
'form' => $form
));
}

In this month I'm going to develop a very big open source project in Zend Framework 2.5 where you will find out the entire code of my search engine based on Lucene!









Leave a Comment