2014-02-20 31 views
0

我是新来的Lucene在Java中。我遇到了一个很奇怪的问题:为什么我不能删除索引目录在Lucene之后我让搜索器为空?

该服务部署在tomcat(struts 2)。当服务启动时,它将创建索引和搜索器实例来提供搜索服务。

然后我想更新索引而不停止服务。所以我创建了另一个索引目录,在此之后,我使用新的索引目录前提到搜索者。然后删除旧的目录。但问题来了,我无法删除旧的目录。 The message says the directory is still being used by an application.

但是为什么?我已经切换了索引目录并关闭了搜索者的前indexReader。我错过了什么吗? Lucene的版本是4.3。

错误消息如下:

Unable to delete file: D:\Projects\.metadata\.me_tcat\webapps\nlp\WEB-INF\data\[email protected]\diag\_0.nvd 

不过,我已经叫indexReader.close()indexDirectory.close()

顺便说一句,有没有什么方法可以找到哪个线程在java中使用目录?

如果我困扰你,我很抱歉我的英语。

回答

0

其实你不能删除该文件夹,因为当你创建一个新的IndexWriter它会锁定该文件夹,并且不允许任何其他应用程序或服务来修改该文件夹。安全问题。

我相信,如果你想删除这是你不得不说

IndexWriter.close(); 

文件夹,如果你有一个打开的IndexSearcher的是文件夹,你还需要说:

Directory directory = null; 
DirectoryReader ireader = null; 
try { 
    directory = FSDirectory.open(new File("path")); 
    ireader = DirectoryReader.open(directory); 

} catch (Exception e) { 
// 
} finally { 
try { 
    if(directory != null) { 
     directory.close(); 
    } 
    if(ireader != null) { 
     ireader.close(); 
    } 
    }catch(IOException e) { 
     // 
    } 
} 

这将解锁文件夹并使其可编辑。

+0

实际上,当我完成索引文档时,我调用IndexWriter.close(),然后使用新索引文件夹初始化搜索器。 –

+0

然后,我想删除旧的索引文件夹,出现消息:“文件夹中的文件正在被应用程序使用”。但搜索者正在使用新的索引文件夹,我不知道为什么我不能删除旧的文件夹 –

+0

你说当服务启动时它会创建一个新的'IndexWriter'和'IndexSearcher',然后创建一个新的索引文件夹,并希望删除旧的,所以你不能删除旧的,除非你调用'IndexWriter.close()'和'IndexSearcher.close()' – Salah