我想在我的webapp上执行SearchBox。我按照教程:SeachBox Tutorial兴奋地做了所有作者提到的,我收到了一个错误:
指定目录中不存在索引。
我的SearchController:
<?php
class SearchController extends Controller
{
private $_indexFiles = 'runtime.search';
public function init(){
Yii::import('application.vendors.*');
require_once('Zend/Search/Lucene.php');
parent::init();
}
/**
* Search index creation
*/
public function actionCreate()
{
$index = Zend_Search_Lucene::create($_indexFiles);
$index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles), true);
$posts = News::model()->findAll();
foreach($news as $news){
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::Text('title',
CHtml::encode($news->name), 'utf-8')
);
$doc->addField(Zend_Search_Lucene_Field::Text('link',
CHtml::encode($news->url)
, 'utf-8')
);
$doc->addField(Zend_Search_Lucene_Field::Text('content',
CHtml::encode($news->description)
, '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'));
}
}
}
有什么想法可以解决这个问题吗?谢谢你的帮助。
编辑:好的,解决方案非常明显。索引没有写,因为它没有真正宣布......这个私有$ _indexFiles ='runtime.search'; 在init之前应该只是在actionCreate函数中 - 然后它可以工作
感谢您的帮助!
答案 0 :(得分:1)
你有一个错字:
$posts = News::model()->findAll();
foreach($news as $news){
应该是:
$posts = News::model()->findAll();
foreach($posts as $news){