在lucene中替换IndexReader.lastModified

时间:2015-08-11 11:59:42

标签: java lucene

在Lucene 4.7.1中,方法IndexReader.lastModified()已被删除(前一段时间它已被弃用)。 lastModified的当前等价物是多少?

1 个答案:

答案 0 :(得分:3)

首先,如果您正在尝试确定阅读器是否是最新的,或者是否需要重新打开,请使用commitData映射 执行以下操作。相反,只需使用:DirectoryReader.isCurrent()

3.6文档中的弃用通知提供的说明仍然在很大程度上相关(尽管不完全是最新的):

  

如果需要跟踪索引的提交时间,可以将其存储在提交数据中(参见IndexWriter.commit(Map))

如果您需要知道上次提交时间,可以使用IndexWriter.setCommitData在提交时将其存储在UserData中:

try {
    res = // ...
    if(!res.first()) {
        // ...
    }
} catch ( SQLException e) {
    // ...
}

您可以从DirectoryReader.getIndexCommit()

获取IndexCommit中的用户数据
Map<String, String> commitData = new HashMap();
commitData.put("lastModified", String.valueOf(new Date().getTime()));
indexWriter.setCommitData(commitData);
//Now commit...

注意:这将获取此阅读器当前打开的string modMillis = dirReader.getIndexCommit().getUserData().get("lastModified"); Date modDate = new Date(Long.valueOf(modifiedMillis)); 的数据。因此,它无法确定是否需要重新打开索引。同样,要做到这一点,请使用IndexCommit