使用 Azuredirectory 在 Azure Blob 中创建 lucene 索引时出错

Error creating lucene index in Azure Blob with Azuredirectory

我一直在努力让这个工作。 这就是我想要实现的目标:

  1. 使用 lucene.Net 搜索功能
  2. 在 Azure 上托管这个

问题:

我使用本教程 Lucene.Net ultra fast search for MVC or WebForms site => made easy! 开始,它在从我的 localdb 检索以在我的本地磁盘中创建索引文件并随后调用 rest 方法执行搜索时完美运行。

现在我正在尝试将部分代码转换为引用 blob。 所以原文是这样的:

private static string _luceneDir =Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "lucene_index");
private static FSDirectory _directoryTemp;
private static FSDirectory _directory {
get {
    if (_directoryTemp == null) _directoryTemp = FSDirectory.Open(new DirectoryInfo(_luceneDir));
    if (IndexWriter.IsLocked(_directoryTemp)) IndexWriter.Unlock(_directoryTemp);
    var lockFilePath = Path.Combine(_luceneDir, "write.lock");
    if (File.Exists(lockFilePath)) File.Delete(lockFilePath);
    return _directoryTemp;
}} 

我改成了这样:

private static AzureDirectory azureDirectory = new AzureDirectory(CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")),"SearchCatalog");
private static AzureDirectory _directoryTemp;


        private static AzureDirectory _directory
        {
            get
            {
                // if (_directoryTemp == null) _directoryTemp = FSDirectory.Open(new DirectoryInfo(_luceneDir));
                if (_directoryTemp == null) _directoryTemp = azureDirectory;
                if (IndexWriter.IsLocked(_directoryTemp)) IndexWriter.Unlock(_directoryTemp);
                return _directoryTemp;
            }
        }

我删除了最后两行,因为我不知道如何在 Azure 中获取 blobl 存储的文件路径,据我所知这是不可能的。

现在发生的是 blob 中的容器已创建,但未创建索引文件,我收到 http 404 错误。 详细错误:

{
  "Message": "An error has occurred.",
  "ExceptionMessage": "The remote server returned an error: (404) Not Found.",
  "ExceptionType": "Microsoft.WindowsAzure.Storage.StorageException",
  "StackTrace": "   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)\r\n   at Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient.GetBlobReferenceFromServer(StorageUri blobUri, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)\r\n   at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.GetBlobReferenceFromServer(String blobName, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)\r\n   at Lucene.Net.Store.Azure.AzureLock.IsLocked()\r\n   at Lucene.Net.Index.IndexWriter.IsLocked(Directory directory)\r\n   at brickandmortarv1.Models.LuceneSearch.get__directory()\r\n   at brickandmortarv1.Models.LuceneSearch.AddUpdateLuceneIndex(IEnumerable`1 products)\r\n   at brickandmortarv1.Controllers.LuceneController.Get(String searchterm)\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
  "InnerException": {
    "Message": "An error has occurred.",
    "ExceptionMessage": "The remote server returned an error: (404) Not Found.",
    "ExceptionType": "System.Net.WebException",
    "StackTrace": "   at System.Net.HttpWebRequest.GetResponse()\r\n   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)"
  }
}

在此先感谢,我真的很迷茫我做错了什么。

请删除以下代码

     if (IndexWriter.IsLocked(_directoryTemp)) IndexWriter.Unlock(_directoryTemp);

去掉上面的代码后,就可以了。这是我这边的截图:

以下是我的代码:

 private static AzureDirectory azureDirectory = new AzureDirectory(CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=jastorage1;AccountKey=***"),"sample"

        private static AzureDirectory _directoryTemp;
        private static AzureDirectory _directory
        {
            get
            {
                // if (_directoryTemp == null) _directoryTemp = FSDirectory.Open(new DirectoryInfo(_luceneDir));
                if (_directoryTemp == null) _directoryTemp = azureDirectory;
               // if (IndexWriter.IsLocked(_directoryTemp)) IndexWriter.Unlock(_directoryTemp);
                _directoryTemp.ClearLock("write.lock");
                return _directoryTemp;
            }
        }