我正在使用Zend_Search_Lucene创建文章索引,以便在我的网站上进行搜索。每当管理员在管理区更新/创建/删除文章时,索引都会被重建:
$config = Zend_Registry::get("config");
$cache = $config->lucene->cache;
$path = $cache . "/articles";
try
{
$index = Zend_Search_Lucene::open($path);
}
catch (Zend_Search_Lucene_Exception $e)
{
$index = Zend_Search_Lucene::create($path);
}
$model = new Default_Model_Articles();
$select = $model->select();
$articles = $model->fetchAll($select);
foreach ($articles as $article)
{
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::Text("title", $article->title));
$index->addDocument($doc);
}
$index->commit();
我的问题是,由于我正在重新索引文章并处理已删除的文章,为什么不每次都使用“create”(而不是“open”和更新)?使用上述方法,我认为文章将与addDocument一起添加每次(因此会有重复)。如何防止这种情况发生?是否有一种方法可以检查文档在索引中是否已存在?
另外,我不认为我完全理解当您“打开”并更新索引时索引是如何工作的。它似乎每次在索引文件夹中创建新的# .cfs文件(因此我有_0.cfs,_1.cfs,_2.cfs),但是当我使用“create”时,它将该文件覆盖为一个新的#.cfs文件并将#递增(例如只有_2.cfs)。您能否请说明这些分段文件是什么?