1- namespace AzureSearch . SDKHowTo
1+ using Azure ;
2+ using Azure . Search . Documents . Indexes ;
3+ using Azure . Search . Documents . Indexes . Models ;
4+ using Microsoft . Extensions . Configuration ;
5+ using System ;
6+ using System . Threading . Tasks ;
7+
8+ namespace AzureSearch . SDKHowTo
29{
3- using System ;
4- using Microsoft . Azure . Search ;
5- using Microsoft . Azure . Search . Models ;
6- using Microsoft . Extensions . Configuration ;
7- using Microsoft . Rest . Azure ;
8-
910 class Program
1011 {
1112 // This sample shows how ETags work by performing conditional updates and deletes
1213 // on an Azure Search index.
13- static void Main ( string [ ] args )
14+ static async Task Main ( string [ ] args )
1415 {
1516 IConfigurationBuilder builder = new ConfigurationBuilder ( ) . AddJsonFile ( "appsettings.json" ) ;
1617 IConfigurationRoot configuration = builder . Build ( ) ;
1718
18- SearchServiceClient serviceClient = CreateSearchServiceClient ( configuration ) ;
19+ SearchIndexClient indexClient = CreateSearchIndexClient ( configuration ) ;
1920
2021 Console . WriteLine ( "Deleting index...\n " ) ;
21- DeleteTestIndexIfExists ( serviceClient ) ;
22+ DeleteTestIndexIfExists ( indexClient ) ;
2223
2324 // Every top-level resource in Azure Search has an associated ETag that keeps track of which version
2425 // of the resource you're working on. When you first create a resource such as an index, its ETag is
2526 // empty.
26- Index index = DefineTestIndex ( ) ;
27+ SearchIndex index = DefineTestIndex ( ) ;
28+
2729 Console . WriteLine (
28- $ "Test index hasn't been created yet, so its ETag should be blank. ETag: '{ index . ETag } '") ;
30+ $ "Test searchIndex hasn't been created yet, so its ETag should be blank. ETag: '{ index . ETag } '") ;
2931
3032 // Once the resource exists in Azure Search, its ETag will be populated. Make sure to use the object
31- // returned by the SearchServiceClient ! Otherwise, you will still have the old object with the
33+ // returned by the SearchIndexClient ! Otherwise, you will still have the old object with the
3234 // blank ETag.
33- Console . WriteLine ( "Creating index...\n " ) ;
34- index = serviceClient . Indexes . Create ( index ) ;
35-
35+ //Console.WriteLine("Creating index...\n");
36+ index = indexClient . CreateIndex ( index ) ;
3637 Console . WriteLine ( $ "Test index created; Its ETag should be populated. ETag: '{ index . ETag } '") ;
3738
39+
3840 // ETags let you do some useful things you couldn't do otherwise. For example, by using an If-Match
39- // condition, we can update an index using CreateOrUpdate and be guaranteed that the update will only
41+ // condition, we can update an index using CreateOrUpdateIndexAsync() and be guaranteed that the update will only
4042 // succeed if the index already exists.
41- index . Fields . Add ( new Field ( "name" , AnalyzerName . EnMicrosoft ) ) ;
42- index =
43- serviceClient . Indexes . CreateOrUpdate (
44- index ,
45- accessCondition : AccessCondition . GenerateIfExistsCondition ( ) ) ;
43+ index . Fields . Add ( new SearchField ( "name" , SearchFieldDataType . String ) { AnalyzerName = LexicalAnalyzerName . EnMicrosoft } ) ;
44+ index = indexClient . CreateOrUpdateIndex ( index ) ;
4645
46+ index = await indexClient . CreateOrUpdateIndexAsync ( index ) ;
4747 Console . WriteLine (
48- $ "Test index updated; Its ETag should have changed since it was created. ETag: '{ index . ETag } '") ;
48+ $ "Test searchIndex updated; Its ETag should have changed since it was created. ETag: '{ index . ETag } '") ;
4949
5050 // More importantly, ETags protect you from concurrent updates to the same resource. If another
5151 // client tries to update the resource, it will fail as long as all clients are using the right
5252 // access conditions.
53- Index indexForClient1 = index ;
54- Index indexForClient2 = serviceClient . Indexes . Get ( "test" ) ;
53+ SearchIndex indexForClientUpdate = index ;
54+ SearchIndex indexForClientUpdateFailed = indexClient . GetIndex ( "test" ) ;
5555
5656 Console . WriteLine ( "Simulating concurrent update. To start, both clients see the same ETag." ) ;
57- Console . WriteLine ( $ "Client 1 ETag: '{ indexForClient1 . ETag } ' Client 2 ETag: '{ indexForClient2 . ETag } '") ;
57+ Console . WriteLine ( $ "ClientUpdate ETag: '{ indexForClientUpdate . ETag } ' ClientUpdateFailed ETag: '{ indexForClientUpdateFailed . ETag } '") ;
5858
59- // Client 1 successfully updates the index.
60- indexForClient1 . Fields . Add ( new Field ( "a" , DataType . Int32 ) ) ;
61- indexForClient1 =
62- serviceClient . Indexes . CreateOrUpdate (
63- indexForClient1 ,
64- accessCondition : AccessCondition . IfNotChanged ( indexForClient1 ) ) ;
59+ // indexForClientUpdate successfully updates the index.
60+ indexForClientUpdate . Fields . Add ( new SearchField ( "a" , SearchFieldDataType . Int32 ) ) ;
61+ indexForClientUpdate = indexClient . CreateOrUpdateIndex ( indexForClientUpdate ) ;
6562
66- Console . WriteLine ( $ "Test index updated by client 1 ; ETag: '{ indexForClient1 . ETag } '") ;
63+ Console . WriteLine ( $ "Test index updated by ClientUpdate ; ETag: '{ indexForClientUpdate . ETag } '") ;
6764
68- // Client 2 tries to update the index, but fails, thanks to the ETag check.
65+ // indexForClientUpdateFailed tries to update the index, but fails, thanks to the ETag check.
6966 try
7067 {
71- indexForClient2 . Fields . Add ( new Field ( "b" , DataType . Boolean ) ) ;
72- serviceClient . Indexes . CreateOrUpdate (
73- indexForClient2 ,
74- accessCondition : AccessCondition . IfNotChanged ( indexForClient2 ) ) ;
68+ indexForClientUpdateFailed . Fields . Add ( new SearchField ( "b" , SearchFieldDataType . Boolean ) ) ;
69+ indexClient . CreateOrUpdateIndex ( indexForClientUpdateFailed ) ;
7570
7671 Console . WriteLine ( "Whoops; This shouldn't happen" ) ;
7772 Environment . Exit ( 1 ) ;
7873 }
79- catch ( CloudException e ) when ( e . IsAccessConditionFailed ( ) )
74+ catch ( RequestFailedException e ) when ( e . Status == 400 )
8075 {
81- Console . WriteLine ( "Client 2 failed to update the index, as expected." ) ;
76+ Console . WriteLine ( "ClientUpdateFailed failed to update the index, as expected." ) ;
8277 }
8378
8479 // You can also use access conditions with Delete operations. For example, you can implement an
8580 // atomic version of the DeleteTestIndexIfExists method from this sample like this:
8681 Console . WriteLine ( "Deleting index...\n " ) ;
87- serviceClient . Indexes . Delete ( "test" , accessCondition : AccessCondition . GenerateIfExistsCondition ( ) ) ;
82+ indexClient . DeleteIndex ( "test" ) ;
8883
8984 // This is slightly better than using the Exists method since it makes only one round trip to
9085 // Azure Search instead of potentially two. It also avoids an extra Delete request in cases where
@@ -96,29 +91,33 @@ static void Main(string[] args)
9691 Console . ReadKey ( ) ;
9792 }
9893
99- private static SearchServiceClient CreateSearchServiceClient ( IConfigurationRoot configuration )
94+ private static SearchIndexClient CreateSearchIndexClient ( IConfigurationRoot configuration )
10095 {
101- string searchServiceName = configuration [ "SearchServiceName " ] ;
96+ string searchServicEndpoint = configuration [ "SearchServicEndpoint " ] ;
10297 string adminApiKey = configuration [ "SearchServiceAdminApiKey" ] ;
10398
104- SearchServiceClient serviceClient =
105- new SearchServiceClient ( searchServiceName , new SearchCredentials ( adminApiKey ) ) ;
106- return serviceClient ;
99+ SearchIndexClient indexClient =
100+ new SearchIndexClient ( new Uri ( searchServicEndpoint ) , new AzureKeyCredential ( adminApiKey ) ) ;
101+ return indexClient ;
107102 }
108103
109- private static void DeleteTestIndexIfExists ( SearchServiceClient serviceClient )
104+ private static void DeleteTestIndexIfExists ( SearchIndexClient indexClient )
110105 {
111- if ( serviceClient . Indexes . Exists ( "test" ) )
106+ try
107+ {
108+ if ( indexClient . GetIndex ( "test" ) != null )
109+ {
110+ indexClient . DeleteIndex ( "test" ) ;
111+ }
112+ }
113+ catch ( RequestFailedException e ) when ( e . Status == 404 )
112114 {
113- serviceClient . Indexes . Delete ( "test" ) ;
115+ //if exception occurred and status is "Not Found", this is work as expect
116+ Console . WriteLine ( "Failed to find index and this is because it's not there." ) ;
114117 }
115118 }
116119
117- private static Index DefineTestIndex ( ) =>
118- new Index ( )
119- {
120- Name = "test" ,
121- Fields = new [ ] { new Field ( "id" , DataType . String ) { IsKey = true } }
122- } ;
120+ private static SearchIndex DefineTestIndex ( ) =>
121+ new SearchIndex ( "test" , new [ ] { new SearchField ( "id" , SearchFieldDataType . String ) { IsKey = true } } ) ;
123122 }
124- }
123+ }
0 commit comments