【问题标题】:Azure BlobStorage blobs to IndexAzure Blob 存储 Blob 到索引
【发布时间】:2017-04-18 09:43:37
【问题描述】:

是否可以将文档上传到 blob 存储并执行以下操作:

  1. 抓取文档内容并添加到索引中。
  2. 从第 1 点的内容中获取关键短语并添加到索引中。

我希望关键短语可以搜索。

我有可以将文档上传到完美运行的 blobstorage 的代码,但获取此索引的唯一方法(据我所知)是使用 Azure 搜索服务中的“导入数据”,该服务创建和索引预定义字段 - 如下:

当只需要这些字段并且索引每 5 分钟自动更新一次时,这非常有用。但是当我想要一个自定义索引时就成了一个问题

但是,我唯一想要的字段如下:

  • 文件ID
  • fileText(这是文档的内容)
  • blobURL(允许下载文档)
  • keyPhrases(将从 fileText 中提取 - 我也有执行此操作的代码)

我唯一的问题是我需要能够检索文档内容(fileText)才能获得关键短语,但据我了解,只有当文档内容已经在索引中时我才能这样做让我访问该内容?

我对 Azure 的了解非常有限,并且努力寻找与我想做的类似的事情。

我用来将文档上传到我的 blob 存储的代码如下:

public CloudBlockBlob UploadBlob(HttpPostedFileBase file)
    {
        string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
        string blobStorageKey = ConfigurationManager.AppSettings["BlobStorageKey"];
        string blobStorageName = ConfigurationManager.AppSettings["BlobStorageName"];
        string blobStorageURL = ConfigurationManager.AppSettings["BlobStorageURL"];
        string UserID = User.Identity.GetUserId();
        string UploadDateTime = DateTime.Now.ToString("yyyyMMddhhmmss").ToString();

        try
        {
            var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), UserID + "_" + UploadDateTime + "_" + file.FileName);

            file.SaveAs(path);

            var credentials = new StorageCredentials(searchServiceName, blobStorageKey);

            var client = new CloudBlobClient(new Uri(blobStorageURL), credentials);

            // Retrieve a reference to a container. (You need to create one using the mangement portal, or call container.CreateIfNotExists())
            var container = client.GetContainerReference(blobStorageName);

            // Retrieve reference to a blob named "myfile.gif".
            var blockBlob = container.GetBlockBlobReference(UserID + "_" + UploadDateTime + "_" + file.FileName);

            // Create or overwrite the "myblob" blob with contents from a local file.
            using (var fileStream = System.IO.File.OpenRead(path))
            {
                blockBlob.UploadFromStream(fileStream);
            }

            System.IO.File.Delete(path);

            return blockBlob;
        }
        catch (Exception e)
        {
            var r = e.Message;
            return null;
        }
    }

我希望我没有提供太多信息,但我不知道如何解释我在寻找什么。如果我说不通,请告诉我,以便我解决我的问题。

我不是在寻找讲义代码,只是在寻找正确的方向。

我将不胜感激。

谢谢!

【问题讨论】:

    标签: c# azure azure-storage azure-blob-storage


    【解决方案1】:

    我们可以通过 Azure Search REST API.NET SDK 使用 Azure Search 来索引文档。 根据您的描述,我用 .NET SDK 创建了一个演示并测试成功。以下是我的详细步骤:

    1. 从 Azure 门户创建 Azure 搜索

    1. 从 Azure 门户获取搜索密钥

    1. 创建自定义索引字段模型

      [SerializePropertyNamesAsCamelCase] public class TomTestModel { [Key] [IsFilterable] public string fileId { get; set; } [IsSearchable] public string fileText { get; set; } public string blobURL { get; set; } [IsSearchable] public string keyPhrases { get; set; } }

    4.创建数据源

           string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
           string adminApiKey = ConfigurationManager.AppSettings["SearchServiceAdminApiKey"];
           SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));
    
           var dataSource = DataSource.AzureBlobStorage("storage name", "connectstrong", "container name");
            //create data source
            if (serviceClient.DataSources.Exists(dataSource.Name))
            {
                serviceClient.DataSources.Delete(dataSource.Name);
            }
            serviceClient.DataSources.Create(dataSource);
    
    1. 创建自定义索引

    var definition = new Index() { Name = "tomcustomindex", Fields = FieldBuilder.BuildForType<TomTestModel>() }; //create Index if (serviceClient.Indexes.Exists(definition.Name)) { serviceClient.Indexes.Delete(definition.Name); } var index = serviceClient.Indexes.Create(definition);

    1. 上传文档到索引,更多使用SDK的操作存储请参考document

              CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string");
              var blobClient = storageAccount.CreateCloudBlobClient();
              var container =blobClient.GetContainerReference("container name");
              var blobList = container.ListBlobs();
      
              var tomIndexsList = blobList.Select(blob => new TomTestModel
              {
                  fileId = Guid.NewGuid().ToString(), blobURL = blob.Uri.ToString(), fileText = "Blob Content", keyPhrases = "key phrases",
              }).ToList();
              var batch = IndexBatch.Upload(tomIndexsList);
              ISearchIndexClient indexClient = serviceClient.Indexes.GetClient("index");
              indexClient.Documents.Index(batch);
      
    2. 检查搜索探索中的搜索结果。

    Page.config 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net452" />
      <package id="Microsoft.Azure.Search" version="3.0.0-rc" targetFramework="net452" />
      <package id="Microsoft.Data.Edm" version="5.6.4" targetFramework="net452" />
      <package id="Microsoft.Data.OData" version="5.6.4" targetFramework="net452" />
      <package id="Microsoft.Data.Services.Client" version="5.6.4" targetFramework="net452" />
      <package id="Microsoft.Rest.ClientRuntime" version="2.3.4" targetFramework="net452" />
      <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.4" targetFramework="net452" />
      <package id="Microsoft.Spatial" version="6.15.0" targetFramework="net452" />
      <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
      <package id="System.Spatial" version="5.6.4" targetFramework="net452" />
      <package id="WindowsAzure.Storage" version="7.2.1" targetFramework="net452" />
    </packages>
    

    TomTestModel 文件:

    using System.ComponentModel.DataAnnotations;
    using Microsoft.Azure.Search;
    using Microsoft.Azure.Search.Models;
    
    namespace TomAzureSearchTest
    {
        [SerializePropertyNamesAsCamelCase]
        public class TomTestModel
        {
            [Key]
            [IsFilterable]
            public string fileId { get; set; }
            [IsSearchable]
            public string fileText { get; set; }
            public string blobURL { get; set; }
            [IsSearchable]
            public string keyPhrases { get; set; }
        }
    }
    

    【讨论】:

    • 感谢汤姆孙。查看代码,我想我可以看到这些部分是如何组合在一起的!谢谢!只有一件事,在您指定 fileId、blobURL、fileText 等的部分中-我遇到的一个问题是访问正在上传的文档中的实际文本-您的代码表明我已经在文档中拥有了内容,我不要,因为我需要从正在上传的文档中获取它。这可能吗?
    • 更多使用SDK的操作存储请参考document
    • 太棒了!谢谢!我将处理您提供的代码和详细信息,以及您提供的链接,并将回复您。谢谢!
    • [isFilterable] 和 [isSearchable] 出现错误。 [SerializePropertyNamesAsCamelCase] 我通过参考 Microsoft.Azure.Search.Models 开始工作,但不知道 [isFilterable] 和 [isSearchable] 参考什么。该建议希望将其更改为 [Filterable],它是 System.Web.IU 的一部分
    • 我更新了答案并添加了 TomtestModel 文件。请试一试。
    猜你喜欢
    • 2018-01-01
    • 2018-04-30
    • 1970-01-01
    • 2012-03-02
    • 2016-03-04
    • 2017-02-20
    • 2019-05-01
    • 2017-11-10
    • 2021-10-08
    相关资源
    最近更新 更多