Azure Search - Part 4 (使用 .NET SDK  來建立 Index)可以建立 Azure Search Index 的方式有三個(1.Azure Portal,2..NET SDK 3.REST API),就讓我們先來看如何使用 .NET SDK 來建立 index
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-4. 取得 主要管理金鑰 或是 次要管理金鑰
兩者皆可用來建立 index
SearchServiceClient 類別的執行個體Microsoft.Azure.SearchInstall-Package Microsoft.Azure.Search
Microsoft.Azure.Search 及 Microsoft.Azure.Search.Modelsstring searchServiceName = "SearchServiceName";//填 AzureServiceName
string adminApiKey = "AdminApiKey";//填 AdminKey, 主要/次要 都可以
SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));
Azure Search Index透過初始化
Indexclass 物件
Index 的 Name 即 index 名稱Index 的 Fields 為 Field 陣列Field 可設定名稱、資料類型及其他屬性(ex:IsSearchable、IsFilterable)DataType.String的欄位為 keydescription 欄位使用繁體中文的分析器,以正確斷字,並設定全文檢索(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 }
                }
            };
Index如果有異常會收到
CloudException
serviceClient.Indexes.Create(definition);
or
serviceClient.Indexes.CreateAsync(definition);
IndexserviceClient.Indexes.Delete("hotels");
or
serviceClient.Indexes.DeleteAsync("hotels");
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 搜尋服務索引, 寫的滿清楚的,就不重複了