-
Notifications
You must be signed in to change notification settings - Fork 90
/
indexes.ts
987 lines (852 loc) · 25.2 KB
/
indexes.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
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
/*
* Bundle: MeiliSearch / Indexes
* Project: MeiliSearch - Javascript API
* Author: Quentin de Quelen <[email protected]>
* Copyright: 2019, MeiliSearch
*/
'use strict'
import { MeiliSearchError } from './errors'
import {
Config,
SearchResponse,
SearchParams,
Filter,
SearchRequestGET,
IndexObject,
IndexOptions,
IndexStats,
DocumentsQuery,
DocumentQuery,
Document,
DocumentOptions,
Settings,
Synonyms,
StopWords,
RankingRules,
DistinctAttribute,
FilterableAttributes,
SortableAttributes,
SearchableAttributes,
DisplayedAttributes,
TypoTolerance,
WaitOptions,
DocumentsResults,
TasksQuery,
TasksResults,
PaginationSettings,
Faceting,
} from './types'
import { removeUndefinedFromObject } from './utils'
import { HttpRequests } from './http-requests'
import { Task, TaskClient } from './task'
import { EnqueuedTask } from './enqueued-task'
class Index<T extends Record<string, any> = Record<string, any>> {
uid: string
primaryKey: string | undefined
createdAt: Date | undefined
updatedAt: Date | undefined
httpRequest: HttpRequests
tasks: TaskClient
/**
* @param config - Request configuration options
* @param uid - UID of the index
* @param primaryKey - Primary Key of the index
*/
constructor(config: Config, uid: string, primaryKey?: string) {
this.uid = uid
this.primaryKey = primaryKey
this.httpRequest = new HttpRequests(config)
this.tasks = new TaskClient(config)
}
///
/// SEARCH
///
/**
* Search for documents into an index
*
* @param query - Query string
* @param options - Search options
* @param config - Additional request configuration options
* @returns Promise containing the search response
*/
async search<D = T>(
query?: string | null,
options?: SearchParams,
config?: Partial<Request>
): Promise<SearchResponse<D>> {
const url = `indexes/${this.uid}/search`
return await this.httpRequest.post(
url,
removeUndefinedFromObject({ q: query, ...options }),
undefined,
config
)
}
/**
* Search for documents into an index using the GET method
*
* @param query - Query string
* @param options - Search options
* @param config - Additional request configuration options
* @returns Promise containing the search response
*/
async searchGet<D = T>(
query?: string | null,
options?: SearchParams,
config?: Partial<Request>
): Promise<SearchResponse<D>> {
const url = `indexes/${this.uid}/search`
const parseFilter = (filter?: Filter): string | undefined => {
if (typeof filter === 'string') return filter
else if (Array.isArray(filter))
throw new MeiliSearchError(
'The filter query parameter should be in string format when using searchGet'
)
else return undefined
}
const getParams: SearchRequestGET = {
q: query,
...options,
filter: parseFilter(options?.filter),
sort: options?.sort?.join(','),
facets: options?.facets?.join(','),
attributesToRetrieve: options?.attributesToRetrieve?.join(','),
attributesToCrop: options?.attributesToCrop?.join(','),
attributesToHighlight: options?.attributesToHighlight?.join(','),
}
return await this.httpRequest.get<SearchResponse<D>>(
url,
removeUndefinedFromObject(getParams),
config
)
}
///
/// INDEX
///
/**
* Get index information.
*
* @returns Promise containing index information
*/
async getRawInfo(): Promise<IndexObject> {
const url = `indexes/${this.uid}`
const res = await this.httpRequest.get<IndexObject>(url)
this.primaryKey = res.primaryKey
this.updatedAt = new Date(res.updatedAt)
this.createdAt = new Date(res.createdAt)
return res
}
/**
* Fetch and update Index information.
*
* @returns Promise to the current Index object with updated information
*/
async fetchInfo(): Promise<this> {
await this.getRawInfo()
return this
}
/**
* Get Primary Key.
*
* @returns Promise containing the Primary Key of the index
*/
async fetchPrimaryKey(): Promise<string | undefined> {
this.primaryKey = (await this.getRawInfo()).primaryKey
return this.primaryKey
}
/**
* Create an index.
*
* @param uid - Unique identifier of the Index
* @param options - Index options
* @param config - Request configuration options
* @returns Newly created Index object
*/
static async create(
uid: string,
options: IndexOptions = {},
config: Config
): Promise<EnqueuedTask> {
const url = `indexes`
const req = new HttpRequests(config)
const task = await req.post(url, { ...options, uid })
return new EnqueuedTask(task)
}
/**
* Update an index.
*
* @param data - Data to update
* @returns Promise to the current Index object with updated information
*/
async update(data: IndexOptions): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}`
const task = await this.httpRequest.patch(url, data)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
/**
* Delete an index.
*
* @returns Promise which resolves when index is deleted successfully
*/
async delete(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}`
const task = await this.httpRequest.delete(url)
return new EnqueuedTask(task)
}
///
/// TASKS
///
/**
* Get the list of all the tasks of the index.
*
* @param parameters - Parameters to browse the tasks
* @returns Promise containing all tasks
*/
async getTasks(parameters: TasksQuery = {}): Promise<TasksResults> {
return await this.tasks.getTasks({ ...parameters, indexUids: [this.uid] })
}
/**
* Get one task of the index.
*
* @param taskUid - Task identifier
* @returns Promise containing a task
*/
async getTask(taskUid: number): Promise<Task> {
return await this.tasks.getTask(taskUid)
}
/**
* Wait for multiple tasks to be processed.
*
* @param taskUids - Tasks identifier
* @param waitOptions - Options on timeout and interval
* @returns Promise containing an array of tasks
*/
async waitForTasks(
taskUids: number[],
{ timeOutMs = 5000, intervalMs = 50 }: WaitOptions = {}
): Promise<Task[]> {
return await this.tasks.waitForTasks(taskUids, {
timeOutMs,
intervalMs,
})
}
/**
* Wait for a task to be processed.
*
* @param taskUid - Task identifier
* @param waitOptions - Options on timeout and interval
* @returns Promise containing an array of tasks
*/
async waitForTask(
taskUid: number,
{ timeOutMs = 5000, intervalMs = 50 }: WaitOptions = {}
): Promise<Task> {
return await this.tasks.waitForTask(taskUid, {
timeOutMs,
intervalMs,
})
}
///
/// STATS
///
/**
* Get stats of an index
*
* @returns Promise containing object with stats of the index
*/
async getStats(): Promise<IndexStats> {
const url = `indexes/${this.uid}/stats`
return await this.httpRequest.get<IndexStats>(url)
}
///
/// DOCUMENTS
///
/**
* Get documents of an index
*
* @param parameters - Parameters to browse the documents
* @returns Promise containing Document responses
*/
async getDocuments<T = Record<string, any>>(
parameters: DocumentsQuery<T> = {}
): Promise<DocumentsResults<T>> {
const url = `indexes/${this.uid}/documents`
const fields = (() => {
if (Array.isArray(parameters?.fields)) {
return parameters?.fields?.join(',')
}
return undefined
})()
return await this.httpRequest.get<Promise<DocumentsResults<T>>>(
url,
removeUndefinedFromObject({
...parameters,
fields,
})
)
}
/**
* Get one document
*
* @param documentId - Document ID
* @param parameters - Parameters applied on a document
* @returns Promise containing Document response
*/
async getDocument<T = Record<string, any>>(
documentId: string | number,
parameters?: DocumentQuery<T>
): Promise<Document<T>> {
const url = `indexes/${this.uid}/documents/${documentId}`
const fields = (() => {
if (Array.isArray(parameters?.fields)) {
return parameters?.fields?.join(',')
}
return undefined
})()
return await this.httpRequest.get<Document<T>>(
url,
removeUndefinedFromObject({
...parameters,
fields,
})
)
}
/**
* Add or replace multiples documents to an index
*
* @param documents - Array of Document objects to add/replace
* @param options - Options on document addition
* @returns Promise containing an EnqueuedTask
*/
async addDocuments(
documents: Array<Document<T>>,
options?: DocumentOptions
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/documents`
const task = await this.httpRequest.post(url, documents, options)
return new EnqueuedTask(task)
}
/**
* Add or replace multiples documents to an index in batches
*
* @param documents - Array of Document objects to add/replace
* @param batchSize - Size of the batch
* @param options - Options on document addition
* @returns Promise containing array of enqueued task objects for each batch
*/
async addDocumentsInBatches(
documents: Array<Document<T>>,
batchSize = 1000,
options?: DocumentOptions
): Promise<EnqueuedTask[]> {
const updates = []
for (let i = 0; i < documents.length; i += batchSize) {
updates.push(
await this.addDocuments(documents.slice(i, i + batchSize), options)
)
}
return updates
}
/**
* Add or update multiples documents to an index
*
* @param documents - Array of Document objects to add/update
* @param options - Options on document update
* @returns Promise containing an EnqueuedTask
*/
async updateDocuments(
documents: Array<Document<Partial<T>>>,
options?: DocumentOptions
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/documents`
const task = await this.httpRequest.put(url, documents, options)
return new EnqueuedTask(task)
}
/**
* Add or update multiples documents to an index in batches
*
* @param documents - Array of Document objects to add/update
* @param batchSize - Size of the batch
* @param options - Options on document update
* @returns Promise containing array of enqueued task objects for each batch
*/
async updateDocumentsInBatches(
documents: Array<Document<Partial<T>>>,
batchSize = 1000,
options?: DocumentOptions
): Promise<EnqueuedTask[]> {
const updates = []
for (let i = 0; i < documents.length; i += batchSize) {
updates.push(
await this.updateDocuments(documents.slice(i, i + batchSize), options)
)
}
return updates
}
/**
* Delete one document
*
* @param documentId - Id of Document to delete
* @returns Promise containing an EnqueuedTask
*/
async deleteDocument(documentId: string | number): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/documents/${documentId}`
const task = await this.httpRequest.delete<EnqueuedTask>(url)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
/**
* Delete multiples documents of an index
*
* @param documentsIds - Array of Document Ids to delete
* @returns Promise containing an EnqueuedTask
*/
async deleteDocuments(
documentsIds: string[] | number[]
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/documents/delete-batch`
const task = await this.httpRequest.post(url, documentsIds)
return new EnqueuedTask(task)
}
/**
* Delete all documents of an index
*
* @returns Promise containing an EnqueuedTask
*/
async deleteAllDocuments(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/documents`
const task = await this.httpRequest.delete<EnqueuedTask>(url)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
///
/// SETTINGS
///
/**
* Retrieve all settings
*
* @returns Promise containing Settings object
*/
async getSettings(): Promise<Settings> {
const url = `indexes/${this.uid}/settings`
return await this.httpRequest.get<Settings>(url)
}
/**
* Update all settings Any parameters not provided will be left unchanged.
*
* @param settings - Object containing parameters with their updated values
* @returns Promise containing an EnqueuedTask
*/
async updateSettings(settings: Settings): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings`
const task = await this.httpRequest.patch(url, settings)
task.enqueued = new Date(task.enqueuedAt)
return task
}
/**
* Reset settings.
*
* @returns Promise containing an EnqueuedTask
*/
async resetSettings(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings`
const task = await this.httpRequest.delete<EnqueuedTask>(url)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
///
/// PAGINATION SETTINGS
///
/**
* Get the pagination settings.
*
* @returns Promise containing object of pagination settings
*/
async getPagination(): Promise<PaginationSettings> {
const url = `indexes/${this.uid}/settings/pagination`
return await this.httpRequest.get<object>(url)
}
/**
* Update the pagination settings.
*
* @param pagination - Pagination object
* @returns Promise containing an EnqueuedTask
*/
async updatePagination(
pagination: PaginationSettings
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/pagination`
const task = await this.httpRequest.patch(url, pagination)
return new EnqueuedTask(task)
}
/**
* Reset the pagination settings.
*
* @returns Promise containing an EnqueuedTask
*/
async resetPagination(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/pagination`
const task = await this.httpRequest.delete(url)
return new EnqueuedTask(task)
}
///
/// SYNONYMS
///
/**
* Get the list of all synonyms
*
* @returns Promise containing object of synonym mappings
*/
async getSynonyms(): Promise<object> {
const url = `indexes/${this.uid}/settings/synonyms`
return await this.httpRequest.get<object>(url)
}
/**
* Update the list of synonyms. Overwrite the old list.
*
* @param synonyms - Mapping of synonyms with their associated words
* @returns Promise containing an EnqueuedTask
*/
async updateSynonyms(synonyms: Synonyms): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/synonyms`
const task = await this.httpRequest.put(url, synonyms)
return new EnqueuedTask(task)
}
/**
* Reset the synonym list to be empty again
*
* @returns Promise containing an EnqueuedTask
*/
async resetSynonyms(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/synonyms`
const task = await this.httpRequest.delete<EnqueuedTask>(url)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
///
/// STOP WORDS
///
/**
* Get the list of all stop-words
*
* @returns Promise containing array of stop-words
*/
async getStopWords(): Promise<string[]> {
const url = `indexes/${this.uid}/settings/stop-words`
return await this.httpRequest.get<string[]>(url)
}
/**
* Update the list of stop-words. Overwrite the old list.
*
* @param stopWords - Array of strings that contains the stop-words.
* @returns Promise containing an EnqueuedTask
*/
async updateStopWords(stopWords: StopWords): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/stop-words`
const task = await this.httpRequest.put(url, stopWords)
return new EnqueuedTask(task)
}
/**
* Reset the stop-words list to be empty again
*
* @returns Promise containing an EnqueuedTask
*/
async resetStopWords(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/stop-words`
const task = await this.httpRequest.delete<EnqueuedTask>(url)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
///
/// RANKING RULES
///
/**
* Get the list of all ranking-rules
*
* @returns Promise containing array of ranking-rules
*/
async getRankingRules(): Promise<string[]> {
const url = `indexes/${this.uid}/settings/ranking-rules`
return await this.httpRequest.get<string[]>(url)
}
/**
* Update the list of ranking-rules. Overwrite the old list.
*
* @param rankingRules - Array that contain ranking rules sorted by order of
* importance.
* @returns Promise containing an EnqueuedTask
*/
async updateRankingRules(rankingRules: RankingRules): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/ranking-rules`
const task = await this.httpRequest.put(url, rankingRules)
return new EnqueuedTask(task)
}
/**
* Reset the ranking rules list to its default value
*
* @returns Promise containing an EnqueuedTask
*/
async resetRankingRules(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/ranking-rules`
const task = await this.httpRequest.delete<EnqueuedTask>(url)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
///
/// DISTINCT ATTRIBUTE
///
/**
* Get the distinct-attribute
*
* @returns Promise containing the distinct-attribute of the index
*/
async getDistinctAttribute(): Promise<string | null> {
const url = `indexes/${this.uid}/settings/distinct-attribute`
return await this.httpRequest.get<string | null>(url)
}
/**
* Update the distinct-attribute.
*
* @param distinctAttribute - Field name of the distinct-attribute
* @returns Promise containing an EnqueuedTask
*/
async updateDistinctAttribute(
distinctAttribute: DistinctAttribute
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/distinct-attribute`
const task = await this.httpRequest.put(url, distinctAttribute)
return new EnqueuedTask(task)
}
/**
* Reset the distinct-attribute.
*
* @returns Promise containing an EnqueuedTask
*/
async resetDistinctAttribute(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/distinct-attribute`
const task = await this.httpRequest.delete<EnqueuedTask>(url)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
///
/// FILTERABLE ATTRIBUTES
///
/**
* Get the filterable-attributes
*
* @returns Promise containing an array of filterable-attributes
*/
async getFilterableAttributes(): Promise<string[]> {
const url = `indexes/${this.uid}/settings/filterable-attributes`
return await this.httpRequest.get<string[]>(url)
}
/**
* Update the filterable-attributes.
*
* @param filterableAttributes - Array of strings containing the attributes
* that can be used as filters at query time
* @returns Promise containing an EnqueuedTask
*/
async updateFilterableAttributes(
filterableAttributes: FilterableAttributes
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/filterable-attributes`
const task = await this.httpRequest.put(url, filterableAttributes)
return new EnqueuedTask(task)
}
/**
* Reset the filterable-attributes.
*
* @returns Promise containing an EnqueuedTask
*/
async resetFilterableAttributes(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/filterable-attributes`
const task = await this.httpRequest.delete<EnqueuedTask>(url)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
///
/// SORTABLE ATTRIBUTES
///
/**
* Get the sortable-attributes
*
* @returns Promise containing array of sortable-attributes
*/
async getSortableAttributes(): Promise<string[]> {
const url = `indexes/${this.uid}/settings/sortable-attributes`
return await this.httpRequest.get<string[]>(url)
}
/**
* Update the sortable-attributes.
*
* @param sortableAttributes - Array of strings containing the attributes that
* can be used to sort search results at query time
* @returns Promise containing an EnqueuedTask
*/
async updateSortableAttributes(
sortableAttributes: SortableAttributes
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/sortable-attributes`
const task = await this.httpRequest.put(url, sortableAttributes)
return new EnqueuedTask(task)
}
/**
* Reset the sortable-attributes.
*
* @returns Promise containing an EnqueuedTask
*/
async resetSortableAttributes(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/sortable-attributes`
const task = await this.httpRequest.delete<EnqueuedTask>(url)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
///
/// SEARCHABLE ATTRIBUTE
///
/**
* Get the searchable-attributes
*
* @returns Promise containing array of searchable-attributes
*/
async getSearchableAttributes(): Promise<string[]> {
const url = `indexes/${this.uid}/settings/searchable-attributes`
return await this.httpRequest.get<string[]>(url)
}
/**
* Update the searchable-attributes.
*
* @param searchableAttributes - Array of strings that contains searchable
* attributes sorted by order of importance(most to least important)
* @returns Promise containing an EnqueuedTask
*/
async updateSearchableAttributes(
searchableAttributes: SearchableAttributes
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/searchable-attributes`
const task = await this.httpRequest.put(url, searchableAttributes)
return new EnqueuedTask(task)
}
/**
* Reset the searchable-attributes.
*
* @returns Promise containing an EnqueuedTask
*/
async resetSearchableAttributes(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/searchable-attributes`
const task = await this.httpRequest.delete<EnqueuedTask>(url)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
///
/// DISPLAYED ATTRIBUTE
///
/**
* Get the displayed-attributes
*
* @returns Promise containing array of displayed-attributes
*/
async getDisplayedAttributes(): Promise<string[]> {
const url = `indexes/${this.uid}/settings/displayed-attributes`
return await this.httpRequest.get<string[]>(url)
}
/**
* Update the displayed-attributes.
*
* @param displayedAttributes - Array of strings that contains attributes of
* an index to display
* @returns Promise containing an EnqueuedTask
*/
async updateDisplayedAttributes(
displayedAttributes: DisplayedAttributes
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/displayed-attributes`
const task = await this.httpRequest.put(url, displayedAttributes)
return new EnqueuedTask(task)
}
/**
* Reset the displayed-attributes.
*
* @returns Promise containing an EnqueuedTask
*/
async resetDisplayedAttributes(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/displayed-attributes`
const task = await this.httpRequest.delete<EnqueuedTask>(url)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
///
/// TYPO TOLERANCE
///
/**
* Get the typo tolerance settings.
*
* @returns Promise containing the typo tolerance settings.
*/
async getTypoTolerance(): Promise<string[]> {
const url = `indexes/${this.uid}/settings/typo-tolerance`
return await this.httpRequest.get<string[]>(url)
}
/**
* Update the typo tolerance settings.
*
* @param typoTolerance - Object containing the custom typo tolerance
* settings.
* @returns Promise containing object of the enqueued update
*/
async updateTypoTolerance(
typoTolerance: TypoTolerance
): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/typo-tolerance`
const task = await this.httpRequest.patch(url, typoTolerance)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
/**
* Reset the typo tolerance settings.
*
* @returns Promise containing object of the enqueued update
*/
async resetTypoTolerance(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/typo-tolerance`
const task = await this.httpRequest.delete<EnqueuedTask>(url)
task.enqueuedAt = new Date(task.enqueuedAt)
return task
}
///
/// FACETING
///
/**
* Get the faceting settings.
*
* @returns Promise containing object of faceting index settings
*/
async getFaceting(): Promise<Faceting> {
const url = `indexes/${this.uid}/settings/faceting`
return await this.httpRequest.get<Faceting>(url)
}
/**
* Update the faceting settings.
*
* @param faceting - Faceting index settings object
* @returns Promise containing an EnqueuedTask
*/
async updateFaceting(faceting: Faceting): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/faceting`
const task = await this.httpRequest.patch(url, faceting)
return new EnqueuedTask(task)
}
/**
* Reset the faceting settings.
*
* @returns Promise containing an EnqueuedTask
*/
async resetFaceting(): Promise<EnqueuedTask> {
const url = `indexes/${this.uid}/settings/faceting`
const task = await this.httpRequest.delete(url)
return new EnqueuedTask(task)
}
}
export { Index }