Problem with Lucene Search in Yii

Hi,

In my application I use zend lucene search. Everything is fine when search is English, but in Cyrillic (Bulgarian), search results go incomprehensible character. I found similar problem in the forum and made ​​the necessary changes, but has no effect. Can anyone help please.

Here is my source code:

Controller:




public function actionCreate()

{

   $index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles), true);

   $messages = Messages::model()->findAll();

   foreach($messages as $message){

       $mes = new Zend_Search_Lucene_Document();                

       $mes->addField(Zend_Search_Lucene_Field::Text('title',CHtml::encode($message->title), 'utf-8'));      

       $mes->addField(Zend_Search_Lucene_Field::Text('content',CHtml::encode($message->content), 'utf-8')); 

       $mes->addField(Zend_Search_Lucene_Field::Text('link',CHtml::encode($message->link), 'utf-8'));                

       $index->addDocument($mes);

   }

   $index->commit();

}


public function actionSearch()

{

 Zend_Search_Lucene_Analysis_Analyzer::setDefault(newZend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive ());

 $this->layout='column2';

 if (($term = Yii::app()->getRequest()->getParam('q', null)) !== null) {

            $this->actionCreate();

            $index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles));

            

            $query = Zend_Search_Lucene_Search_QueryParser::parse($term, 'utf-8');

            $results = $index->find($query);

             

            $dataProvider = new CArrayDataProvider($results, array(

                'pagination'=>array(

                'pageSize'=>3,

                ),

            ));      

            

            $this->render('search', array('dataProvider'=>$dataProvider, 'query'=>$query, 'results'=>$results, 'term'=>$term));

        }

}



And View:

search.php




<h3>Search Results for: "<?php echo CHtml::encode($term); ?>"</h3>

            

<?php

    $this->widget('zii.widgets.CListView', array(

      'dataProvider'=>$dataProvider,

       'itemView'=>'_view',

       'viewData'=>array('query'=>$query),           

    ));

?> 



_view.php




<?php if (!empty($data)): ?>                 

      <p><strong>Title:</strong> <?php echo $query->highlightMatches(CHtml::encode($data->title)); ?></p>

      <?php //echo CHtml::link('Read', array('view', 'id'=>$data->id)); ?>

      <p><strong>Link: </strong><?php echo CHtml::link(CHtml::encode($data->link), CHtml::encode($data->link), array('target'=>'_blank')) ?></p>

      <p><strong>Content:</strong> <?php echo $query->highlightMatches(CHtml::encode($data->content)); ?></p>

      <hr/>

<?php else: ?>

      <p class="error">No results matched your search terms.</p>

<?php endif; ?>



The results are some like this:

компютърните…

I try with


setlocale(LC_ALL, 'bg_BG.UTF-8');

but nothing happend.

Do you have any suggestion?

Thanks

I found the problem, if anyone is interested. You just have to find another way of highlightMatches. Тhat’s all.

Hi Gogo,

i used your code to paginate the results. But pagination only works when i set the params at the pagination array.


$q=isset($_POST['SearchForm']['query'])?$_POST['SearchForm']['query']:$_GET['q'];

            //$this->actionCreate();

            $index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles));

            $results = $index->find($q);

            $query = Zend_Search_Lucene_Search_QueryParser::parse($q);

            $dataProvider = new CArrayDataProvider($results, array(

                'pagination' => array(

                    'pageSize' => 2,

                    'params' => array(

                        'q' => $q,

                    ),

                ),

            ));

and according to your original issue, I set another param at the highlightMatches method like this:


$query->highlightMatches($data->title,'utf-8')

and this was only needed at windows developing environment.

Best Regards

Laszlo from Hungary

Hi, testing over and over the HighlightMatches() method, there is definitely an issue about it. It does not work well sometimes, if the highlighted word has some special hungarian characters.

bye