2013-07-19 26 views
2

我有一个工作的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)总是抛出IllegalStateExceptionNo index commit to snapshot。查看代码,SnapshotDeletionPolicy似乎认为没有任何提交,即使我每隔5秒左右对磁盘进行一次提交。我已经验证过,并且有文档正在编写,并且始终致力于索引,但是snapshotter总是认为已经有零次提交。

有人可以告诉我我可能会做错什么吗?让我知道是否需要发布更多细节。

+0

Lucene/Solr邮件列表可能会有更多的帮助。什么是'Lucene集群?'你的意思是Solr吗? – bmargulies

+0

对不起,延迟回复。我确实在Lucene邮件列表上得到了帮助,谢谢指出。通过“Lucene集群”,我的意思是一个本土Solr类集群,但不使用Solr。 – mjuarez

回答

2

我将这个问题发布到Lucene的java用户邮件列表中,并且几乎立即得到了答案。问题在于用于最初配置IndexWriter的SnapshotDeletionPolicy与IndexWriter使用的不同。在建设中,IndexWriter居然克隆SnapshotDeletionPolicy你通过了,所以上面的代码中的第一个块应该是这样的,而不是:

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(); 
} 

通知的最后一行,在那里你设置snapshotter到IndexDeletionPolicy从IndexWriter配置。这是关键。之后,原始问题中详细介绍的第二部分代码完美地工作。

仅供参考,here's the answer我从Apache Lucene邮件列表中获得。