1

我已经阅读了这个问题 创建和更新 Zend_Search_Lucene 索引

但它未能回答我的问题。这篇来自 zend 的文章告诉我们无法更新文档。为了有效地更新,每个文档都必须被删除并重新索引。

$removePath = ...;
$hits = $index->find('path:' . $removePath);
foreach ($hits as $hit) {
    $index->delete($hit->id);
}

现在,这对我不起作用。我在中给出了索引路径$removePath并尝试了代码。它没有用。如果我使用与我的特定索引相关的东西,比如$index->find("title:test");它会抛出

Fatal error:  Exception thrown without a stack frame in Unknown on line 0

我也尝试过使用

  $query = new Zend_Search_Lucene_Search_Query_Term(new Zend_Search_Lucene_Index_Term('test', 'title'));
  $hits = $this -> index->find($query);

但它给出了相同的结果。

我什至不知道如何调试这种类型的错误。即使它被调试,我也只会得到搜索的项目而不是所有的文档。因此,不会删除所有文档。

谁能告诉我我做错了什么。如何更新您的搜索索引?

4

2 回答 2

2

致命错误:在第 0 行的 Unknown 中抛出没有堆栈帧的异常

表示你抛出了一个异常,不能抛出异常。当您尝试在 php 析构函数或 php 异常处理程序中抛出异常时,通常会发生这种情况(析构函数和异常处理程序没有stack frame

此错误消息有点神秘,因为它没有提示您错误可能在哪里。


但是,这是一个已知问题:Using the index as static property

所以你应该在你的索引上调用commit() 。它将防止 lucene 抛出异常:

$this->index->commit();

要删除文档,您必须遍历索引并删除每个文档。

$index = Zend_Search_Lucene::open('data/index');

$hits = $index->find('id:'.$id);

  foreach ($hits as $hit) {
     $index->delete($hit->id);
  }
}

因此,使用 id 或 path 您可以识别应该与要删除的记录中的参数匹配的字段。所有找到的文档都将从索引中删除。

于 2011-04-12T05:59:57.043 回答
1

@mrN,下面是一个小脚本,可以满足您的要求:

// Function will delete all the docs from the given index 
function delete_all_docs_from_index(Zend_Search_Lucene_Proxy $index) {
    $count = 0;
    $indexDocs = $index->maxDoc();// Get the number of non-deleted docs before running this
    //print "Num of Docs in the index before deletion " . $indexDocs;
    for ($count; $count < $indexDocs; $count++) {
            if (!$index->isDeleted($count)) {
                $index->delete($count);
                $index->commit(); // You have to commit at this point after deleting
        }
    }
    $index->optimize(); // highly recommended
    //print  "Num of Docs in the index after deletion " . $indexDocs;
    return $index;
}

修改您认为合适的功能。

我希望他们的 API 比现在更友好。

让我知道这是否有帮助。

于 2014-09-19T05:08:13.353 回答