Java Lucene 4.3.1备份过程

Java Lucene 4.3.1备份过程,java,lucene,backup,Java,Lucene,Backup,我有一个正在工作的Lucene 4.3.1集群,我正在添加一个自动热备份过程,类似于Manning的《Lucene in Action》(Lucene in Action)一书和几篇博客文章中所描述的。然而,这本书是基于Lucene 2.3的,API在4.3.1中稍有改动。书中说要实例化IndexWriter,如下所示: IndexDeletionPolicy policy = new KeepOnlyLastCommitDeletionPolicy(); SnapshotDeletionPol

我有一个正在工作的Lucene 4.3.1集群,我正在添加一个自动热备份过程,类似于Manning的《Lucene in Action》(Lucene in Action)一书和几篇博客文章中所描述的。然而,这本书是基于Lucene 2.3的,API在4.3.1中稍有改动。书中说要实例化
IndexWriter
,如下所示:

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();
}
启动备份时,您不能只执行
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
没有索引提交到snapshot
。查看代码,SnapshotDeletionPolicy似乎认为没有提交,即使我每隔5秒左右提交一次磁盘。我已经验证过了,一直都有文档被写入并提交到索引中,但是
snapshotter
总是认为没有提交


谁能告诉我我可能做错了什么?如果需要发布更多详细信息,请告诉我。

我将同样的问题发布到Lucene java用户邮件列表中,并几乎立即得到了答复。问题在于,最初用于配置
IndexWriter
的快照删除策略与IndexWriter使用的策略不同。在构造时,
IndexWriter
实际上克隆了传入的
snapshotdelectionpolicy
,因此上面的第一个代码块应该如下所示:

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();
} 
请注意最后一行,您将从IndexWriter配置中将快照器设置为
IndexDeletePolicy
。这是关键。在那之后,原始问题中详细描述的第二段代码可以完美地工作


作为参考,我从ApacheLucene邮件列表中获得。

Lucene/Solr邮件列表可能会更有帮助。什么是‘Lucene集群’?你是说Solr吗?很抱歉回复得太晚了。我确实在Lucene邮件列表上得到了帮助,谢谢你指出这一点。我所说的“Lucene集群”是指一个类似于Solr的本地集群,但不使用Solr。
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();
}