1

我对编程很陌生,对 Zend/Lucene 索引肯定很陌生。不过,据我所知,我的代码是正确的。我觉得我可能忽略了一个步骤或尝试上传更改并添加到数据库中的内容,以便它们出现在我网站的搜索中。我没有收到任何类型的错误消息。下面是控制器的代码。我想让我知道您是否需要其他任何东西来帮助您理解这一点。提前感谢您提供的任何指导。

class SearchController extends Zend_Controller_Action
{

  public function init()
  {
    $auth = Zend_Auth::getInstance();

    if($auth->hasIdentity()) {
      $this->view->identity = $auth->getIdentity(); 
    }
 }

 public function indexAction()
 {
    // action body
 }

 public function buildAction()
 {
    // create the index
    $index = Zend_Search_Lucene::open(APPLICATION_PATH . '/indexes');
    $page = $this->_request->getParam('page');

    // build product pages
     if ($page == 'search') {
       $mdl = new Model_Search();
       $search = $mdl->fetchAll();
       if ($search->count() > 0) {
       foreach ($search as $s) {
         $doc = new Zend_Search_Lucene_Document();
         $doc->addField(Zend_Search_Lucene_Field::unIndexed('id', $s->id));
         $doc->addField(Zend_Search_Lucene_Field::text('name', $s->name));
         $doc->addField(Zend_Search_Lucene_Field::text('uri', $s->uri));
         $doc->addField(Zend_Search_Lucene_Field::text('description', $s->description));
         $index->addDocument($doc);
        }
       }
       $index->optimize();
       $this->view->indexSize = $index->numDocs();
  } 
 }

  public function resultsAction()
  {
    if($this->_request->isPost()) {
    $keywords = $this->_request->getParam('query');
    $query = Zend_Search_Lucene_Search_QueryParser::parse($keywords);
    $index = Zend_Search_Lucene::open(APPLICATION_PATH . '/indexes');
    $hits = $index->find($query);
    $this->view->results = $hits;
    $this->view->keywords = $keywords;
  } else {
    $this->view->results = null;
  }
}

}

4

1 回答 1

2

Lucene 索引不会自动与您的数据库保持同步,您要么需要重建整个索引,要么在相关文档发生更改时删除并重新添加(您无法编辑现有文档)。

public function updateAction()
{
   // something made the db change
   $hits = $index->find("name: " . $name);
   foreach($hits as $hit) {
     $index->delete($hit->id)  
   }

   $doc = new Zend_Search_Lucene_Document();
   // build your doc

   $index->add($doc);

}

Note that lucene documents had their own internal id property, and be careful not to mistake it for an id keyword that you provide.

于 2010-11-24T20:50:58.857 回答