php lucene如何更新和删除索引文件中的行
当用户更改其帖子的内容时,实际数据库中的内容字段将得到更新 如何使索引文件中的同一字段也得到更新php lucene如何更新和删除索引文件中的行,php,zend-framework,search,lucene,Php,Zend Framework,Search,Lucene,当用户更改其帖子的内容时,实际数据库中的内容字段将得到更新 如何使索引文件中的同一字段也得到更新 当用户删除帖子时,我如何删除索引文件中的帖子?我使用了lucene search和Symfony,下面是我如何使用它: // Called when an object is saved public function save(Doctrine_Connection $conn = null) { $conn = $conn ? $conn : $this->getTable()-&
当用户删除帖子时,我如何删除索引文件中的帖子?我使用了lucene search和Symfony,下面是我如何使用它:
// 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);
}
}
希望它能帮助你;) 我将lucene search与Symfony一起使用,下面是我如何使用它的:
// 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);
}
}
希望它能帮助你;) 我对这段代码有一个小小的疑问。如何在未编制索引的字段中使用find()?我对这段代码有一个小的查询。如何在未编制索引的字段中使用find()?