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

后端 未结 6 2190
陌清茗
陌清茗 2021-02-13 21:29

I\'m building an ASP.NET MVC site where I want to use Lucene.Net for search. I\'ve already built a SearchController and all of its methods, but I\'m getting an error at runtime

相关标签:
6条回答
  • 2021-02-13 21:41

    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

    0 讨论(0)
  • 2021-02-13 21:42
    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);
    }
    
    0 讨论(0)
  • 2021-02-13 21: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.

    0 讨论(0)
  • 2021-02-13 21:51

    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.

    0 讨论(0)
  • 2021-02-13 21:58

    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.

    0 讨论(0)
  • 2021-02-13 22:05

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

    azureDirectory.ClearLock("write.lock")
    
    0 讨论(0)
提交回复
热议问题