lucene學習 記憶體索引庫和檔案索引庫結合

2021-07-12 05:02:59 字數 2282 閱讀 1220

在lucene索引庫的建立的時候,我們有兩種不同的索引庫建立方式
final path docdir = paths.get("index");

directory directory=fsdirectory.open(paths.get("index"));

這樣建立的索引庫是在本地磁碟上建立乙個index資料夾,並且將索引放在index中,也稱為檔案索引庫

優點:將索引持久化到磁碟上,能長久儲存。

缺點:相比較記憶體索引庫,讀取慢

directory directory =new ramdirectory();
只需要一句**,就建立了乙個記憶體索引庫

優點:讀取快

缺點:不具備持久化能力,結束時候記憶體索引庫便會刪除

根據兩種索引庫的特點我們可以將兩種索引庫結合起來,設計的思路是在程式啟動時,將檔案索引庫中的索引拷貝到記憶體索引庫中,然後讓程式與記憶體索引庫互動,當互動完畢後再將記憶體索引庫的索引持久化到檔案索引庫。
/**

* 1.建立兩個索引庫

* 2.建立兩個indexwriter

* 3.把檔案索引庫中的內容放到記憶體索引庫中

* 4.讓記憶體索引庫和客戶端進行互動

* 5.把記憶體索引庫的內容放到檔案索引庫

*/final path docdir = paths.get("index");

//建立檔案索引庫

directory filedirectory=fsdirectory.open(paths.get("index"));

//建立記憶體索引庫

directory ramdirectory = new ramdirectory(fsdirectory.open(paths.get("index")), null);

analyzer analyzer = new standardanalyzer();

indexwriterconfig iwc = new indexwriterconfig(analyzer);

//操作檔案的indexwriter

indexwriter fileindexwriter = new indexwriter(filedirectory, iwc);

//操作記憶體的indexwriter

analyzer analyzer1 = new standardanalyzer();

indexwriterconfig iwc1 = new indexwriterconfig(analyzer1);

indexwriter ramindexwriter=new indexwriter(ramdirectory, iwc1);

article article = new article();

article.setaid(1l);

article.settitle("lucene是乙個全文檢索引擎");

article.setcontent("baidu,google都是很好的全文檢索引擎");

// 建立document

document document = new document();

field idfield = new field("aid", article.getaid().tostring(),

textfield.type_stored);

field titlefield = new field("title", article.gettitle().tostring(),

textfield.type_stored);

field contentfield = new field("content", article.getcontent()

.tostring(), textfield.type_stored);

document.add(idfield);

document.add(titlefield);

document.add(contentfield);

//把document放到記憶體當中

ramindexwriter.adddocument(document);

ramindexwriter.close();

//把記憶體索引庫的內容合併到檔案索引庫

fileindexwriter.addindexes(ramdirectory);

fileindexwriter.close();

Lucene索引庫的維護

公共 提取,下面的例子會用到的方法 提取公共 獲得indexwrite物件 public indexwriter getindexwriter throws ioexception和建立索引庫一樣 test public void addindex throws ioexception test p...

Lucene專題 索引庫維護

是否分析 是否對域的內容進行分詞處理。前提是我們要對域的內容進行查詢。是否索引 將field分析後的詞或整個field值進行索引,只有索引方可搜尋到。比如 商品名稱 商品簡介分析後進行索引,訂單號 身份證號不用分析但也要索引,這些將來都要作為查詢條件。是否儲存 將field值儲存在文件中,儲存在文件...

Boost 庫和文件索引

翻譯 dozb 按字母順序庫列表 按主題庫列表 字串和文字處理 string and text processing 容器 containers 迭代器 iterators 演算法 algorithms 函式物件和高階程式設計 unction objects and higher order pro...