Ini potongan Codingnya bro…
Search Controler
public function init(){
Yii::import('application.vendors.*');
require_once('Zend/Search/Lucene.php');
parent::init();
}
public function actionCreate()
{
$index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles), true);
$butirs = Butir::model()->findAll();
foreach($butirs as $butir){
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::Text('id_butir',
CHtml::encode($butir->id_butir), 'utf-8')
);
$doc->addField(Zend_Search_Lucene_Field::Text('no_butir',
CHtml::encode($butir->no_butir), 'utf-8')
);
$doc->addField(Zend_Search_Lucene_Field::Text('isi_butir',
CHtml::encode($butir->isi_butir), 'utf-8')
);
// $doc->addField(Zend_Search_Lucene_Field::Text('id_pasal',
// CHtml::encode($butir->pasal->id_pasal), 'utf-8')
// );
$doc->addField(Zend_Search_Lucene_Field::Text('no_pasal',
CHtml::encode($butir->pasal->no_pasal), 'utf-8')
);
$doc->addField(Zend_Search_Lucene_Field::Text('isi_pasal',
CHtml::encode($butir->pasal->isi_pasal), 'utf-8')
);
$doc->addField(Zend_Search_Lucene_Field::Text('nama_kategori',
CHtml::encode($butir->pasal->prokum->kategori->nama_kategori), 'utf-8')
);
$doc->addField(Zend_Search_Lucene_Field::Text('tentang_prokum',
CHtml::encode($butir->pasal->prokum->tentang_prokum), 'utf-8')
);
$doc->addField(Zend_Search_Lucene_Field::Text('id_prokum',
CHtml::encode($butir->pasal->prokum->id_prokum), 'utf-8')
);
$doc->addField(Zend_Search_Lucene_Field::Text('nama',
CHtml::encode($butir->pasal->prokum->generateFilename()), 'utf-8')
);
/*$doc->addField(Zend_Search_Lucene_Field::Text('tentang_prokum',
CHtml::encode($butir->pasal->prokum->generateFilename()), 'utf-8')
);
$doc->addField(Zend_Search_Lucene_Field::Text('no_butir',
CHtml::encode($butir->pasal->butirs->no_butir), 'utf-8')
);
$doc->addField(Zend_Search_Lucene_Field::Text('isi_butir',
CHtml::encode($butir->pasals->butirs->isi_butir), 'utf-8')
);*/
$index->addDocument($doc);
}
$index->commit();
echo 'Lucene index created';
}
public function actionSearch()
{
$this->layout='column2';
if (($term = Yii::app()->getRequest()->getParam('q', null)) !== null) {
$index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles));
$results = $index->find($term);
$query = Zend_Search_Lucene_Search_QueryParser::parse($term);
$this->render('search', compact('results', 'term', 'query'));
}
}
ini di view nya, buat nampilin hasil search berbentuk tree
search.php
<h3>Search Results for: "<?php echo CHtml::encode($term); ?>"</h3>
<?php
if (!empty($results)):
foreach($results as $result):
$dataArray = array(
array('text'=> $query->highlightMatches(CHtml::encode($result->nama)), 'children'=>array(
array('text'=>'Pasal Ke-'.$query->highlightMatches(CHtml::encode($result->no_pasal)), 'children'=>array(
array('text'=>'Butir Ke-'.$query->highlightMatches(CHtml::encode($result->no_butir)), 'children'=>array(
array('text'=>$query->highlightMatches(CHtml::encode($result->isi_butir))),
)),
// array('text'=>'Butir Ke-'),
)),
//array('text'=>'Pasal Ke-'),
)),
);
$this->widget('CTreeView', array(
'data'=>$dataArray,
'animated' => 'fast',
'collapsed' => true,
//'url' => array('AjaxFillTree'),
'htmlOptions' => array('class' => 'treeview-red',
))
);
?>
<?php endforeach; ?>
<?php else: ?>
<p class="error">No results matched your search terms.</p>
<?php endif; ?>
dan hasil masih belum sperti yg diinginkan.
hasil tree masih belum mengGRUP jd satu.
contoh pencarian : ‘[color="#FF0000"]pasal[/color]’
dan hasilnya :
-> undang - undang no 19 tahun 1935
—> [color="#FF0000"]pasal[/color] 1
-----> butir 1
-> undang - undang no 19 tahun 1935
—> [color="#FF0000"]pasal[/color] 2
-----> butir 1
-> undang - undang no 19 tahun 1935
—> [color="#FF0000"]pasal[/color] 3
-----> butir 1
-> undang - undang no 30 tahun 2034
—> [color="#FF0000"]pasal[/color] 1
-----> butir 1
-> undang - undang no 30 tahun 2034
—> [color="#FF0000"]pasal[/color] 2
-----> butir 1
masih seperti ini.