GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Friday, April 15, 2011

Zend - Lucene Search

To create a search application using Zend, you use lucene class:

<?php
require_once "Zend/Search/Lucene.php";

$index = new Zend_Search_Lucene('lola', true);
The above code will create a directory folder for search index. There are five files for it:
1. read.lock.file
2. write.lock.file
3. segments_1
4. segments_2
5. segments_gen

$doc->addField(Zend_Search_Lucene_Field::Keyword('link', '<a href="#">Boo</a>'));
$doc->addField(Zend_Search_Lucene_Field::Text('title', 'Hello World'));
$doc->addField(Zend_Search_Lucene_Field::Unstored('contents', 'This is description'));
$index->addDocument($doc);
$index->commit();
echo $index->count()." Documents indexed";

For displaying the result:

$index = new Zend_Search_Lucene('lola');
$query = 'World';
$hits = $index->find($query);
echo "Index contains: ".$index->count()." documents.<br/>";
        echo "Search for: \"".$query."\" returned \"".count($hits)."\" hits";
foreach($hits as $hit){
     echo "<ul>";
     echo "<li>".$hit->link."</li>";
     echo "<li>".$hit->title."</li>";
     echo "<li>".$hit->score."</li>";
      echo "</ul>";
}
Share/Bookmark

2 comments:

  1. This example does not work. :(

    "Notice: Undefined variable: doc". You should define $doc variable. :)

    ReplyDelete
  2. $doc = new Zend_Search_Lucene_Document();

    ReplyDelete