77using System . Linq ;
88using System . Net . Http ;
99using System . Threading ;
10- using Microsoft . Azure . Search ;
11- using Microsoft . Azure . Search . Models ;
12- using Newtonsoft . Json ;
1310using Microsoft . Graph ;
11+ using Azure . Search . Documents ;
12+ using Azure . Search . Documents . Indexes ;
13+ using Azure ;
14+ using Azure . Search . Documents . Models ;
15+ using Azure . Search . Documents . Indexes . Models ;
1416
1517namespace DotNetHowToSecurityTrimming
1618{
1719 static class Program
1820 {
1921 public static string ClientId ;
2022
21- private static ISearchServiceClient _searchClient ;
22- private static ISearchIndexClient _indexClient ;
23+ private static SearchClient searchClient ;
24+ private static SearchIndexClient indexClient ;
2325
2426 private static ConcurrentDictionary < string , List < string > > _groupsCache = new ConcurrentDictionary < string , List < string > > ( ) ;
2527 private static MicrosoftGraphHelper _microsoftGraphHelper ;
@@ -33,9 +35,9 @@ static void Main(string[] args)
3335 _microsoftGraphHelper = new MicrosoftGraphHelper ( ClientId ) ;
3436 _microsoftGraphHelper . CreateGraphServiceClient ( ) ;
3537 string tenant = ConfigurationManager . AppSettings [ "Tenant" ] ;
36-
38+
3739 // Azure Search Initialization
38- string searchServiceName = ConfigurationManager . AppSettings [ "SearchServiceName " ] ;
40+ string searchServicEndpoint = ConfigurationManager . AppSettings [ "SearchServicEndpoint " ] ;
3941 string apiKey = ConfigurationManager . AppSettings [ "SearchServiceApiKey" ] ;
4042 string indexName = "securedfiles" ;
4143
@@ -50,8 +52,8 @@ static void Main(string[] args)
5052 RefreshCache ( users ) ;
5153
5254 // Create an HTTP reference to the catalog index
53- _searchClient = new SearchServiceClient ( searchServiceName , new SearchCredentials ( apiKey ) ) ;
54- _indexClient = new SearchIndexClient ( searchServiceName , indexName , new SearchCredentials ( apiKey ) ) ;
55+ indexClient = new SearchIndexClient ( new Uri ( searchServicEndpoint ) , new AzureKeyCredential ( apiKey ) ) ;
56+ searchClient = indexClient . GetSearchClient ( indexName ) ;
5557
5658 if ( DeleteIndex ( indexName ) )
5759 {
@@ -138,7 +140,7 @@ private static async void RefreshCacheIfRequired(string user)
138140 _groupsCache [ user ] = groups ;
139141 }
140142 }
141-
143+
142144 private static async void RefreshCache ( IEnumerable < User > users )
143145 {
144146 HttpClient client = new HttpClient ( ) ;
@@ -151,59 +153,53 @@ private static void SearchQueryWithFilter(string user)
151153 // Using the filter below, the search result will contain all documents that their GroupIds field contain any one of the
152154 // Ids in the groups list
153155 string filter = String . Format ( "groupIds/any(p:search.in(p, '{0}'))" , string . Join ( "," , String . Join ( "," , _groupsCache [ user ] ) ) ) ;
154- SearchParameters parameters =
155- new SearchParameters ( )
156+ SearchOptions searchOptions =
157+ new SearchOptions ( )
156158 {
157- Filter = filter ,
158- Select = new [ ] { "name" }
159+ Filter = filter
159160 } ;
161+ searchOptions . Select . Add ( "name" ) ;
160162
161- DocumentSearchResult < SecuredFiles > results = _indexClient . Documents . Search < SecuredFiles > ( "*" , parameters ) ;
163+ SearchResults < SecuredFiles > results = searchClient . Search < SecuredFiles > ( "*" , searchOptions ) ;
162164
163- Console . WriteLine ( "Results for groups '{0}' : {1}" , _groupsCache [ user ] , results . Results . Select ( r => r . Document . Name ) ) ;
165+ Console . WriteLine ( "Results for groups '{0}' : {1}" , _groupsCache [ user ] , results . GetResults ( ) . Select ( r => r . Document . Name ) ) ;
164166 }
165167
166168 private static void IndexDocuments ( string indexName , List < string > groups )
167169 {
168- var actions = new IndexAction < SecuredFiles > [ ]
169- {
170- IndexAction . Upload (
171- new SecuredFiles ( )
172- {
173- FileId = "1" ,
174- Name = "secured_file_a" ,
175- GroupIds = new [ ] { groups [ 0 ] }
176- } ) ,
177- IndexAction . Upload (
178- new SecuredFiles ( )
179- {
180- FileId = "2" ,
181- Name = "secured_file_b" ,
182- GroupIds = new [ ] { groups [ 0 ] }
183- } ) ,
184- IndexAction . Upload (
185- new SecuredFiles ( )
186- {
187- FileId = "3" ,
188- Name = "secured_file_c" ,
189- GroupIds = new [ ] { groups [ 1 ] }
190- } )
191- } ;
192-
193- var batch = IndexBatch . New ( actions ) ;
170+ IndexDocumentsBatch < SecuredFiles > batch = IndexDocumentsBatch . Create (
171+ IndexDocumentsAction . Upload (
172+ new SecuredFiles ( )
173+ {
174+ FileId = "1" ,
175+ Name = "secured_file_a" ,
176+ GroupIds = new [ ] { groups [ 0 ] }
177+ } ) ,
178+ IndexDocumentsAction . Upload (
179+ new SecuredFiles ( )
180+ {
181+ FileId = "2" ,
182+ Name = "secured_file_b" ,
183+ GroupIds = new [ ] { groups [ 0 ] }
184+ } ) ,
185+ IndexDocumentsAction . Upload (
186+ new SecuredFiles ( )
187+ {
188+ FileId = "3" ,
189+ Name = "secured_file_c" ,
190+ GroupIds = new [ ] { groups [ 1 ] }
191+ } ) ) ;
194192
195193 try
196194 {
197- _indexClient . Documents . Index ( batch ) ;
195+ IndexDocumentsResult result = searchClient . IndexDocuments ( batch ) ;
198196 }
199- catch ( IndexBatchException e )
197+ catch ( Exception )
200198 {
201199 // Sometimes when your Search service is under load, indexing will fail for some of the documents in
202200 // the batch. Depending on your application, you can take compensating actions like delaying and
203201 // retrying. For this simple demo, we just log the failed document keys and continue.
204- Console . WriteLine (
205- "Failed to index some of the documents: {0}" ,
206- String . Join ( ", " , e . IndexingResults . Where ( r => ! r . Succeeded ) . Select ( r => r . Key ) ) ) ;
202+ Console . WriteLine ( "Failed to index some of the documents: {0}" ) ;
207203 }
208204
209205 Console . WriteLine ( "Waiting for documents to be indexed...\n " ) ;
@@ -214,12 +210,12 @@ private static bool DeleteIndex(string indexName)
214210 {
215211 try
216212 {
217- _searchClient . Indexes . Delete ( indexName ) ;
213+ indexClient . DeleteIndex ( indexName ) ;
218214 }
219215 catch ( Exception ex )
220216 {
221217 Console . WriteLine ( "Error deleting index: {0}\r \n " , ex . Message ) ;
222- Console . WriteLine ( "Did you remember to add your SearchServiceName and SearchServiceApiKey to the app.config?\r \n " ) ;
218+ Console . WriteLine ( "Did you remember to add your SearchServicEndpoint and SearchServiceApiKey to the app.config?\r \n " ) ;
223219 return false ;
224220 }
225221
@@ -231,13 +227,12 @@ private static void CreateIndex(string indexName)
231227 // Create the Azure Search index based on the included schema
232228 try
233229 {
234- var definition = new Index ( )
235- {
236- Name = indexName ,
237- Fields = FieldBuilder . BuildForType < SecuredFiles > ( )
238- } ;
230+ FieldBuilder fieldBuilder = new FieldBuilder ( ) ;
231+ var searchFields = fieldBuilder . Build ( typeof ( SecuredFiles ) ) ;
232+ var definition = new SearchIndex ( indexName , searchFields ) ;
233+
234+ indexClient . CreateOrUpdateIndex ( definition ) ;
239235
240- _searchClient . Indexes . Create ( definition ) ;
241236 }
242237 catch ( Exception ex )
243238 {
0 commit comments