替换 lucene 中的 IndexReader.lastModified

And*_*rew 2 java lucene

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

fem*_*gon 5

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


3.6 文档中的弃用通知提供了一个仍然主要相关的注释(尽管不是完全最新的):

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

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

Map<String, String> commitData = new HashMap();
commitData.put("lastModified", String.valueOf(new Date().getTime()));
indexWriter.setCommitData(commitData);
//Now commit...
Run Code Online (Sandbox Code Playgroud)

您可以从 IndexCommit 获取用户数据 DirectoryReader.getIndexCommit()

string modMillis = dirReader.getIndexCommit().getUserData().get("lastModified");
Date modDate = new Date(Long.valueOf(modifiedMillis));
Run Code Online (Sandbox Code Playgroud)

注意:这将获取IndexCommit此阅读器当前打开的数据。因此,它对于确定是否需要重新打开索引没有用。同样,要做到这一点,请使用DirectoryReader.isCurrent().