Apache Lucene 6.5中RAMDirectory备份至FSDirectory问题求助
Hey there! Let's figure out how to properly back up your Lucene index from RAMDirectory to the filesystem—since your previous attempts ran into roadblocks, let's break down the right approaches step by step.
Why Your Previous Methods Failed
Directory.copy()is gone: You're right, this method was removed in newer Lucene versions (starting around 4.x), so it's no longer available for use.indexWriter.addIndexes()throwsIndexNotFoundException: This method expects the target directory to already contain a valid Lucene index (withsegments*files). Since your filesystem directory is empty initially, it throws this error because there's no existing index structure to add to.
Solution 1: Use IndexWriter.copyIndexes() (Recommended)
This is the cleanest, official way to copy an entire index from one directory to another. It handles all index structure details automatically, including creating the necessary segments* files in the target directory.
Here's a working code example:
// Initialize your existing RAMDirectory RAMDirectory ramDir = ...; // Your RAMDirectory instance with the built index // Define the target filesystem directory Path backupDirPath = Paths.get("/your/backup/directory/path"); FSDirectory fsDir = FSDirectory.open(backupDirPath); // Set up IndexWriterConfig (use the same Analyzer as your original index!) IndexWriterConfig writerConfig = new IndexWriterConfig(new StandardAnalyzer()); // Use CREATE mode to overwrite any existing data in the backup directory // Use APPEND if you want to add to an existing backup (not recommended for full backups) writerConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE); // Copy the index safely with try-with-resources to auto-close resources try (IndexWriter writer = new IndexWriter(fsDir, writerConfig)) { writer.copyIndexes(ramDir); System.out.println("Backup completed successfully!"); } catch (IOException e) { e.printStackTrace(); }
Solution 2: Manually Copy Index Files (For Fine-Grained Control)
If you need more control over which files to copy, you can iterate over all files in the RAMDirectory and copy them one by one to the FSDirectory. Just make sure to sync the filesystem afterward to ensure all data is persisted to disk.
RAMDirectory ramDir = ...; Path backupDirPath = Paths.get("/your/backup/directory/path"); FSDirectory fsDir = FSDirectory.open(backupDirPath); try { // Get all file names in the RAMDirectory String[] allFiles = ramDir.listAll(); // Copy each file individually for (String fileName : allFiles) { try (IndexInput input = ramDir.openInput(fileName, IOContext.DEFAULT); IndexOutput output = fsDir.createOutput(fileName, IOContext.DEFAULT)) { input.copyTo(output); } } // Sync the filesystem to ensure all files are written to disk (critical for crash recovery) fsDir.sync(allFiles); System.out.println("Manual backup finished!"); } catch (IOException e) { e.printStackTrace(); }
Important Notes
- Pause writes during backup: Make sure no writes are happening to the
RAMDirectorywhile you're backing it up. If the index changes mid-copy, your backup could be corrupted. - Incremental backups: If you need incremental backups instead of full ones, you can use
addIndexes()—but only after you've done an initial full backup (so the target directory has a valid index structure). - Lucene version compatibility: Ensure your code uses the correct APIs for your Lucene version (e.g., in 8.x+,
FSDirectory.open(Path)is preferred over older constructors).
内容的提问来源于stack exchange,提问作者Prakhar Nigam

