简体   繁体   English

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

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

I am working on an application that enables indexed-search in a big static repository of data. 我正在开发一个应用程序,它可以在大型静态数据存储库中启用索引搜索。 This is not a server-client application, in which the server is always up, but is a native application that is launched each time by demand . 不是服务器客户端应用程序,其中服务器始终处于启动状态,但它是每次按需启动的本机应用程序

I want to index the files in the repository once, and to save my work into a file. 我想将存储库中的文件编入索引一次,并将我的工作保存到文件中。 Then, I want every user of my application to be able to load the already created index from the saved file. 然后,我希望我的应用程序的每个用户都能够从保存的文件中加载已创建的索引。

I saw the following basic code of index creation in "Lucene in 5 Minutes": 我在“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);
}
  • How can I now save into a file the variables analyzer, index and config ? 我现在如何将变量分析器,索引配置保存到文件中?
  • How can I later load them from the saved files, and use them for queries? 我怎样才能在以后从保存的文件中加载它们,并将它们用于查询?

I have a solution - I will share it here with you: 我有一个解决方案 - 我将在这里与您分享:

The whole change should be taken, is instead of using RAMDirectory index, just use FSDirectory . 应该采取整个更改,而不是使用RAMDirectory索引,只需使用FSDirectory

Example: 例:

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

In the above example, the directory C:\\temp\\index.lucene will be created and the index will be written into it. 在上面的示例中,将创建目录C:\\temp\\index.lucene并将索引写入其中。

Now I can follow the steps for querying the index as shown in "Lucene in 5 Minutes": http://www.lucenetutorial.com/lucene-in-5-minutes.html 现在我可以按照“Lucene in 5 Minutes”中所示的步骤查询索引: http//www.lucenetutorial.com/lucene-in-5-minutes.html

So, if I want to run a query in another application, I should just open the index in the same way, and I can immediately run queries on it... No need to index the documents again... 所以,如果我想在另一个应用程序中运行查询,我应该以相同的方式打开索引,我可以立即对它运行查询...无需再次索引文档...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM