2016-12-16

Azure Search - Part 4 (使用 .NET SDK 來建立 Index)

可以建立 Azure Search Index 的方式有三個(1.Azure Portal,2..NET SDK 3.REST API),就讓我們先來看如何使用 .NET SDK 來建立 index

1. 取得 API Key

Azure Search Service Key
key Type 備註 用途說明
admin keys(系統管理金鑰) 主要/次要 管理服務、建立/刪除 Index、索引子、資料來源;主要/次要用途相同
query keys(查詢金鑰) - 提供 client 端用來讀取 index 與 document
  • 1-1. 登入 Azure Portal
  • 1-2. 開啟 Azure Search
  • 1-3. 點擊 金鑰
    1.AZUREKEY
  • 1-4. 取得 主要管理金鑰 或是 次要管理金鑰
    兩者皆可用來建立 index 1-4ADMINKEY

2. 建立 SearchServiceClient 類別的執行個體

2-1. nuget 安裝 Microsoft.Azure.Search

Install-Package Microsoft.Azure.Search

2-2. using Microsoft.Azure.SearchMicrosoft.Azure.Search.Models

2-3. 建立 SearchServiceClient 執行個體

string searchServiceName = "SearchServiceName";//填 AzureServiceName
string adminApiKey = "AdminApiKey";//填 AdminKey, 主要/次要 都可以
SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));

3. 定義 Azure Search Index

透過初始化 Index class 物件

3-1. IndexName 即 index 名稱

3-2. IndexFieldsField 陣列

  • Field 可設定名稱、資料類型及其他屬性(ex:IsSearchableIsFilterable)
  • 必需指定一個 DataType.String的欄位為 key
  • description 欄位使用繁體中文的分析器,以正確斷字,並設定全文檢索(IsSearchable)
var definition = new Index()
            {
                Name = "hotels",
                Fields = new[]
                {
                    new Field("hotelId", DataType.String){ IsKey = true, IsFilterable = true },
                    new Field("baseRate", DataType.Double){ IsFilterable = true, IsSortable = true, IsFacetable = true },
                    new Field("description", DataType.String,AnalyzerName.ZhHantLucene){ IsSearchable = true },
                    new Field("description_fr",DataType.String, AnalyzerName.FrLucene){ IsSearchable = true },
                    new Field("hotelName", DataType.String){ IsSearchable = true, IsFilterable = true, IsSortable = true },
                    new Field("category", DataType.String){ IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true },
                    new Field("tags", DataType.Collection(DataType.String)){ IsSearchable = true, IsFilterable = true, IsFacetable = true },
                    new Field("parkingIncluded", DataType.Boolean){ IsFilterable = true, IsFacetable = true },
                    new Field("smokingAllowed", DataType.Boolean){ IsFilterable = true, IsFacetable = true },
                    new Field("lastRenovationDate", DataType.DateTimeOffset){ IsFilterable = true, IsSortable = true, IsFacetable = true },
                    new Field("rating", DataType.Int32){ IsFilterable = true, IsSortable = true, IsFacetable = true },
                    new Field("location", DataType.GeographyPoint){ IsFilterable = true, IsSortable = true }
                }
            };

4. 建立 Index

如果有異常會收到CloudException
serviceClient.Indexes.Create(definition);
or
serviceClient.Indexes.CreateAsync(definition);

5. 刪除 Index

serviceClient.Indexes.Delete("hotels");
or
serviceClient.Indexes.DeleteAsync("hotels");

6. 完整程式碼

static void Main(string[] args)
        {
            string searchServiceName = "SearchServiceName";//填 AzureServiceName
   string adminApiKey = "AdminApiKey";//填 AdminKey, 主要/次要 都可以
            SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));

            var definition = new Index()
            {
                Name = "hotels",
                Fields = new[]
                {
                    new Field("hotelId", DataType.String){ IsKey = true, IsFilterable = true },
                    new Field("baseRate", DataType.Double){ IsFilterable = true, IsSortable = true, IsFacetable = true },
                    new Field("description", DataType.String,AnalyzerName.ZhHantLucene){ IsSearchable = true },
                    new Field("description_fr",DataType.String, AnalyzerName.FrLucene){ IsSearchable = true },
                    new Field("hotelName", DataType.String){ IsSearchable = true, IsFilterable = true, IsSortable = true },
                    new Field("category", DataType.String){ IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true },
                    new Field("tags", DataType.Collection(DataType.String)){ IsSearchable = true, IsFilterable = true, IsFacetable = true },
                    new Field("parkingIncluded", DataType.Boolean){ IsFilterable = true, IsFacetable = true },
                    new Field("smokingAllowed", DataType.Boolean){ IsFilterable = true, IsFacetable = true },
                    new Field("lastRenovationDate", DataType.DateTimeOffset){ IsFilterable = true, IsSortable = true, IsFacetable = true },
                    new Field("rating", DataType.Int32){ IsFilterable = true, IsSortable = true, IsFacetable = true },
                    new Field("location", DataType.GeographyPoint){ IsFilterable = true, IsSortable = true }
                }
            };
            serviceClient.Indexes.Create(definition);
        }
文件算清楚,如果有先規劃欄位好 SDK 開發速度上應該是快不少 btw: REST API 建立 index 的用法 使用 REST API 建立 Azure 搜尋服務索引, 寫的滿清楚的,就不重複了

參考資料

  1. 使用 .NET SDK 建立 Azure 搜尋服務索引
  2. Azure-Samples/search-dotnet-getting-started
  3. Microsoft Azure Search Library

沒有留言:

張貼留言