我有一个工作的Lucene 4.3.1群集,并且我添加了一个自动热备份过程,类似于中描述的“Lucene in Action”来自Manning的书,以及几个博客文章在那里。但是,本书基于Lucene 2.3,API在4.3.1中略有变化。书上说来实例化IndexWriter
像这样:Lucene 4.3.1备份过程
IndexDeletionPolicy policy = new KeepOnlyLastCommitDeletionPolicy();
SnapshotDeletionPolicy snapshotter = new SnapshotDeletionPolicy(policy);
IndexWriter writer = new IndexWriter(dir, analyzer, snapshotter,
IndexWriter.MaxFieldLength.UNLIMITED);
当做一个备份:
try {
IndexCommit commit = snapshotter.snapshot();
Collection<String> fileNames = commit.getFileNames();
/*<iterate over & copy files from fileNames>*/
} finally {
snapshotter.release();
}
但是,看来这改变了使用Lucene 4.x版在某些时候。 SnapshotDeletionPolicy现在使用IndexWriterConfig进行配置,并在创建IndexWriter时传入。这是我到现在为止代码:
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);
}
和启动备份时,你不能只是做snapshotter.snapshot()
。您现在必须指定一个任意的commitIdentifier
ID,并在完成释放快照后使用该ID。
SnapshotDeletionPolicy snapshotter = indexer.getSnapshotter();
String commitIdentifier = generateCommitIdentifier();
try {
IndexCommit commit = snapshotter.snapshot(commitIdentifier);
for (String fileName : commit.getFileNames()) {
backupFile(fileName);
}
} catch (Exception e) {
logger.error("Exception", e);
} finally {
snapshotter.release(commitIdentifier);
indexer.deleteUnusedFiles();
}
但是,这似乎并没有工作。无论是否存在文档索引,也不管是否已经承诺,我致电snapshotter.snapshot(commitIdentifier)
总是抛出IllegalStateException
说No index commit to snapshot
。查看代码,SnapshotDeletionPolicy似乎认为没有任何提交,即使我每隔5秒左右对磁盘进行一次提交。我已经验证过,并且有文档正在编写,并且始终致力于索引,但是snapshotter
总是认为已经有零次提交。
有人可以告诉我我可能会做错什么吗?让我知道是否需要发布更多细节。
Lucene/Solr邮件列表可能会有更多的帮助。什么是'Lucene集群?'你的意思是Solr吗? – bmargulies
对不起,延迟回复。我确实在Lucene邮件列表上得到了帮助,谢谢指出。通过“Lucene集群”,我的意思是一个本土Solr类集群,但不使用Solr。 – mjuarez