2011-04-12 154 views
1

我已閱讀此問題 Creating and updating Zend_Search_Lucene indexes更新Zend的索引文件搜索Lucene索引

但它未能回答我的問題。來自zend的This文章告訴說,更新文檔是不可能的。要有效更新,每個文檔都必須刪除並重新編制索引。

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

現在,這不適用於我。我給了$removePath中的索引Path並嘗試了代碼。它沒有工作。如果我使用的東西相對於我特定的索引,如$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); 

嘗試,但它給了相同的結果。

我甚至不知道如何調試該類型的錯誤。即使它被調試,我只會得到搜索的項目,而不是所有的文件。所以,所有的文件都不會被刪除。

任何人都可以告訴我我做錯了什麼。如何更新您的搜索索引?

+0

請問您可以張貼索引處於開放狀態的部分嗎? – opHASnoNAME 2011-04-12 05:16:30

回答

2

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

表示您在拋出異常時拋出異常。通常發生這種情況時,當你嘗試拋出一個異常的PHP破壞或一個PHP異常處理程序(析構函數和異常處理程序沒有stack frame

此錯誤消息有點神祕,因爲它給你沒有提示錯誤的地方可能。


然而,這是一個已知問題:Using the index as static property

所以,你應該叫提交()上的索引。這將防止從Lucene的拋出異常:

$this->index->commit(); 

要刪除已通過索引來interate並刪除每個文檔的文檔。

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

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

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

因此,使用id或路徑標識與您想要刪除的記錄中的參數匹配的字段。所有找到的文件都將從索引中刪除。

+0

感謝您解決我的錯誤。儘管如此,我的實際問題仍未得到解答。如何刪除索引中的所有文檔? – mrN 2011-04-12 10:24:26

+0

我更新了我的答案;) – 2011-04-12 14:00:08

+0

我已經找到了類似的代碼,但'$ id'如何定位所有文檔。據我所知,它只針對那些匹配的$ id,是id字段。 – mrN 2011-04-13 07:29:46

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比目前的情況更友好。

讓我知道是否有幫助。