Welcome to Abdul Malik Ikhsan's Blog

Zend Framework : Zend_Search_Lucene – Content Indexing

Posted in Teknologi, Tutorial PHP, Zend Framework by samsonasik on August 8, 2011

Lucene adalah indexing dan retrieval library yang awalnya didevelop di teknologi Java, dan disupport oleh Apache Software Foundation. Ketika data sudah terindex dalam sistem file, maka tidak membutuhkan database server. Zend_Search_Lucene adalah salah satu komponen dari Zend Framework yang mengimplementasikan teknologi ini.  Zend_Search_Lucene support beberapa fitur :

1. Perangkingan hasil pencarian
2. Powerful query types : boolean, wildcard, phrase queries
3. pencarian dengan field spesifik

Ok, let’s go to example :
Anggaplah kita mempunyai data artikel yang akan kita simpan di index file sistem kita. Kita mempunyai controller yang memanggil komponen Zend_Search_Lucene sebagai berikut :

class TestluceneController extends Zend_Controller_Action
    public function init()
        /* Initialize action controller here */
        $this->indexPath = APPLICATION_PATH.'/indexsearch/index';

Dalam function init, kita deklarasikan path tempat menyimpan index kita. Kalau sudah, kita buat action untuk meng-index data dari database, semisal sebagai berikut :

public function reindexAction()
    // action body
    //just SAMPLE , access to db
    // ( in your REAL DEVELOPMENT, access to db is only in model ) !!!!
    //asumption , 'db' is already registered in registry !!!
    $db = Zend_Registry::get('db');
    $fetch = $db->query("select * from articles")->fetchAll();

    $index = Zend_Search_Lucene::create($this->indexPath);

    foreach($fetch as $key=>$row)
        $doc = new Zend_Search_Lucene_Document();
        $doc->addField(Zend_Search_Lucene_Field::Text('title', $row['title']));
        $doc->addField(Zend_Search_Lucene_Field::UnStored('content', $row['content'] ));

        echo 'Added ' . $row['title'] . ' to index.
    //optimize index...


Nah, action reindexAction kita jalankan dulu sebelum mencoba melakukan pencarian data. Kalau sudah, tinggal kita test deh :

//.......Pencarian data yang sudah terindex.
public function searchAction()
    $data  = array();

    // If a search_query parameter has been posted, search the index.
    $indexopen = Zend_Search_Lucene::open($this->indexPath);
     // Get results.
    $data  = $indexopen->find('"PHP framework" AND "Zend Framework"');
    foreach($data as $key=>$row)
        echo $row->title; echo "<br>";

Okeey, semoga bermanfaat🙂
Referensi :

2 Responses

Subscribe to comments with RSS.

  1. dadan said, on January 28, 2012 at 4:41 pm

    sip bagus sekali, sya jadi faham..terima kasih tutorialnya…

  2. Rocco said, on June 14, 2013 at 6:01 pm

    Hi samsonasik,

    i have indexed a database with 100,000 entries and i would paginate. but ai have a doubt.

    The example of official page http://framework.zend.com/manual/1.12/en/learning.lucene.pagination.html
    put the result into array for nex paginate.

    It is however expensive ?!?!

    I seem that not exist a metod for passing to find() for optimizing limit for a custom result previously of search.

    I try to use \ZendSearch\Lucene\Lucene::setResultSetLimit(50) for restrict sarch but not seem suitable for me.

    What is the best solution?

    Thanks and sorry for my english.

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: