2011-02-28 13 views
0

ユーザーが投稿の内容を変更すると、実際のデータベースのコンテンツフィールドが更新されることがあります。php luceneインデックスファイルの行を更新および削除する方法

同じフィールドにインデックスファイルを更新するにはどうすればよいですか?

ユーザーが投稿を削除すると、その投稿をインデックスファイルで削除するにはどうすればよいですか?

答えて

3

私はsymfonyにLucene検索を使用し、ここで私はそれを使用する方法です:

// Called when an object is saved 
public function save(Doctrine_Connection $conn = null) { 
    $conn = $conn ? $conn : $this->getTable()->getConnection(); 
    $conn->beginTransaction(); 
    try { 
     $ret = parent::save($conn); 

     $this->updateLuceneIndex(); 

     $conn->commit(); 

     return $ret; 
    } catch (Exception $e) { 
     $conn->rollBack(); 
     throw $e; 
    } 
} 

public function updateLuceneIndex() { 
    $index = $this->getTable()->getLuceneIndex(); 

    // remove existing entries 
    foreach ($index->find('pk:' . $this->getId()) as $hit) { 
     $index->delete($hit->id); 
    } 

    $doc = new Zend_Search_Lucene_Document(); 

    // store job primary key to identify it in the search results 
    $doc->addField(Zend_Search_Lucene_Field::UnIndexed('pk', $this->getId())); 

    // index job fields 
    $doc->addField(Zend_Search_Lucene_Field::unStored('title', Utils::stripAccent($this->getTitle()), 'utf-8')); 
    $doc->addField(Zend_Search_Lucene_Field::unStored('summary', Utils::stripAccent($this->getSummary()), 'utf-8')); 

    // add job to the index 
    $index->addDocument($doc); 
    $index->commit(); 
} 

// Called when an object is deleted 
public function delete(Doctrine_Connection $conn = null) { 
    $index = $this->getTable()->getLuceneIndex(); 

    foreach ($index->find('pk:' . $this->getId()) as $hit) { 
     $index->delete($hit->id); 
    } 

    return parent::delete($conn); 
} 

そして、ここでは、私は私のインデックスを取得する方法である:

public static function getInstance() { 
    return Doctrine_Core::getTable('Work'); 
} 

static public function getLuceneIndexFile() { 
    return sfConfig::get('sf_data_dir') . '/indexes/work.' . sfConfig::get('sf_environment') . '.index'; 
} 

static public function getLuceneIndex() { 
    ProjectConfiguration::registerZend(); 

    if (file_exists($index = self::getLuceneIndexFile())) { 

     return Zend_Search_Lucene::open($index); 
    } else { 
     return Zend_Search_Lucene::create($index); 
    } 
} 

が、それはあなたを助けることを願っています。)

+0

私はこのコードについて小さな質問をしています。索引付けされていないフィールドでfind()をどのように使用できますか? – CodePorter

関連する問題