Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

lucene

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":

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?
like image 663
SomethingSomething Avatar asked Sep 28 '22 13:09

SomethingSomething


1 Answers

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.

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.

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

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...

like image 200
SomethingSomething Avatar answered Oct 18 '22 00:10

SomethingSomething