-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
searchIndexerClient.ts
758 lines (716 loc) · 22.7 KB
/
searchIndexerClient.ts
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { KeyCredential, TokenCredential, isTokenCredential } from "@azure/core-auth";
import {
createPipelineFromOptions,
InternalPipelineOptions,
operationOptionsToRequestOptionsBase,
PipelineOptions,
RequestPolicyFactory,
bearerTokenAuthenticationPolicy
} from "@azure/core-http";
import { SpanStatusCode } from "@azure/core-tracing";
import { SDK_VERSION } from "./constants";
import { SearchIndexerStatus } from "./generated/service/models";
import { SearchServiceClient as GeneratedClient } from "./generated/service/searchServiceClient";
import { logger } from "./logger";
import { createSearchApiKeyCredentialPolicy } from "./searchApiKeyCredentialPolicy";
import {
CreateOrUpdateSkillsetOptions,
CreateSkillsetOptions,
DeleteSkillsetOptions,
GetSkillSetOptions,
ListSkillsetsOptions,
SearchIndexerSkillset,
ListIndexersOptions,
CreateIndexerOptions,
GetIndexerOptions,
CreateorUpdateIndexerOptions,
DeleteIndexerOptions,
GetIndexerStatusOptions,
ResetIndexerOptions,
RunIndexerOptions,
ListDataSourceConnectionsOptions,
SearchIndexer,
SearchIndexerDataSourceConnection,
CreateDataSourceConnectionOptions,
DeleteDataSourceConnectionOptions,
GetDataSourceConnectionOptions,
CreateorUpdateDataSourceConnectionOptions
} from "./serviceModels";
import * as utils from "./serviceUtils";
import { createSpan } from "./tracing";
import { odataMetadataPolicy } from "./odataMetadataPolicy";
/**
* Client options used to configure Cognitive Search API requests.
*/
export interface SearchIndexerClientOptions extends PipelineOptions {
/**
* The API version to use when communicating with the service.
*/
apiVersion?: string;
}
/**
* Class to perform operations to manage
* (create, update, list/delete)
* indexers, datasources & skillsets.
*/
export class SearchIndexerClient {
/**
* The API version to use when communicating with the service.
*/
public readonly apiVersion: string = "2020-06-30-Preview";
/**
* The endpoint of the search service
*/
public readonly endpoint: string;
/**
* @internal
* @hidden
* A reference to the auto-generated SearchServiceClient
*/
private readonly client: GeneratedClient;
/**
* Creates an instance of SearchIndexerClient.
*
* Example usage:
* ```ts
* const { SearchIndexerClient, AzureKeyCredential } = require("@azure/search-documents");
*
* const client = new SearchIndexerClient(
* "<endpoint>",
* new AzureKeyCredential("<Admin Key>");
* );
* ```
* @param endpoint - The endpoint of the search service
* @param credential - Used to authenticate requests to the service.
* @param options - Used to configure the Search client.
*/
constructor(
endpoint: string,
credential: KeyCredential | TokenCredential,
options: SearchIndexerClientOptions = {}
) {
this.endpoint = endpoint;
const libInfo = `azsdk-js-search-documents/${SDK_VERSION}`;
if (!options.userAgentOptions) {
options.userAgentOptions = {};
}
if (options.userAgentOptions.userAgentPrefix) {
options.userAgentOptions.userAgentPrefix = `${options.userAgentOptions.userAgentPrefix} ${libInfo}`;
} else {
options.userAgentOptions.userAgentPrefix = libInfo;
}
const internalPipelineOptions: InternalPipelineOptions = {
...options,
...{
loggingOptions: {
logger: logger.info,
allowedHeaderNames: [
"elapsed-time",
"Location",
"OData-MaxVersion",
"OData-Version",
"Prefer",
"throttle-reason"
]
}
}
};
const requestPolicyFactory: RequestPolicyFactory = isTokenCredential(credential)
? bearerTokenAuthenticationPolicy(credential, utils.DEFAULT_SEARCH_SCOPE)
: createSearchApiKeyCredentialPolicy(credential);
const pipeline = createPipelineFromOptions(internalPipelineOptions, requestPolicyFactory);
if (Array.isArray(pipeline.requestPolicyFactories)) {
pipeline.requestPolicyFactories.unshift(odataMetadataPolicy("minimal"));
}
let apiVersion = this.apiVersion;
if (options.apiVersion) {
if (!["2020-06-30", "2021-04-30-Preview"].includes(options.apiVersion)) {
throw new Error(`Invalid Api Version: ${options.apiVersion}`);
}
apiVersion = options.apiVersion;
}
this.client = new GeneratedClient(this.endpoint, apiVersion, pipeline);
}
/**
* Retrieves a list of existing indexers in the service.
* @param options - Options to the list indexers operation.
*/
public async listIndexers(options: ListIndexersOptions = {}): Promise<Array<SearchIndexer>> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-listIndexers", options);
try {
const result = await this.client.indexers.list(
operationOptionsToRequestOptionsBase(updatedOptions)
);
return result.indexers.map(utils.generatedSearchIndexerToPublicSearchIndexer);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Retrieves a list of names of existing indexers in the service.
* @param options - Options to the list indexers operation.
*/
public async listIndexersNames(options: ListIndexersOptions = {}): Promise<Array<string>> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-listIndexersNames", options);
try {
const result = await this.client.indexers.list({
...operationOptionsToRequestOptionsBase(updatedOptions),
select: "name"
});
return result.indexers.map((idx) => idx.name);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Retrieves a list of existing data sources in the service.
* @param options - Options to the list indexers operation.
*/
public async listDataSourceConnections(
options: ListDataSourceConnectionsOptions = {}
): Promise<Array<SearchIndexerDataSourceConnection>> {
const { span, updatedOptions } = createSpan(
"SearchIndexerClient-listDataSourceConnections",
options
);
try {
const result = await this.client.dataSources.list(
operationOptionsToRequestOptionsBase(updatedOptions)
);
return result.dataSources.map(utils.generatedDataSourceToPublicDataSource);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Retrieves a list of names of existing data sources in the service.
* @param options - Options to the list indexers operation.
*/
public async listDataSourceConnectionsNames(
options: ListDataSourceConnectionsOptions = {}
): Promise<Array<string>> {
const { span, updatedOptions } = createSpan(
"SearchIndexerClient-listDataSourceConnectionsNames",
options
);
try {
const result = await this.client.dataSources.list({
...operationOptionsToRequestOptionsBase(updatedOptions),
select: "name"
});
return result.dataSources.map((ds) => ds.name);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Retrieves a list of existing Skillsets in the service.
* @param options - Options to the list Skillsets operation.
*/
public async listSkillsets(
options: ListSkillsetsOptions = {}
): Promise<Array<SearchIndexerSkillset>> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-listSkillsets", options);
try {
const result = await this.client.skillsets.list(
operationOptionsToRequestOptionsBase(updatedOptions)
);
return result.skillsets.map(utils.generatedSkillsetToPublicSkillset);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Retrieves a list of names of existing Skillsets in the service.
* @param options - Options to the list Skillsets operation.
*/
public async listSkillsetsNames(options: ListSkillsetsOptions = {}): Promise<Array<string>> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-listSkillsetsNames", options);
try {
const result = await this.client.skillsets.list({
...operationOptionsToRequestOptionsBase(updatedOptions),
select: "name"
});
return result.skillsets.map((sks) => sks.name);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Retrieves information about an Indexer.
* @param indexerName - The name of the Indexer.
* @param options - Additional optional arguments.
*/
public async getIndexer(
indexerName: string,
options: GetIndexerOptions = {}
): Promise<SearchIndexer> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-getIndexer", options);
try {
const result = await this.client.indexers.get(
indexerName,
operationOptionsToRequestOptionsBase(updatedOptions)
);
return utils.generatedSearchIndexerToPublicSearchIndexer(result);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Retrieves information about a DataSource
* @param dataSourceName - The name of the DataSource
* @param options - Additional optional arguments
*/
public async getDataSourceConnection(
dataSourceConnectionName: string,
options: GetDataSourceConnectionOptions = {}
): Promise<SearchIndexerDataSourceConnection> {
const { span, updatedOptions } = createSpan(
"SearchIndexerClient-getDataSourceConnection",
options
);
try {
const result = await this.client.dataSources.get(
dataSourceConnectionName,
operationOptionsToRequestOptionsBase(updatedOptions)
);
return utils.generatedDataSourceToPublicDataSource(result);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Retrieves information about an Skillset.
* @param indexName - The name of the Skillset.
* @param options - Additional optional arguments.
*/
public async getSkillset(
skillsetName: string,
options: GetSkillSetOptions = {}
): Promise<SearchIndexerSkillset> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-getSkillset", options);
try {
const result = await this.client.skillsets.get(
skillsetName,
operationOptionsToRequestOptionsBase(updatedOptions)
);
return utils.generatedSkillsetToPublicSkillset(result);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Creates a new indexer in a search service.
* @param indexer - The indexer definition to create in a search service.
* @param options - Additional optional arguments.
*/
public async createIndexer(
indexer: SearchIndexer,
options: CreateIndexerOptions = {}
): Promise<SearchIndexer> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-createIndexer", options);
try {
const result = await this.client.indexers.create(
utils.publicSearchIndexerToGeneratedSearchIndexer(indexer),
operationOptionsToRequestOptionsBase(updatedOptions)
);
return utils.generatedSearchIndexerToPublicSearchIndexer(result);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Creates a new dataSource in a search service.
* @param dataSourceConnection - The dataSource definition to create in a search service.
* @param options - Additional optional arguments.
*/
public async createDataSourceConnection(
dataSourceConnection: SearchIndexerDataSourceConnection,
options: CreateDataSourceConnectionOptions = {}
): Promise<SearchIndexerDataSourceConnection> {
const { span, updatedOptions } = createSpan(
"SearchIndexerClient-createDataSourceConnection",
options
);
try {
const result = await this.client.dataSources.create(
utils.publicDataSourceToGeneratedDataSource(dataSourceConnection),
operationOptionsToRequestOptionsBase(updatedOptions)
);
return utils.generatedDataSourceToPublicDataSource(result);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Creates a new skillset in a search service.
* @param skillset - The skillset containing one or more skills to create in a search service.
* @param options - Additional optional arguments.
*/
public async createSkillset(
skillset: SearchIndexerSkillset,
options: CreateSkillsetOptions = {}
): Promise<SearchIndexerSkillset> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-createSkillset", options);
try {
const result = await this.client.skillsets.create(
utils.publicSkillsetToGeneratedSkillset(skillset),
operationOptionsToRequestOptionsBase(updatedOptions)
);
return utils.generatedSkillsetToPublicSkillset(result);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Creates a new indexer or modifies an existing one.
* @param indexer - The information describing the indexer to be created/updated.
* @param options - Additional optional arguments.
*/
public async createOrUpdateIndexer(
indexer: SearchIndexer,
options: CreateorUpdateIndexerOptions = {}
): Promise<SearchIndexer> {
const { span, updatedOptions } = createSpan(
"SearchIndexerClient-createOrUpdateIndexer",
options
);
try {
const etag = options.onlyIfUnchanged ? indexer.etag : undefined;
const result = await this.client.indexers.createOrUpdate(
indexer.name,
utils.publicSearchIndexerToGeneratedSearchIndexer(indexer),
{
...operationOptionsToRequestOptionsBase(updatedOptions),
ifMatch: etag,
ignoreResetRequirements: options.ignoreResetRequirements,
disableCacheReprocessingChangeDetection: options.disableCacheReprocessingChangeDetection
}
);
return utils.generatedSearchIndexerToPublicSearchIndexer(result);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Creates a new datasource or modifies an existing one.
* @param dataSourceConnection - The information describing the datasource to be created/updated.
* @param options - Additional optional arguments.
*/
public async createOrUpdateDataSourceConnection(
dataSourceConnection: SearchIndexerDataSourceConnection,
options: CreateorUpdateDataSourceConnectionOptions = {}
): Promise<SearchIndexerDataSourceConnection> {
const { span, updatedOptions } = createSpan(
"SearchIndexerClient-createOrUpdateDataSourceConnection",
options
);
try {
const etag = options.onlyIfUnchanged ? dataSourceConnection.etag : undefined;
const result = await this.client.dataSources.createOrUpdate(
dataSourceConnection.name,
utils.publicDataSourceToGeneratedDataSource(dataSourceConnection),
{
...operationOptionsToRequestOptionsBase(updatedOptions),
ifMatch: etag,
ignoreResetRequirements: options.ignoreResetRequirements
}
);
return utils.generatedDataSourceToPublicDataSource(result);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Creates a new Skillset or modifies an existing one.
* @param skillset - The information describing the index to be created.
* @param options - Additional optional arguments.
*/
public async createOrUpdateSkillset(
skillset: SearchIndexerSkillset,
options: CreateOrUpdateSkillsetOptions = {}
): Promise<SearchIndexerSkillset> {
const { span, updatedOptions } = createSpan(
"SearchIndexerClient-createOrUpdateSkillset",
options
);
try {
const etag = options.onlyIfUnchanged ? skillset.etag : undefined;
const result = await this.client.skillsets.createOrUpdate(
skillset.name,
utils.publicSkillsetToGeneratedSkillset(skillset),
{
...operationOptionsToRequestOptionsBase(updatedOptions),
ifMatch: etag,
ignoreResetRequirements: options.ignoreResetRequirements,
disableCacheReprocessingChangeDetection: options.disableCacheReprocessingChangeDetection
}
);
return utils.generatedSkillsetToPublicSkillset(result);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Deletes an existing indexer.
* @param indexer - Indexer/Name of the indexer to delete.
* @param options - Additional optional arguments.
*/
public async deleteIndexer(
indexer: string | SearchIndexer,
options: DeleteIndexerOptions = {}
): Promise<void> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-deleteIndexer", options);
try {
const indexerName: string = typeof indexer === "string" ? indexer : indexer.name;
const etag =
typeof indexer === "string"
? undefined
: options.onlyIfUnchanged
? indexer.etag
: undefined;
await this.client.indexers.delete(indexerName, {
...operationOptionsToRequestOptionsBase(updatedOptions),
ifMatch: etag
});
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Deletes an existing datasource.
* @param dataSource - Datasource/Name of the datasource to delete.
* @param options - Additional optional arguments.
*/
public async deleteDataSourceConnection(
dataSourceConnection: string | SearchIndexerDataSourceConnection,
options: DeleteDataSourceConnectionOptions = {}
): Promise<void> {
const { span, updatedOptions } = createSpan(
"SearchIndexerClient-deleteDataSourceConnection",
options
);
try {
const dataSourceConnectionName: string =
typeof dataSourceConnection === "string" ? dataSourceConnection : dataSourceConnection.name;
const etag =
typeof dataSourceConnection === "string"
? undefined
: options.onlyIfUnchanged
? dataSourceConnection.etag
: undefined;
await this.client.dataSources.delete(dataSourceConnectionName, {
...operationOptionsToRequestOptionsBase(updatedOptions),
ifMatch: etag
});
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Deletes an existing Skillset.
* @param skillset - Skillset/Name of the Skillset to delete.
* @param options - Additional optional arguments.
*/
public async deleteSkillset(
skillset: string | SearchIndexerSkillset,
options: DeleteSkillsetOptions = {}
): Promise<void> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-deleteSkillset", options);
try {
const skillsetName: string = typeof skillset === "string" ? skillset : skillset.name;
const etag =
typeof skillset === "string"
? undefined
: options.onlyIfUnchanged
? skillset.etag
: undefined;
await this.client.skillsets.delete(skillsetName, {
...operationOptionsToRequestOptionsBase(updatedOptions),
ifMatch: etag
});
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Returns the current status and execution history of an indexer.
* @param indexerName - The name of the indexer.
* @param options - Additional optional arguments.
*/
public async getIndexerStatus(
indexerName: string,
options: GetIndexerStatusOptions = {}
): Promise<SearchIndexerStatus> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-getIndexerStatus", options);
try {
const result = await this.client.indexers.getStatus(
indexerName,
operationOptionsToRequestOptionsBase(updatedOptions)
);
return result;
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Resets the change tracking state associated with an indexer.
* @param indexerName - The name of the indexer to reset.
* @param options - Additional optional arguments.
*/
public async resetIndexer(indexerName: string, options: ResetIndexerOptions = {}): Promise<void> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-resetIndexer", options);
try {
await this.client.indexers.reset(
indexerName,
operationOptionsToRequestOptionsBase(updatedOptions)
);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Runs an indexer on-demand.
* @param indexerName - The name of the indexer to run.
* @param options - Additional optional arguments.
*/
public async runIndexer(indexerName: string, options: RunIndexerOptions = {}): Promise<void> {
const { span, updatedOptions } = createSpan("SearchIndexerClient-runIndexer", options);
try {
await this.client.indexers.run(
indexerName,
operationOptionsToRequestOptionsBase(updatedOptions)
);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message
});
throw e;
} finally {
span.end();
}
}
}