我正在尝试索引从 tomcat 服务器获得的大量日志文件。我编写了代码来打开每个文件,为每一行创建一个索引,然后使用 Apache lucene 存储每一行。所有这些都是使用多线程完成的。
当我尝试使用此代码时出现此异常
org.apache.lucene.store.LockObtainFailedException: Lock obtain timed out:
代码
if (indexWriter.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE)
{
// New index, so we just add the document (no old document can be there):
System.out.println("adding " + path);
indexWriter.addDocument(doc);
} else {
// Existing index (an old copy of this document may have been indexed) so
// we use updateDocument instead to replace the old one matching the exact
// path, if present:
System.out.println("updating " + path);
indexWriter.updateDocument(new Term("path", path), doc);
}
indexWriter.commit();
indexWriter.close();
现在我想,因为我每次都提交索引,它可能会导致写锁。所以我删除了indexWriter.commit();
:
if (indexWriter.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE)
{
// New index, so we just add the document (no old document can be there):
System.out.println("adding " + path);
indexWriter.addDocument(doc);
} else {
// Existing index (an old copy of this document may have been indexed) so
// we use updateDocument instead to replace the old one matching the exact
// path, if present:
System.out.println("updating " + path);
indexWriter.updateDocument(new Term("path", path), doc);
}
indexWriter.close();
现在我也不例外
问:所以我的问题是为什么 indexWriter.commit(); 导致异常。即使我删除 indexWriter.commit(); 我在搜索时没有遇到任何问题。那就是我得到了我想要的确切结果。那为什么要使用 indexWriter.commit(); ?