i have working lucene 4.3.1 cluster, , i'm adding automatic hot-backup process, similar what's described in "lucene in action" book manning, , several blog posts out there. however, book based on lucene 2.3, , api has changed in 4.3.1. book says instantiate indexwriter so:
indexdeletionpolicy policy = new keeponlylastcommitdeletionpolicy(); snapshotdeletionpolicy snapshotter = new snapshotdeletionpolicy(policy); indexwriter writer = new indexwriter(dir, analyzer, snapshotter, indexwriter.maxfieldlength.unlimited); and when doing backup:
try { indexcommit commit = snapshotter.snapshot(); collection<string> filenames = commit.getfilenames(); /*<iterate on & copy files filenames>*/ } { snapshotter.release(); } however, seems changed lucene 4.x @ point. snapshotdeletionpolicy configured indexwriterconfig, , that's passed in when creating indexwriter. code have now:
public indexer(directory indexdir, printstream printstream) throws ioexception { indexwriterconfig config = new indexwriterconfig(version.lucene_43, new analyzer()); snapshotter = new snapshotdeletionpolicy(new keeponlylastcommitdeletionpolicy()); writerconfig.setindexdeletionpolicy(snapshotter); indexwriter = new indexwriter(indexdir, writerconfig); } and when starting backup, can't snapshotter.snapshot(). have specify arbitrary commitidentifier id, , use after you're done release snapshot.
snapshotdeletionpolicy snapshotter = indexer.getsnapshotter(); string commitidentifier = generatecommitidentifier(); try { indexcommit commit = snapshotter.snapshot(commitidentifier); (string filename : commit.getfilenames()) { backupfile(filename); } } catch (exception e) { logger.error("exception", e); } { snapshotter.release(commitidentifier); indexer.deleteunusedfiles(); } however, doesn't seem working. regardless of whether there have been docs indexed or not, , regardless of whether have committed or not, call snapshotter.snapshot(commitidentifier) throws illegalstateexception saying no index commit snapshot. looking @ code, snapshotdeletionpolicy seems think there have been no commits, though i'm committing disk every 5 seconds or so. i've verified, , there docs being written , committed indexes time, snapshotter thinks there have been 0 commits.
can tell me might doing wrong? let me know if need post more details.
i posted same question lucene java-user mailing list, , got answer immediately. problem snapshotdeletionpolicy use configure indexwriter initially, not same 1 indexwriter uses. on construction, indexwriter clones snapshotdeletionpolicy passed in, first block of code above should instead:
public indexer(directory indexdir, printstream printstream) throws ioexception { indexwriterconfig config = new indexwriterconfig(version.lucene_43, new analyzer()); writerconfig.setindexdeletionpolicy(new snapshotdeletionpolicy(new keeponlylastcommitdeletionpolicy())); indexwriter = new indexwriter(indexdir, writerconfig); snapshotter = (snapshotdeletionpolicy) indexwriter.getconfig().getindexdeletionpolicy(); } notice last line, you're setting snapshotter indexdeletionpolicy indexwriter config. that's key. after that, second chunk of code detailed in original question works perfectly.
for reference, here's answer got apache lucene mailing list.
Comments
Post a Comment