Apache Lucene: How to save the index to a file?
I am working on an application that allows indexing-searching on a large static data store . It is not a server-client application where the server always runs, but it is a native application that runs every time it is requested .
I want to index files in a repository once and save my work to 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 for creating an index 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 do I save the parser, index and config variables to a file now ?
- How can I later load them from the saved files and use them for queries?
source to share
I have a solution - I will share it with you:
The whole change should be accepted, instead of using the index RAMDirectory
just use FSDirectory
.
Example:
FSDirectory index = FSDirectory.open(Paths.get("C:\\temp\\index.lucene"));
The above example will create a directory C:\temp\index.lucene
and write an index to it.
Now I can follow the steps to query 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 have to just open the index in the same way and I can run queries on it right away ... No need to index documents again ...
source to share