lucene中的刪除索引

2021-08-22 05:32:16 字數 810 閱讀 2654

今天在測試lucene的刪除索引中遇到乙個問題

測試**如下:

protected

void

setup()

throws

exception

...

public

void

testdeleteindex()

throws

exception

...

在測試中,首先重建索引(見setup方法),在測試方法中可以看出

1) 刪除前有23個doc(見**段1)

2) 刪除索引(共10個doc,見**段2)

3) 刪除後有13個doc(見**段3)

第一次執行,沒問題,第二次執行就發現**段1處出現斷言錯誤,說該處此刻只有13個doc

後來發現原來使用indexreader.deletedocuments 方法就算呼叫了indexreader.close也並不刪除doc,這與lucene in action(page 33)中的說法有些出入

我想也許是lucene版本不一樣的原因.

其實使用lucene刪除,lucene會做好刪除標記直到你呼叫indexwriter.optimize()才真正從庫中刪除.

出現上面測試所遇到的問題正是因為在重建索引時才真正刪除doc,這樣的話就與斷言的23不符合,測試失敗

lucene的這種刪除方式似乎也不大會影響你的應用,就算沒有從庫中刪除,你也搜尋不到這個

doc了,至於是不是要在刪除索引的時候添上indexwriter.optimize()以及indexwriter.close()這樣的**視情況而定.

lucene中的刪除索引

今天在測試lucene的刪除索引中遇到乙個問題 測試 如下 protected void setup throws exception public void testdeleteindex throws exception 在測試中,首先重建索引 見setup方法 在測試方法中可以看出 1 刪除前...

lucene刪除索引中的Document示例

下面的例項是刪除索引中的document的例項 package com.cn import org.apache.lucene.analysis.standard.standardanalyzer import org.apache.lucene.document.document import o...

lucene 索引刪除

1.indexwriter和indexreader都有刪除索引的方法 deletedocuments 不建議使用indexreader刪除索引 使用indexreader進行刪除時,必須關閉所有已經開啟的indexwriter 當使用當前的indexreader進行搜尋時,即使在不關閉indexre...