[英]Azure BlobStorage blobs to Index
是否可以將文檔上傳到Blob存儲並執行以下操作:
我希望關鍵詞可以搜索。
我有可以將文檔上載到完美的blobstorage的代碼,但是獲得此索引(我知道)的唯一方法是使用Azure搜索服務中的“導入數據”,該服務使用預定義的字段創建和索引-如下:
當僅需要這些字段並且索引每5分鍾自動更新一次時,這非常有用。 但是當我想要一個自定義索引時變成一個問題
但是,我只想要以下字段:
我唯一的問題是我需要能夠檢索Document內容(fileText)才能獲取keyPhrases,但是據我所知,只有在Document Content已經存在於我的索引中時,我才能這樣做訪問該內容?
我對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;
}
}
我希望我能提供太多信息,但是我不知道該如何解釋我所尋找的東西。 如果我沒有道理,請告訴我,以便我解決問題。
我不是在尋找講義代碼,只是在尋找正確的方向。
我將不勝感激任何幫助。
謝謝!
我們可以使用Azure搜索通過Azure搜索REST API和.NET SDK為文檔編制索引。 根據您的描述,我使用.NET SDK創建了一個演示並成功進行了測試。 以下是我的詳細步驟:
創建自定義索引字段模型
[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);
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);
將文檔上傳到索引,有關使用SDK進行操作存儲的更多信息,請參考文檔
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);
從搜索瀏覽器中檢查搜索結果。
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; }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.