我正在使用 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();
我的问题是这个。既然我正在重新索引文章并处理已删除的文章,为什么我不每次都使用“创建”(而不是“打开”和更新)?使用上述方法,我认为每次都会使用 addDocument 添加文章(因此会有重复)。我将如何防止这种情况?有没有办法检查索引中是否已经存在文档?
另外,我认为我不完全理解当您“打开”并更新它时索引是如何工作的。似乎每次都会在索引文件夹中创建新的#.cfs(所以我有_0.cfs、_1.cfs、_2.cfs)文件,但是当我使用“create”时,它会用新的#.cfs 覆盖该文件# 递增的文件(例如,仅 _2.cfs)。你能解释一下这些分段文件是什么吗?