[Azure] C# 使用 Azure.Search.Documents Azure Blob 搜尋中的 JSON 資料 -複製(修改)索引,索引子

2022-07-27

之前文章我們談到了 使用 Azure.Search.Documents Azure Blob 搜尋中的 JSON 資料 -刪除索引子資料 還有 更動資料只要重新製作索引子(indexer) 就會更新 index 

但是,如果您開發的系統不可能因為刪除一些資料而高頻繁的重新製作索引,正在製作的時候你查詢會出現錯誤。



這篇我們要做的測試就是,試著將 BLOB 上面的資料刪除 Id : USER994,USER995,USER996 這三筆資料

之後我會建立一個新的 index : azureblob-index-new 再建立一個索引子 indexer: auzreblob-indexer-new,並且該索引子是使用新創立的 azureblob-index-new。


1. 我先將 BLOB 上面資料刪除 Id USER994,USER995,USER996 
C# code:

public static void DeleteSomeTestData() { var i = 994; var blobClient = new Azure.Storage.Blobs.BlobClient(ConnectionString, "test1000", "data/" + "data" + i + ".json"); var res = blobClient.DeleteIfExists().Value; Console.WriteLine("Delete USER994 Result:" + res); i = 995; blobClient = new Azure.Storage.Blobs.BlobClient(ConnectionString, "test1000", "data/" + "data" + i + ".json"); res = blobClient.DeleteIfExists().Value; Console.WriteLine("Delete USER995 Result:" + res); i = 996; blobClient = new Azure.Storage.Blobs.BlobClient(ConnectionString, "test1000", "data/" + "data" + i + ".json"); res = blobClient.DeleteIfExists().Value; Console.WriteLine("Delete USER996 Result:" + res); }


2.接下來,因為我們之前透過 Azure  後台建立過 azureblob-index (使用 Azure.Search.Documents Azure Blob 搜尋中的 JSON 資料 -建立環境 ),我這邊採取做法就是將他讀取出來 這樣就不用自己準備物件之後改名字後在寫入 ,程式碼部分

就是將他讀取出來,改名為 azureblob-index-new 在寫入回去 ,當然你可以自己準備這樣比較麻煩,這算是偷吃步的方法,既然能坐何必要站,既然能躺何必要坐

C# code:

public static void CloneNewIndex() { var indexClient = new Azure.Search.Documents.Indexes.SearchIndexClient(new Uri(SearchServiceEndPoint), new Azure.AzureKeyCredential(AdminApiKey)); var temp1 = indexClient.GetIndex("azureblob-index"); var temp2 = new Azure.Search.Documents.Indexes.Models.SearchIndex("azureblob-index-new"); temp2.Fields = temp1.Value.Fields; var res = indexClient.CreateOrUpdateIndex(temp2); //這是對 indexer 做重新建立 Console.WriteLine("CreateNewIndex Result:"); Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(res)); }


這時候我們看看 Azure 後台看看是不是出現該索引了。


3.接下來就是 一樣讀取之前建立的索引子 azureblob-indexer  (使用 Azure.Search.Documents Azure Blob 搜尋中的 JSON 資料 -建立環境 ),並且更改名字為 azureblob-indexer-new 並且將所以改成第二步驟產生的 azureblob-index  

C# code:

public static void CloneNewIndexer() { var indexer = new Azure.Search.Documents.Indexes.SearchIndexerClient(new Uri(SearchServiceEndPoint), new Azure.AzureKeyCredential(AdminApiKey)); var dataSource = new Azure.Search.Documents.Indexes.Models.SearchIndexerDataSourceConnection( "blob-datasorurce", Azure.Search.Documents.Indexes.Models.SearchIndexerDataSourceType.AzureBlob,ConnectionString,new Azure.Search.Documents.Indexes.Models.SearchIndexerDataContainer("test1000")); var temp1 = indexer.GetIndexer("azureblob-indexer"); temp1.Value.TargetIndexName = "azureblob-index-new"; temp1.Value.Name = "aureblob-indexer-new"; var res = indexer.CreateOrUpdateIndexer(temp1); //這是對 indexer 做重新建立 Console.WriteLine("CreateNewIndex Result:"); Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(res)); }


看一下是不是 Azure 後臺是布是有出現建立的索引子



4.之後就是搜尋 azureblob-index-new 這樣就會出現沒有 USER994,USER995,USER996 的答案了


簡單說一下為何要這樣作,因為在製作索引的時候需要耗費時間,這樣透過這方式你可以設計線在切換到哪一個新索引,透過切換的方式讓客戶使用最新的索引

透過這技巧來作到讓客戶無感作索引的時間。


當麻許的碎念筆記 2014 | Donma Hsu Design.