Directory lock error with Lucene.Net usage in an ASP.NET MVC site

雨燕双飞 提交于 2019-12-03 12:05:46

The reason why this happens is that the writer creates an empty file called write.lock as a cross-process lock. When that file exists, Lucene assumes that someone has a write lock on that directory.

When you terminate your process incorrectly, the file will not get deleted. So Lucene thinks that someone is still holding on to the lock. That's why you should always have a finally statement which closes the index.

If you are sure that the file is there in error (i.e. no Lucene processes are running) it is fine to just delete the file. Your index may, however, be in a corrupted state, since the writing was obviously terminated midstream.

Seems to be a dead-lock on lucene.

If supposedly NO index update into the collection,
simply remove this lock file C:\Users\Username\Desktop\SiteSolution\Site\lucene\write.lock.

After that re-run the index writing.

Nayab
try
{
    writer = new IndexWriter(directory, new StandardAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
}

catch (LockObtainFailedException ex)
{
     DirectoryInfo indexDirInfo = new DirectoryInfo(directory);
     FSDirectory indexFSDir = FSDirectory.Open(indexDirInfo, new Lucene.Net.Store.SimpleFSLockFactory(indexDirInfo));
     IndexWriter.Unlock(indexFSDir);
     writer = new IndexWriter(directory, new StandardAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
}

Researching this myself it seems that the indended approach would be to use multiple physical indexes and then merge them using the IndexWriter's .addIndexesNoOptimize or .addIndexes to merge all concurrent index changes.

Lucene documentation

Haven't checked it myself but wouldn't this work (write after creating azureDirectory object)?

azureDirectory.ClearLock("write.lock")

I encountered the same issue and I was using an IProviderContext. In my case I had to: Optimize,Commit and Dispose the ProviderContext.

providerContext.Optimize();
providerContext.Commit();
providerContext.Dispose();

I hope this helps. After implementing the above snippet, my index rebuilt successfully.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!