Reputation: 658
my bad for poorly drafted question I am trying to Take Backup of RAMDirectory index into a FileSystem Directory Path for recovery of an index in case of any crash.
I have tried these approaches
Directory.copy(ramDir, FSDirectory.open(indexDir), false);
But this method is not even showing in Newer versions of Lucene.
The second Approach i used is to indexwriter.addIndexes() but it is throwing this exception
org.apache.lucene.index.IndexNotFoundException: no segments* file found in MMapDirectory
This is source code
BufferedReader reader=new BufferedReader(new FileReader("hash.txt"));
RAMDirectory idx=new RAMDirectory();
String str=reader.readLine();
while(str!=null)
{
Document doc = new Document();
doc.add(new StringField("SPAM",str, Field.Store.YES));
str=reader.readLine();
dcmnts.add(doc);
}
String indexDir="C:\\Users\\xyz\\Desktop\\cmengine\\src\\com\\company\\lucene";
Directory dir = FSDirectory.open(Paths.get(indexDir));
writer.addDocuments(dcmnts);//here dcmnts is ArrayList<Documents>
writer.commit();
// writer.addIndexes(dir); i even tried this didnt worked so i took
//seprate index writer
writer.close();
IndexWriterConfig iwc2 = new IndexWriterConfig(analyzer);
iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
IndexWriter writer2=new IndexWriter(idx, iwc2);
writer2.addIndexes(dir);
Here i even Tried using same IndexWriter to add RAMDirectory to file system.but nothing worked. Is it the order in which i am calling commit then close is this wrong ?? RAMDirectory has a method Sync(Collection) whose javadoc is exactly what i need but I am not aware of how to use it. what is the best approach to solve this problem. Following Answers i checked on SO but nothing worked.. Directory.copy approach
Upvotes: 2
Views: 385
Reputation: 658
The approach that worked in my case is i have used the same instance of indexWriter by reinitializing and pointing it to the FSDirectory where i wanted it to take backup of the RAMIndex. Use Same Analyzer instance for both RAMDirectory and FSDirectory. Define the Separate instance of IWC(index writer config) for the SYNC up task. Code is as follows
IndexWriterConfig iwc2 = new IndexWriterConfig(analyzer);
iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
Directory dir = FSDirectory.open(Paths.get(indexDir));
writer=new IndexWriter(dir,iwc2);
writer.addIndexes(idx);
writer.close();
Upvotes: 2