/
SearchIndexClient.cs
1366 lines (1287 loc) · 67.7 KB
/
SearchIndexClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Core.Serialization;
using Azure.Search.Documents.Indexes.Models;
namespace Azure.Search.Documents.Indexes
{
/// <summary>
/// Azure Cognitive Search client that can be used to manage indexes on a Search service.
/// </summary>
public partial class SearchIndexClient
{
private readonly HttpPipeline _pipeline;
private readonly ClientDiagnostics _clientDiagnostics;
private readonly SearchClientOptions.ServiceVersion _version;
private readonly ObjectSerializer _serializer;
private SearchServiceRestClient _serviceClient;
private IndexesRestClient _indexesClient;
private SynonymMapsRestClient _synonymMapsClient;
private string _serviceName;
/// <summary>
/// The HTTP pipeline for sending and receiving REST requests and responses.
/// </summary>
public virtual HttpPipeline Pipeline => _pipeline;
/// <summary>
/// Gets the URI endpoint of the Search service. This is likely
/// to be similar to "https://{search_service}.search.windows.net".
/// </summary>
public virtual Uri Endpoint { get; }
/// <summary>
/// Gets the name of the Search service.
/// </summary>
public virtual string ServiceName =>
_serviceName ??= Endpoint.GetSearchServiceName();
/// <summary>
/// Initializes a new instance of the <see cref="SearchIndexClient"/> class for mocking.
/// </summary>
protected SearchIndexClient() { }
/// <summary>
/// Initializes a new instance of the <see cref="SearchIndexClient"/> class.
/// </summary>
/// <param name="endpoint">Required. The URI endpoint of the Search service. This is likely to be similar to "https://{search_service}.search.windows.net". The URI must use HTTPS.</param>
/// <param name="credential">
/// Required. The API key credential used to authenticate requests against the Search service.
/// You need to use an admin key to perform any operations on the SearchIndexClient.
/// See <see href="https://docs.microsoft.com/azure/search/search-security-api-keys">Create and manage api-keys for an Azure Cognitive Search service</see> for more information about API keys in Azure Cognitive Search.
/// </param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="endpoint"/> or <paramref name="credential"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="endpoint"/> is not using HTTPS.</exception>
public SearchIndexClient(Uri endpoint, AzureKeyCredential credential) :
this(endpoint, credential, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SearchIndexClient"/> class.
/// </summary>
/// <param name="endpoint">Required. The URI endpoint of the Search service. This is likely to be similar to "https://{search_service}.search.windows.net". The URI must use HTTPS.</param>
/// <param name="tokenCredential">
/// Required. The token credential used to authenticate requests against the Search service.
/// See <see href="https://docs.microsoft.com/azure/search/search-security-rbac">Use role-based authorization in Azure Cognitive Search</see> for more information about role-based authorization in Azure Cognitive Search.
/// </param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="endpoint"/> or <paramref name="tokenCredential"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="endpoint"/> is not using HTTPS.</exception>
public SearchIndexClient(Uri endpoint, TokenCredential tokenCredential) :
this(endpoint, tokenCredential, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SearchIndexClient"/> class.
/// </summary>
/// <param name="endpoint">Required. The URI endpoint of the Search service. This is likely to be similar to "https://{search_service}.search.windows.net". The URI must use HTTPS.</param>
/// <param name="credential">
/// Required. The API key credential used to authenticate requests against the Search service.
/// You need to use an admin key to perform any operations on the SearchIndexClient.
/// See <see href="https://docs.microsoft.com/azure/search/search-security-api-keys">Create and manage api-keys for an Azure Cognitive Search service</see> for more information about API keys in Azure Cognitive Search.
/// </param>
/// <param name="options">Client configuration options for connecting to Azure Cognitive Search.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="endpoint"/> or <paramref name="credential"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="endpoint"/> is not using HTTPS.</exception>
public SearchIndexClient(
Uri endpoint,
AzureKeyCredential credential,
SearchClientOptions options)
{
Argument.AssertNotNull(endpoint, nameof(endpoint));
endpoint.AssertHttpsScheme(nameof(endpoint));
Argument.AssertNotNull(credential, nameof(credential));
options ??= new SearchClientOptions();
Endpoint = endpoint;
_serializer = options.Serializer;
_clientDiagnostics = new ClientDiagnostics(options);
_pipeline = options.Build(credential);
_version = options.Version;
}
/// <summary>
/// Initializes a new instance of the <see cref="SearchIndexClient"/> class.
/// </summary>
/// <param name="endpoint">Required. The URI endpoint of the Search service. This is likely to be similar to "https://{search_service}.search.windows.net". The URI must use HTTPS.</param>
/// <param name="tokenCredential">
/// Required. The token credential used to authenticate requests against the Search service.
/// See <see href="https://docs.microsoft.com/azure/search/search-security-rbac">Use role-based authorization in Azure Cognitive Search</see> for more information about role-based authorization in Azure Cognitive Search.
/// </param>
/// <param name="options">Client configuration options for connecting to Azure Cognitive Search.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="endpoint"/> or <paramref name="tokenCredential"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="endpoint"/> is not using HTTPS.</exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "AZC0006:DO provide constructor overloads that allow specifying additional options.", Justification = "Avoid ambiguous method definition")]
public SearchIndexClient(
Uri endpoint,
TokenCredential tokenCredential,
SearchClientOptions options)
{
Argument.AssertNotNull(endpoint, nameof(endpoint));
endpoint.AssertHttpsScheme(nameof(endpoint));
Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));
options ??= new SearchClientOptions();
Endpoint = endpoint;
_serializer = options.Serializer;
_clientDiagnostics = new ClientDiagnostics(options);
_pipeline = options.Build(tokenCredential);
_version = options.Version;
}
/// <summary>
/// Initializes a new instance of the <see cref="SearchIndexClient"/> class.
/// </summary>
/// <param name="endpoint">Required. The URI endpoint of the Search service. This is likely to be similar to "https://{search_service}.search.windows.net". The URI must use HTTPS.</param>
/// <param name="serializer">An optional customized serializer to use for search documents.</param>
/// <param name="pipeline">The authenticated <see cref="HttpPipeline"/> used for sending requests to the Search Service.</param>
/// <param name="diagnostics">The <see cref="Azure.Core.Pipeline.ClientDiagnostics"/> used to provide tracing support for the client library.</param>
/// <param name="version">The REST API version of the Search Service to use when making requests.</param>
internal SearchIndexClient(
Uri endpoint,
ObjectSerializer serializer,
HttpPipeline pipeline,
ClientDiagnostics diagnostics,
SearchClientOptions.ServiceVersion version)
{
Debug.Assert(endpoint != null);
Debug.Assert(string.Equals(endpoint.Scheme, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase));
Debug.Assert(pipeline != null);
Debug.Assert(diagnostics != null);
Debug.Assert(
SearchClientOptions.ServiceVersion.V2020_06_30 <= version &&
version <= SearchClientOptions.LatestVersion);
Endpoint = endpoint;
_serializer = serializer;
_clientDiagnostics = diagnostics;
_pipeline = pipeline;
_version = version;
}
/// <summary>
/// Gets the generated <see cref="SearchServiceRestClient"/> to make requests.
/// </summary>
private SearchServiceRestClient ServiceClient => LazyInitializer.EnsureInitialized(ref _serviceClient, () => new SearchServiceRestClient(
_clientDiagnostics,
_pipeline,
Endpoint.AbsoluteUri,
null,
_version.ToVersionString())
);
/// <summary>
/// Gets the generated <see cref="IndexesRestClient"/> to make requests.
/// </summary>
private IndexesRestClient IndexesClient => LazyInitializer.EnsureInitialized(ref _indexesClient, () => new IndexesRestClient(
_clientDiagnostics,
_pipeline,
Endpoint.AbsoluteUri,
null,
_version.ToVersionString())
);
/// <summary>
/// Gets the generated <see cref="SynonymMapsRestClient"/> to make requests.
/// </summary>
private SynonymMapsRestClient SynonymMapsClient => LazyInitializer.EnsureInitialized(ref _synonymMapsClient, () => new SynonymMapsRestClient(
_clientDiagnostics,
_pipeline,
Endpoint.AbsoluteUri,
null,
_version.ToVersionString())
);
/// <summary>
/// Get a <see cref="SearchClient"/> for the given <paramref name="indexName"/> to use for document operations like querying or adding documents to a Search Index.
/// </summary>
/// <param name="indexName">The name of the desired Search Index.</param>
/// <returns>A SearchClient for the desired Search Index.</returns>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="indexName"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="indexName"/> is empty.</exception>
/// <remarks>
/// The same request <see cref="HttpPipeline"/> (including authentication and any other configuration) will be used for the
/// <see cref="SearchClient"/>.
/// </remarks>
public virtual SearchClient GetSearchClient(string indexName)
{
Argument.AssertNotNullOrEmpty(indexName, nameof(indexName));
return new SearchClient(
Endpoint,
indexName,
_serializer,
_pipeline,
_clientDiagnostics,
_version);
}
#region Service operations
/// <summary>
/// <para>
/// Gets service level statistics for a Search service.
/// </para>
/// <para>
/// This operation returns the number and type of objects in your
/// service, the maximum allowed for each object type given the service
/// tier, actual and maximum storage, and other limits that vary by
/// tier. This request pulls information from the service so that you
/// don't have to look up or calculate service limits.
/// </para>
/// <para>
/// Statistics on document count and storage size are collected every
/// few minutes, not in real time. Therefore, the statistics returned
/// by this API may not reflect changes caused by recent indexing
/// operations.
/// </para>
/// </summary>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response{T}"/> from the server containing <see cref="SearchServiceStatistics"/>.</returns>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual Response<SearchServiceStatistics> GetServiceStatistics(
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(GetServiceStatistics)}");
scope.Start();
try
{
return ServiceClient.GetServiceStatistics(
cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// <para>
/// Gets service level statistics for a Search service.
/// </para>
/// <para>
/// This operation returns the number and type of objects in your
/// service, the maximum allowed for each object type given the service
/// tier, actual and maximum storage, and other limits that vary by
/// tier. This request pulls information from the service so that you
/// don't have to look up or calculate service limits.
/// </para>
/// <para>
/// Statistics on document count and storage size are collected every
/// few minutes, not in real time. Therefore, the statistics returned
/// by this API may not reflect changes caused by recent indexing
/// operations.
/// </para>
/// </summary>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response{T}"/> from the server containing <see cref="SearchServiceStatistics"/>.</returns>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual async Task<Response<SearchServiceStatistics>> GetServiceStatisticsAsync(
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(GetServiceStatistics)}");
scope.Start();
try
{
return await ServiceClient.GetServiceStatisticsAsync(
cancellationToken)
.ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
#endregion
#region Index operations
/// <summary>
/// Shows how an analyzer breaks text into tokens.
/// </summary>
/// <param name="indexName">The name of the index used to test an analyzer.</param>
/// <param name="options">The <see cref="AnalyzeTextOptions"/> containing the text and analyzer or analyzer components to test.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Response{T}"/> from the server containing a list of <see cref="AnalyzedTokenInfo"/> for analyzed text.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="indexName"/> or <paramref name="options"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual Response<IReadOnlyList<AnalyzedTokenInfo>> AnalyzeText(
string indexName,
AnalyzeTextOptions options,
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(AnalyzeText)}");
scope.Start();
try
{
Response<AnalyzeResult> result = IndexesClient.Analyze(
indexName,
options,
cancellationToken);
return Response.FromValue(result.Value.Tokens, result.GetRawResponse());
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Shows how an analyzer breaks text into tokens.
/// </summary>
/// <param name="indexName">The name of the index used to test an analyzer.</param>
/// <param name="options">The <see cref="AnalyzeTextOptions"/> containing the text and analyzer or analyzer components to test.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Response{T}"/> from the server containing a list of <see cref="AnalyzedTokenInfo"/> for analyzed text.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="indexName"/> or <paramref name="options"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual async Task<Response<IReadOnlyList<AnalyzedTokenInfo>>> AnalyzeTextAsync(
string indexName,
AnalyzeTextOptions options,
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(AnalyzeText)}");
scope.Start();
try
{
Response<AnalyzeResult> result = await IndexesClient.AnalyzeAsync(
indexName,
options,
cancellationToken)
.ConfigureAwait(false);
return Response.FromValue(result.Value.Tokens, result.GetRawResponse());
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Creates a new search index.
/// </summary>
/// <param name="index">Required. The <see cref="SearchIndex"/> to create.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Response{T}"/> from the server containing the <see cref="SearchIndex"/> that was created.
/// This may differ slightly from what was passed in since the service may return back fields set to their default values depending on the field type and other properties.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="index"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual Response<SearchIndex> CreateIndex(
SearchIndex index,
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(CreateIndex)}");
scope.Start();
try
{
return IndexesClient.Create(
index,
cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Creates a new search index.
/// </summary>
/// <param name="index">Required. The <see cref="SearchIndex"/> to create.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Response{T}"/> from the server containing the <see cref="SearchIndex"/> that was created.
/// This may differ slightly from what was passed in since the service may return back fields set to their default values depending on the field type and other properties.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="index"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual async Task<Response<SearchIndex>> CreateIndexAsync(
SearchIndex index,
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(CreateIndex)}");
scope.Start();
try
{
return await IndexesClient.CreateAsync(
index,
cancellationToken)
.ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Creates a new search index or updates an existing index.
/// </summary>
/// <param name="index">Required. The <see cref="SearchIndex"/> to create or update.</param>
/// <param name="allowIndexDowntime">
/// Optional value indicating whether to allow analyzers, tokenizers, token filters, or character filters to be added to the index by temporarily taking the index
/// offline for a few seconds. The default is false. This temporarily causes indexing and queries to fail.
/// Performance and write availability of the index can be impaired for several minutes after the index is updated, or longer for very large indexes.
/// </param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SearchIndex.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Response{T}"/> from the server containing the <see cref="SearchIndex"/> that was created or updated.
/// This may differ slightly from what was passed in since the service may return back fields set to their default values depending on the field type and other properties.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="index"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual Response<SearchIndex> CreateOrUpdateIndex(
SearchIndex index,
bool allowIndexDowntime = false,
bool onlyIfUnchanged = false,
CancellationToken cancellationToken = default)
{
// Generated client validates indexName parameter first, which is not a parameter of this method.
Argument.AssertNotNull(index, nameof(index));
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(CreateOrUpdateIndex)}");
scope.Start();
try
{
return IndexesClient.CreateOrUpdate(
index?.Name,
index,
allowIndexDowntime,
onlyIfUnchanged ? index?.ETag?.ToString() : null,
null,
cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Creates a new search index or updates an existing index.
/// </summary>
/// <param name="index">Required. The <see cref="SearchIndex"/> to create or update.</param>
/// <param name="allowIndexDowntime">
/// Optional value indicating whether to allow analyzers, tokenizers, token filters, or character filters to be added to the index by temporarily taking the index
/// offline for a few seconds. The default is false. This temporarily causes indexing and queries to fail.
/// Performance and write availability of the index can be impaired for several minutes after the index is updated, or longer for very large indexes.
/// </param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SearchIndex.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Response{T}"/> from the server containing the <see cref="SearchIndex"/> that was created or updated.
/// This may differ slightly from what was passed in since the service may return back fields set to their default values depending on the field type and other properties.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="index"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual async Task<Response<SearchIndex>> CreateOrUpdateIndexAsync(
SearchIndex index,
bool allowIndexDowntime = false,
bool onlyIfUnchanged = false,
CancellationToken cancellationToken = default)
{
// Generated client validates indexName parameter first, which is not a parameter of this method.
Argument.AssertNotNull(index, nameof(index));
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(CreateOrUpdateIndex)}");
scope.Start();
try
{
return await IndexesClient.CreateOrUpdateAsync(
index?.Name,
index,
allowIndexDowntime,
onlyIfUnchanged ? index?.ETag?.ToString() : null,
null,
cancellationToken)
.ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Deletes a search index and all the documents it contains.
/// </summary>
/// <param name="indexName">Required. The name of the <see cref="SearchIndex"/> to delete.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response"/> from the server.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="indexName"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual Response DeleteIndex(
string indexName,
CancellationToken cancellationToken = default) => DeleteIndex(
indexName,
null,
false,
cancellationToken);
/// <summary>
/// Deletes a search index and all the documents it contains.
/// </summary>
/// <param name="indexName">Required. The name of the <see cref="SearchIndex"/> to delete.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response"/> from the server.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="indexName"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual async Task<Response> DeleteIndexAsync(
string indexName,
CancellationToken cancellationToken = default) => await DeleteIndexAsync(
indexName,
null,
false,
cancellationToken)
.ConfigureAwait(false);
/// <summary>
/// Deletes a search index and all the documents it contains.
/// </summary>
/// <param name="index">Required. The <see cref="SearchIndex"/> to delete.</param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SearchIndex.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response"/> from the server.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="index"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual Response DeleteIndex(
SearchIndex index,
bool onlyIfUnchanged = false,
CancellationToken cancellationToken = default)
{
// Generated client validates indexName parameter first, which is not a parameter of this method.
Argument.AssertNotNull(index, nameof(index));
return DeleteIndex(
index?.Name,
index?.ETag,
onlyIfUnchanged,
cancellationToken);
}
/// <summary>
/// Deletes a search index and all the documents it contains.
/// </summary>
/// <param name="index">Required. The <see cref="SearchIndex"/> to delete.</param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SearchIndex.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response"/> from the server.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="index"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual async Task<Response> DeleteIndexAsync(
SearchIndex index,
bool onlyIfUnchanged = false,
CancellationToken cancellationToken = default)
{
// Generated client validates indexName parameter first, which is not a parameter of this method.
Argument.AssertNotNull(index, nameof(index));
return await DeleteIndexAsync(
index?.Name,
index?.ETag,
onlyIfUnchanged,
cancellationToken)
.ConfigureAwait(false);
}
private Response DeleteIndex(
string indexName,
ETag? etag,
bool onlyIfUnchanged,
CancellationToken cancellationToken)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(DeleteIndex)}");
scope.Start();
try
{
return IndexesClient.Delete(
indexName,
onlyIfUnchanged ? etag?.ToString() : null,
null,
cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
private async Task<Response> DeleteIndexAsync(
string indexName,
ETag? etag,
bool onlyIfUnchanged,
CancellationToken cancellationToken)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(DeleteIndex)}");
scope.Start();
try
{
return await IndexesClient.DeleteAsync(
indexName,
onlyIfUnchanged ? etag?.ToString() : null,
null,
cancellationToken)
.ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Gets a specific <see cref="SearchIndex"/>.
/// </summary>
/// <param name="indexName">Required. The name of the index to get.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response{T}"/> from the server containing the requested <see cref="SearchIndex"/>.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="indexName"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual Response<SearchIndex> GetIndex(
string indexName,
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(GetIndex)}");
scope.Start();
try
{
return IndexesClient.Get(
indexName,
cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Gets a specific <see cref="SearchIndex"/>.
/// </summary>
/// <param name="indexName">Required. The name of the index to get.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response{T}"/> from the server containing the requested <see cref="SearchIndex"/>.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="indexName"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual async Task<Response<SearchIndex>> GetIndexAsync(
string indexName,
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(GetIndex)}");
scope.Start();
try
{
return await IndexesClient.GetAsync(
indexName,
cancellationToken)
.ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Gets a list of all indexes.
/// </summary>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Pageable{T}"/> from the server containing a list of <see cref="SearchIndex"/>.</returns>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual Pageable<SearchIndex> GetIndexes(
CancellationToken cancellationToken = default)
{
return PageResponseEnumerator.CreateEnumerable((continuationToken) =>
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(GetIndexes)}");
scope.Start();
try
{
if (continuationToken != null)
{
throw new NotSupportedException("A continuation token is unsupported.");
}
Response<ListIndexesResult> result = IndexesClient.List(
Constants.All,
cancellationToken);
return Page<SearchIndex>.FromValues(result.Value.Indexes, null, result.GetRawResponse());
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
});
}
/// <summary>
/// Gets a list of all indexes.
/// </summary>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response{T}"/> from the server containing a list of <see cref="SearchIndex"/>.</returns>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual AsyncPageable<SearchIndex> GetIndexesAsync(
CancellationToken cancellationToken = default)
{
return PageResponseEnumerator.CreateAsyncEnumerable(async (continuationToken) =>
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(GetIndexes)}");
scope.Start();
try
{
if (continuationToken != null)
{
throw new NotSupportedException("A continuation token is unsupported.");
}
Response<ListIndexesResult> result = await IndexesClient.ListAsync(
Constants.All,
cancellationToken)
.ConfigureAwait(false);
return Page<SearchIndex>.FromValues(result.Value.Indexes, null, result.GetRawResponse());
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
});
}
/// <summary>
/// Gets a list of all index names.
/// </summary>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Pageable{T}"/> from the server containing a list of <see cref="SearchIndex"/> names.</returns>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual Pageable<string> GetIndexNames(
CancellationToken cancellationToken = default)
{
return PageResponseEnumerator.CreateEnumerable((continuationToken) =>
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(GetIndexNames)}");
scope.Start();
try
{
if (continuationToken != null)
{
throw new NotSupportedException("A continuation token is unsupported.");
}
Response<ListIndexesResult> result = IndexesClient.List(
Constants.NameKey,
cancellationToken);
IReadOnlyList<string> names = result.Value.Indexes.Select(value => value.Name).ToArray();
return Page<string>.FromValues(names, null, result.GetRawResponse());
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
});
}
/// <summary>
/// Gets a list of all index names.
/// </summary>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response{T}"/> from the server containing a list of <see cref="SearchIndex"/> names.</returns>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual AsyncPageable<string> GetIndexNamesAsync(
CancellationToken cancellationToken = default)
{
return PageResponseEnumerator.CreateAsyncEnumerable(async (continuationToken) =>
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(GetIndexNames)}");
scope.Start();
try
{
if (continuationToken != null)
{
throw new NotSupportedException("A continuation token is unsupported.");
}
Response<ListIndexesResult> result = await IndexesClient.ListAsync(
Constants.NameKey,
cancellationToken)
.ConfigureAwait(false);
IReadOnlyList<string> names = result.Value.Indexes.Select(value => value.Name).ToArray();
return Page<string>.FromValues(names, null, result.GetRawResponse());
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
});
}
/// <summary>
/// Gets <see cref="SearchIndexStatistics"/> for the given index, including a document count and storage usage.
/// </summary>
/// <param name="indexName">Required. The name of the index.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response{T}"/> from the server containing <see cref="SearchIndexStatistics"/> names.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="indexName"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual Response<SearchIndexStatistics> GetIndexStatistics(
string indexName,
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(GetIndexStatistics)}");
scope.Start();
try
{
return IndexesClient.GetStatistics(
indexName,
cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Gets <see cref="SearchIndexStatistics"/> for the given index, including a document count and storage usage.
/// </summary>
/// <param name="indexName">Required. The name of the index.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Response{T}"/> from the server containing <see cref="SearchIndexStatistics"/>.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="indexName"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual async Task<Response<SearchIndexStatistics>> GetIndexStatisticsAsync(
string indexName,
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(GetIndexStatistics)}");
scope.Start();
try
{
return await IndexesClient.GetStatisticsAsync(
indexName,
cancellationToken)
.ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
#endregion
#region SynonymMaps operations
/// <summary>
/// Creates a new synonym map.
/// </summary>
/// <param name="synonymMap">Required. The <see cref="SynonymMap"/> to create.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Response{T}"/> from the server containing the <see cref="SynonymMap"/> that was created.
/// This may differ slightly from what was passed in since the service may return back properties set to their default values.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="synonymMap"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual Response<SynonymMap> CreateSynonymMap(
SynonymMap synonymMap,
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(CreateSynonymMap)}");
scope.Start();
try
{
return SynonymMapsClient.Create(
synonymMap,
cancellationToken);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Creates a new synonym map.
/// </summary>
/// <param name="synonymMap">Required. The <see cref="SynonymMap"/> to create.</param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Response{T}"/> from the server containing the <see cref="SynonymMap"/> that was created.
/// This may differ slightly from what was passed in since the service may return back properties set to their default values.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="synonymMap"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual async Task<Response<SynonymMap>> CreateSynonymMapAsync(
SynonymMap synonymMap,
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SearchIndexClient)}.{nameof(CreateSynonymMap)}");
scope.Start();
try
{
return await SynonymMapsClient.CreateAsync(
synonymMap,
cancellationToken)
.ConfigureAwait(false);
}
catch (Exception ex)
{
scope.Failed(ex);
throw;
}
}
/// <summary>
/// Creates a new synonym map or updates an existing synonym map.
/// </summary>
/// <param name="synonymMap">Required. The <see cref="SynonymMap"/> to create or update.</param>
/// <param name="onlyIfUnchanged">
/// True to throw a <see cref="RequestFailedException"/> if the <see cref="SynonymMap.ETag"/> does not match the current service version;
/// otherwise, the current service version will be overwritten.
/// </param>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/> to propagate notifications that the operation should be canceled.</param>
/// <returns>
/// The <see cref="Response{T}"/> from the server containing the <see cref="SynonymMap"/> that was created.
/// This may differ slightly from what was passed in since the service may return back properties set to their default values.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="synonymMap"/> is null.</exception>
/// <exception cref="RequestFailedException">Thrown when a failure is returned by the Search service.</exception>
public virtual Response<SynonymMap> CreateOrUpdateSynonymMap(
SynonymMap synonymMap,