【问题标题】:Updating indexed documents of Zend Search Lucene indexes更新 Zend Search Lucene 索引的索引文档
【发布时间】:2011-04-12 04:38:29
【问题描述】:

我已经阅读了这个问题 Creating and updating Zend_Search_Lucene indexes.

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

$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);

但它给出了相同的结果。

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

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

【问题讨论】:

  • 能把索引打开的部分贴出来吗?

标签: php zend-framework zend-search-lucene


【解决方案1】:

致命错误:抛出异常 第 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 或路径,您可以识别应该与要删除的记录中的参数匹配的字段。所有找到的文档都将从索引中删除。

【讨论】:

  • 感谢您解决我的错误。尽管如此,我的实际问题仍未得到解答。如何删除索引中的所有文档?
  • 我已经找到了类似的代码,但是$id 将如何定位所有文档。据我所知,它只针对那些匹配 $id 的,是 id 字段。
  • 如果 id 是识别文档的唯一键,它与您的记录 1:1 匹配
  • 对了,如何找到所有文件并删除。
  • 如何索引它们?与删除它们的方式相同,循环遍历每个元素
【解决方案2】:

@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 比现在更友好。

如果有帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多