簡體   English   中英

Apache Lucene:如何將索引保存到文件中?

[英]Apache Lucene: How to save an index into a file?

我正在開發一個應用程序,它可以在大型靜態數據存儲庫中啟用索引搜索。 不是服務器客戶端應用程序,其中服務器始終處於啟動狀態,但它是每次按需啟動的本機應用程序

我想將存儲庫中的文件編入索引一次,並將我的工作保存到文件中。 然后,我希望我的應用程序的每個用戶都能夠從保存的文件中加載已創建的索引。

我在“Lucene in 5 Minutes”中看到了以下基本的索引創建代碼:

StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
Directory index = new RAMDirectory();

IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);

IndexWriter w = new IndexWriter(index, config);
addDoc(w, "Lucene in Action", "193398817");
addDoc(w, "Lucene for Dummies", "55320055Z");
addDoc(w, "Managing Gigabytes", "55063554A");
addDoc(w, "The Art of Computer Science", "9900333X");
w.close();


private static void addDoc(IndexWriter w, String title, String isbn) throws IOException {
    Document doc = new Document();
    doc.add(new TextField("title", title, Field.Store.YES));
    doc.add(new StringField("isbn", isbn, Field.Store.YES));
    w.addDocument(doc);
}
  • 我現在如何將變量分析器,索引配置保存到文件中?
  • 我怎樣才能在以后從保存的文件中加載它們,並將它們用於查詢?

我有一個解決方案 - 我將在這里與您分享:

應該采取整個更改,而不是使用RAMDirectory索引,只需使用FSDirectory

例:

FSDirectory index = FSDirectory.open(Paths.get("C:\\temp\\index.lucene"));

在上面的示例中,將創建目錄C:\\temp\\index.lucene並將索引寫入其中。

現在我可以按照“Lucene in 5 Minutes”中所示的步驟查詢索引: http//www.lucenetutorial.com/lucene-in-5-minutes.html

所以,如果我想在另一個應用程序中運行查詢,我應該以相同的方式打開索引,我可以立即對它運行查詢...無需再次索引文檔...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM