首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure BlobStorage blobs索引

Azure BlobStorage blobs索引
EN

Stack Overflow用户
提问于 2016-12-03 16:19:24
回答 1查看 1.2K关注 0票数 2

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

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

我希望关键的词组能够被搜索。

我有一些代码可以将文档上传到工作完美的below存储中,但要获得这个索引(据我所知),唯一的方法是在Azure搜索服务中使用"Import“,该服务使用预定义的字段创建和索引--如下所示:

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

然而,我唯一想要的字段是:

  • fileId
  • fileText(这是文档的内容)
  • blobURL(允许下载文档)
  • keyPhrases(将从fileText中提取)--我也有这样做的代码)

我唯一的问题是,我需要能够检索文档内容(FileText)才能获得keyPhrases,但据我所知,只有当文档内容已经在索引中供我访问该内容时,我才能这样做?

我对Azure的了解非常有限,我很难找到任何与我想做的事情类似的东西。

用于将文档上载到blob存储区的代码如下:

代码语言:javascript
复制
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;
        }
    }

我希望我没有提供太多的信息,但我不知道如何解释我在寻找什么。如果我说不通,请让我知道,这样我就可以解决我的问题了。

我不是在寻找施舍代码,我只是在寻找一个正确的方向。

我很感谢你的帮助。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-05 07:54:35

通过Azure搜索、REST API.NET SDK,可以使用Azure搜索对文档进行索引。根据您的描述,我使用.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.创建DataSource

代码语言:javascript
复制
       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操作存储的更多信息请参阅文档。 CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string");var blobClient = storageAccount.CreateCloudBlobClient();var容器=blobClient.GetContainerReference(“容器名称”);var blobList = container.ListBlobs();var tomIndexsList = blobList.Select(blob => new TomTestModel { fileId = Guid.NewGuid().ToString(),blobURL = blob.Uri.ToString(),blobURL=blob.Uri.ToString= "Blob Content",.ToString= "key词组“,})();var batch = IndexBatch.Upload(tomIndexsList);ISearchIndexClient indexClient = serviceClient.Indexes.GetClient("index");indexClient.Documents.Index(批处理);
  2. 从搜索探索中检查搜索结果。

Page.config文件:

代码语言:javascript
复制
<?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文件:

代码语言:javascript
复制
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; }
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40949845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档