-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.dart
887 lines (763 loc) · 24.3 KB
/
index.dart
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
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:meilisearch/meilisearch.dart';
import 'annotations.dart';
import 'package:collection/collection.dart';
import 'http_request.dart';
const _ndjsonContentType = 'application/x-ndjson';
const _csvContentType = 'text/csv';
class MeiliSearchIndex {
MeiliSearchIndex(
this.client,
this.uid, {
this.primaryKey,
DateTime? createdAt,
DateTime? updatedAt,
}) : _createdAt = createdAt,
_updatedAt = updatedAt;
final MeiliSearchClient client;
final String uid;
String? primaryKey;
DateTime? _createdAt;
DateTime? get createdAt => _createdAt;
DateTime? _updatedAt;
DateTime? get updatedAt => _updatedAt;
HttpRequest get http => client.http;
factory MeiliSearchIndex.fromMap(
MeiliSearchClient client,
Map<String, Object?> map,
) {
final createdAtRaw = map['createdAt'];
final primaryKeyRaw = map['primaryKey'];
final updatedAtRaw = map['updatedAt'];
return MeiliSearchIndex(
client,
map['uid'] as String,
primaryKey: primaryKeyRaw is String ? primaryKeyRaw : null,
createdAt:
createdAtRaw is String ? DateTime.tryParse(createdAtRaw) : null,
updatedAt:
updatedAtRaw is String ? DateTime.tryParse(updatedAtRaw) : null,
);
}
//
// Index endpoints
//
/// Update the primary Key of the index.
Future<Task> update({String? primaryKey}) async {
final data = <String, Object?>{
if (primaryKey != null) 'primaryKey': primaryKey,
};
return await _getTask(http.patchMethod('/indexes/$uid', data: data));
}
/// Delete the index.
Future<Task> delete() async {
return await _getTask(http.deleteMethod('/indexes/$uid'));
}
/// Get the information of the index from the Meilisearch server and return it.
Future<MeiliSearchIndex> fetchInfo() async {
final index = await client.getIndex(uid);
primaryKey = index.primaryKey;
_createdAt = index.createdAt;
_updatedAt = index.updatedAt;
return index;
}
/// Update the primaryKey of the index and return it.
Future<String?> fetchPrimaryKey() async {
final index = await fetchInfo();
return index.primaryKey;
}
//
// Search endpoints
//
/// Search for documents matching a specific query in the index.
Future<Searcheable<Map<String, dynamic>>> search(
String? text, [
SearchQuery? q,
]) async {
final response = await http.postMethod<Map<String, Object?>>(
'/indexes/$uid/search',
data: {
'q': text,
...?q?.toSparseMap(),
},
);
return Searcheable.createSearchResult(response.data!, indexUid: uid);
}
@RequiredMeiliServerVersion('1.3.0')
Future<FacetSearchResult> facetSearch(FacetSearchQuery query) async {
final response = await http.postMethod<Map<String, Object?>>(
'/indexes/$uid/facet-search',
data: query.toSparseMap(),
);
return FacetSearchResult.fromMap(response.data!);
}
//
// Document endpoints
//
Future<Task> _getTask(Future<Response<Map<String, Object?>>> future) async {
final response = await future;
return Task.fromMap(response.data!);
}
/// {@template meili.add_docs}
/// Add a list of documents by given [documents] and optional [primaryKey] parameter.
/// {@endtemplate}
///
/// {@template meili.index_upsert}
/// If the index does not exist, tries to create a new index and adds documents.
/// {@endtemplate}
Future<Task> addDocuments(
List<Map<String, Object?>> documents, {
String? primaryKey,
}) {
return _getTask(http.postMethod(
'/indexes/$uid/documents',
data: documents,
queryParameters: {
if (primaryKey != null) 'primaryKey': primaryKey,
},
));
}
/// {@macro meili.add_docs}
///
/// * The passed [documents] must be a valid JSON string representing an array of objects.
/// *
/// {@macro meili.index_upsert}
Future<Task> addDocumentsJson(String documents, {String? primaryKey}) {
final decoded = jsonDecode(documents);
if (decoded is List<Object?>) {
final casted = decoded.whereType<Map<String, Object?>>().toList();
return addDocuments(casted, primaryKey: primaryKey);
}
throw MeiliSearchApiException(
"Provided json must be an array of documents, consider using addDocumentsNdjson if this isn't the case",
);
}
/// {@macro meili.add_docs}
///
/// *
/// {@template meili.csv}
/// The passed documents must be a valid CSV string, where the first line contains objects' keys and types, and each subsequent line corresponds to an object.
/// [see relevant documentation](https://www.meilisearch.com/docs/learn/core_concepts/documents#csv)
/// {@endtemplate}
///
/// *
/// {@macro meili.index_upsert}
Future<Task> addDocumentsCsv(
String documents, {
String? primaryKey,
String? csvDelimiter,
}) {
return _getTask(http.postMethod(
'/indexes/$uid/documents',
data: documents,
queryParameters: {
if (primaryKey != null) 'primaryKey': primaryKey,
if (csvDelimiter != null) 'csvDelimiter': csvDelimiter,
},
contentType: _csvContentType,
));
}
/// {@macro meili.add_docs}
///
/// * The passed [documents] must be a valid Newline Delimited Json (NdJson) string, where each line corresponds to an object.
/// *
/// {@macro meili.index_upsert}
Future<Task> addDocumentsNdjson(String documents, {String? primaryKey}) {
return _getTask(http.postMethod(
'/indexes/$uid/documents',
data: documents,
queryParameters: {
if (primaryKey != null) 'primaryKey': primaryKey,
},
contentType: _ndjsonContentType,
));
}
/// {@template meili.add_docs_batches}
/// Add a list of documents in batches of size [batchSize] by given [documents] and optional [primaryKey] parameter.
/// {@endtemplate}
///
/// {@macro meili.index_upsert}
Future<List<Task>> addDocumentsInBatches(
List<Map<String, dynamic>> documents, {
int batchSize = 1000,
String? primaryKey,
}) =>
Future.wait(
documents
.slices(batchSize)
.map((slice) => addDocuments(slice, primaryKey: primaryKey)),
);
/// {@macro meili.add_docs_batches}
///
/// *
/// {@macro meili.csv}
/// *
/// {@macro meili.index_upsert}
Future<List<Task>> addDocumentsCsvInBatches(
String documents, {
String? primaryKey,
int batchSize = 1000,
String? csvDelimiter,
}) {
final ls = LineSplitter();
final split = ls.convert(documents);
//header is shared for all slices
final header = split.first;
return Future.wait(
split.skip(1).slices(batchSize).map(
(slice) => addDocumentsCsv(
[header, ...slice].join('\n'),
primaryKey: primaryKey,
csvDelimiter: csvDelimiter,
),
),
);
}
/// {@macro meili.add_docs_batches}
///
/// * The passed [documents] must be a valid Newline Delimited Json (NdJson) string, where each line corresponds to an object.
/// *
/// {@macro meili.index_upsert}
Future<List<Task>> addDocumentsNdjsonInBatches(
String documents, {
String? primaryKey,
int batchSize = 1000,
}) {
final ls = LineSplitter();
final split = ls.convert(documents);
return Future.wait(
split.slices(batchSize).map(
(slice) => addDocumentsNdjson(
slice.join('\n'),
primaryKey: primaryKey,
),
),
);
}
/// {@template meili.update_docs}
/// Add a list of documents or update them if they already exist by given [documents] and optional [primaryKey] parameter.
/// {@endtemplate}
///
/// {@macro meili.index_upsert}
Future<Task> updateDocuments(
List<Map<String, Object?>> documents, {
String? primaryKey,
}) async {
return await _getTask(http.putMethod(
'/indexes/$uid/documents',
data: documents,
queryParameters: {
if (primaryKey != null) 'primaryKey': primaryKey,
},
));
}
/// {@macro meili.update_docs}
///
/// * the passed [documents] must be a valid JSON string representing an array of objects.
/// *
/// {@macro meili.index_upsert}
Future<Task> updateDocumentsJson(
String documents, {
String? primaryKey,
}) {
final decoded = jsonDecode(documents);
if (decoded is List<Object?>) {
final casted = decoded.whereType<Map<String, Object?>>().toList();
return updateDocuments(casted, primaryKey: primaryKey);
}
throw MeiliSearchApiException(
"Provided json must be an array of documents, consider using updateDocumentsNdjson if this isn't the case",
);
}
/// {@macro meili.update_docs}
///
/// *
/// {@macro meili.csv}
/// *
/// {@macro meili.index_upsert}
Future<Task> updateDocumentsCsv(
String documents, {
String? primaryKey,
String? csvDelimiter,
}) {
return _getTask(http.putMethod(
'/indexes/$uid/documents',
data: documents,
queryParameters: {
if (primaryKey != null) 'primaryKey': primaryKey,
if (csvDelimiter != null) 'csvDelimiter': csvDelimiter,
},
contentType: _csvContentType,
));
}
/// {@macro meili.update_docs}
///
/// * The passed [documents] must be a valid Newline Delimited Json (NdJson) string, where each line corresponds to an object.
/// *
/// {@macro meili.index_upsert}
Future<Task> updateDocumentsNdjson(String documents, {String? primaryKey}) {
return _getTask(http.putMethod(
'/indexes/$uid/documents',
data: documents,
queryParameters: {
if (primaryKey != null) 'primaryKey': primaryKey,
},
contentType: _ndjsonContentType,
));
}
/// {@template meili.update_docs_batches}
/// Add a list of documents or update them if they already exist in batches of size [batchSize] by given [documents] and optional [primaryKey] parameter.
/// {@endtemplate}
///
/// {@macro meili.index_upsert}
Future<List<Task>> updateDocumentsInBatches(
List<Map<String, Object?>> documents, {
int batchSize = 1000,
String? primaryKey,
}) =>
Future.wait(
documents
.slices(batchSize)
.map((slice) => updateDocuments(slice, primaryKey: primaryKey)),
);
/// {@macro meili.update_docs_batches}
///
/// * The passed [documents] must be a valid CSV string, where each line corresponds to an object.
/// *
/// {@macro meili.index_upsert}
Future<List<Task>> updateDocumentsCsvInBatches(
String documents, {
String? primaryKey,
int batchSize = 1000,
String? csvDelimiter,
}) {
final ls = LineSplitter();
final split = ls.convert(documents);
//header is shared for all slices
final header = split.first;
return Future.wait(
split.skip(1).slices(batchSize).map(
(slice) => updateDocumentsCsv(
[header, ...slice].join('\n'),
primaryKey: primaryKey,
csvDelimiter: csvDelimiter,
),
),
);
}
/// {@macro meili.update_docs_batches}
///
/// * The passed [documents] must be a valid Newline Delimited Json (NdJson) string, where each line corresponds to an object.
/// *
/// {@macro meili.index_upsert}
Future<List<Task>> updateDocumentsNdjsonInBatches(
String documents, {
String? primaryKey,
int batchSize = 1000,
}) {
final ls = LineSplitter();
final split = ls.convert(documents);
return Future.wait(
split.slices(batchSize).map(
(slice) => updateDocumentsNdjson(
slice.join('\n'),
primaryKey: primaryKey,
),
),
);
}
/// Delete all documents in the specified index.
Future<Task> deleteAllDocuments() async {
return await _getTask(http.deleteMethod('/indexes/$uid/documents'));
}
/// Delete one document by given [id].
Future<Task> deleteDocument(Object id) async {
return await _getTask(http.deleteMethod('/indexes/$uid/documents/$id'));
}
/// Delete a selection of documents by given query.
Future<Task> deleteDocuments(DeleteDocumentsQuery query) async {
String route;
Object? data;
if (query.ids != null) {
// if ids are available, fallback to the delete-batch route
route = 'delete-batch';
data = query.ids;
} else {
// if ids are NOT available, use the new delete route
route = 'delete';
data = query.toSparseMap();
}
return await _getTask(
http.postMethod(
'/indexes/$uid/documents/$route',
data: data,
),
);
}
/// Return the document in the index by given [id].
Future<Map<String, dynamic>?> getDocument(
Object id, {
List<String> fields = const [],
}) async {
final params = DocumentsQuery(fields: fields);
final response = await http.getMethod<Map<String, dynamic>>(
'/indexes/$uid/documents/$id',
queryParameters: params.toQuery(),
);
return response.data;
}
/// Return a list of all existing documents in the index.
Future<Result<Map<String, dynamic>>> getDocuments({
DocumentsQuery? params,
}) async {
if (params != null && params.containsFilter) {
final response = await http.postMethod<Map<String, Object?>>(
'/indexes/$uid/documents/fetch',
data: params.toSparseMap(),
);
return Result.fromMap(response.data!);
}
final response = await http.getMethod<Map<String, Object?>>(
'/indexes/$uid/documents',
queryParameters: params?.toQuery(),
);
return Result.fromMap(response.data!);
}
//
// Settings endpoints
//
/// Get the settings of the index.
Future<IndexSettings> getSettings() async {
final response =
await http.getMethod<Map<String, Object?>>('/indexes/$uid/settings');
return IndexSettings.fromMap(response.data!);
}
/// Reset the settings of the index.
/// All settings will be reset to their default value.
Future<Task> resetSettings() async {
return await _getTask(http.deleteMethod('/indexes/$uid/settings'));
}
/// Update the settings of the index. Any parameters not provided in the body will be left unchanged.
Future<Task> updateSettings(IndexSettings settings) async {
return await _getTask(http.patchMethod(
'/indexes/$uid/settings',
data: settings.toMap(),
));
}
/// Get filterable attributes of the index.
Future<List<String>> getFilterableAttributes() async {
final response = await http.getMethod<List<Object?>>(
'/indexes/$uid/settings/filterable-attributes');
return response.data!.cast<String>();
}
/// Reset filterable attributes of the index.
Future<Task> resetFilterableAttributes() async {
return await _getTask(
http.deleteMethod('/indexes/$uid/settings/filterable-attributes'));
}
/// Update filterable attributes of the index.
Future<Task> updateFilterableAttributes(
List<String> filterableAttributes,
) async {
return await _getTask(
http.putMethod(
'/indexes/$uid/settings/filterable-attributes',
data: filterableAttributes,
),
);
}
/// Get the displayed attributes of the index.
Future<List<String>> getDisplayedAttributes() async {
final response = await http.getMethod<List<Object?>>(
'/indexes/$uid/settings/displayed-attributes');
return response.data!.cast<String>();
}
/// Reset the displayed attributes of the index.
Future<Task> resetDisplayedAttributes() async {
return await _getTask(
http.deleteMethod('/indexes/$uid/settings/displayed-attributes'),
);
}
/// Update the displayed attributes of the index.
Future<Task> updateDisplayedAttributes(
List<String> displayedAttributes,
) async {
return await _getTask(
http.putMethod(
'/indexes/$uid/settings/displayed-attributes',
data: displayedAttributes,
),
);
}
/// Get the distinct attribute for the index.
Future<String?> getDistinctAttribute() async {
final response = await http
.getMethod<String?>('/indexes/$uid/settings/distinct-attribute');
return response.data;
}
/// Reset the distinct attribute for the index.
Future<Task> resetDistinctAttribute() async {
return await _getTask(
http.deleteMethod('/indexes/$uid/settings/distinct-attribute'),
);
}
/// Update the distinct attribute for the index.
Future<Task> updateDistinctAttribute(String distinctAttribute) async {
return await _getTask(
http.putMethod(
'/indexes/$uid/settings/distinct-attribute',
data: '"$distinctAttribute"',
),
);
}
/// Get ranking rules of the index.
Future<List<String>> getRankingRules() async {
final response = await http
.getMethod<List<Object?>>('/indexes/$uid/settings/ranking-rules');
return response.data!.cast<String>();
}
/// Reset ranking rules of the index.
Future<Task> resetRankingRules() async {
return await _getTask(
http.deleteMethod('/indexes/$uid/settings/ranking-rules'),
);
}
/// Update ranking rules of the index.
Future<Task> updateRankingRules(List<String> rankingRules) async {
return await _getTask(
http.putMethod(
'/indexes/$uid/settings/ranking-rules',
data: rankingRules,
),
);
}
/// Get separator tokens of the index.
Future<List<String>> getSeparatorTokens() async {
final response = await http
.getMethod<List<Object?>>('/indexes/$uid/settings/separator-tokens');
return response.data!.cast<String>();
}
/// Reset separator tokens of the index.
Future<Task> resetSeparatorTokens() async {
return await _getTask(
http.deleteMethod('/indexes/$uid/settings/separator-tokens'),
);
}
/// Update separator tokens of the index.
Future<Task> updateSeparatorTokens(List<String> separatorTokens) async {
return await _getTask(
http.putMethod(
'/indexes/$uid/settings/separator-tokens',
data: separatorTokens,
),
);
}
/// Get non separator tokens of the index.
Future<List<String>> getNonSeparatorTokens() async {
final response = await http.getMethod<List<Object?>>(
'/indexes/$uid/settings/non-separator-tokens');
return response.data!.cast<String>();
}
/// Reset separator tokens of the index.
Future<Task> resetNonSeparatorTokens() async {
return await _getTask(
http.deleteMethod('/indexes/$uid/settings/non-separator-tokens'),
);
}
/// Update separator tokens of the index.
Future<Task> updateNonSeparatorTokens(List<String> nonSeparatorTokens) async {
return await _getTask(
http.putMethod(
'/indexes/$uid/settings/non-separator-tokens',
data: nonSeparatorTokens,
),
);
}
/// Get searchable attributes of the index.
Future<List<String>> getSearchableAttributes() async {
final response = await http.getMethod<List<Object?>>(
'/indexes/$uid/settings/searchable-attributes',
);
return response.data!.cast<String>();
}
/// Reset searchable attributes of the index.
Future<Task> resetSearchableAttributes() async {
return await _getTask(
http.deleteMethod('/indexes/$uid/settings/searchable-attributes'),
);
}
/// Update the searchable attributes of the index.
Future<Task> updateSearchableAttributes(
List<String> searchableAttributes) async {
return await _getTask(
http.putMethod(
'/indexes/$uid/settings/searchable-attributes',
data: searchableAttributes,
),
);
}
//
// StopWords endpoints
//
/// Get stop words of the index.
Future<List<String>> getStopWords() async {
final response = await http
.getMethod<List<Object?>>('/indexes/$uid/settings/stop-words');
return response.data!.cast<String>();
}
/// Reset stop words of the index.
Future<Task> resetStopWords() async {
return await _getTask(
http.deleteMethod('/indexes/$uid/settings/stop-words'));
}
/// Update stop words of the index
Future<Task> updateStopWords(List<String> stopWords) async {
return await _getTask(
http.putMethod('/indexes/$uid/settings/stop-words', data: stopWords));
}
//
// Synonyms endpoints
//
/// Get synonyms of the index.
Future<Map<String, List<String>>> getSynonyms() async {
final response = await http
.getMethod<Map<String, Object?>>('/indexes/$uid/settings/synonyms');
return response.data!
.map((key, value) => MapEntry(key, (value as List).cast<String>()));
}
/// Reset synonyms of the index.
Future<Task> resetSynonyms() async {
return await _getTask(http.deleteMethod('/indexes/$uid/settings/synonyms'));
}
/// Update synonyms of the index
Future<Task> updateSynonyms(Map<String, List<String>> synonyms) async {
return await _getTask(
http.putMethod('/indexes/$uid/settings/synonyms', data: synonyms));
}
//
// Sortable Attributes endpoints
//
/// Get sortable attributes of the index.
Future<List<String>> getSortableAttributes() async {
final response = await http
.getMethod<List<Object?>>('/indexes/$uid/settings/sortable-attributes');
return response.data!.cast<String>();
}
/// Reset sortable attributes of the index.
Future<Task> resetSortableAttributes() async {
return await _getTask(
http.deleteMethod('/indexes/$uid/settings/sortable-attributes'));
}
/// Update sortable attributes of the index.
Future<Task> updateSortableAttributes(List<String> sortableAttributes) async {
return _getTask(
http.putMethod(
'/indexes/$uid/settings/sortable-attributes',
data: sortableAttributes,
),
);
}
//
// Typo Tolerance endpoints
//
/// Get typo tolerance settings of the index.
Future<TypoTolerance> getTypoTolerance() async {
final response = await http.getMethod<Map<String, Object?>>(
'/indexes/$uid/settings/typo-tolerance',
);
return TypoTolerance.fromMap(response.data!);
}
/// Reset typo tolerance settings of the index.
Future<Task> resetTypoTolerance() async {
return await _getTask(
http.deleteMethod('/indexes/$uid/settings/typo-tolerance'),
);
}
/// Update typo tolerance settings of the index.
Future<Task> updateTypoTolerance(TypoTolerance typoTolerance) async {
return await _getTask(
http.patchMethod(
'/indexes/$uid/settings/typo-tolerance',
data: typoTolerance.toMap(),
),
);
}
//
// Pagination endpoints
//
/// Get pagination settings of the index.
Future<Pagination> getPagination() async {
final response = await http.getMethod<Map<String, Object?>>(
'/indexes/$uid/settings/pagination',
);
return Pagination.fromMap(response.data!);
}
/// Reset pagination settings of the index.
Future<Task> resetPagination() async {
return await _getTask(
http.deleteMethod('/indexes/$uid/settings/pagination'),
);
}
/// Update pagination settings of the index.
Future<Task> updatePagination(Pagination pagination) async {
return await _getTask(
http.patchMethod(
'/indexes/$uid/settings/pagination',
data: pagination.toMap(),
),
);
}
//
// Faceting endpoints
//
/// Get faceting settings of the index.
Future<Faceting> getFaceting() async {
final response = await http.getMethod<Map<String, Object?>>(
'/indexes/$uid/settings/faceting',
);
return Faceting.fromMap(response.data!);
}
/// Reset faceting settings of the index.
Future<Task> resetFaceting() async {
return await _getTask(
http.deleteMethod('/indexes/$uid/settings/faceting'),
);
}
/// Update faceting settings of the index.
Future<Task> updateFaceting(Faceting faceting) async {
return await _getTask(
http.patchMethod(
'/indexes/$uid/settings/faceting',
data: faceting.toMap(),
),
);
}
//
// Stats endponts
//
/// Get stats of the index.
Future<IndexStats> getStats() async {
final response =
await http.getMethod<Map<String, Object?>>('/indexes/$uid/stats');
return IndexStats.fromMap(response.data!);
}
//
// Tasks endpoints
//
/// Get all tasks from the index.
Future<TasksResults> getTasks({TasksQuery? params}) async {
if (params == null) {
params = TasksQuery(indexUids: [uid]);
} else {
if (!params.indexUids.contains(uid)) {
params = params.copyWith(indexUids: [...params.indexUids, uid]);
}
}
return await client.getTasks(params: params);
}
/// Get a task from an index specified by uid.
Future<Task> getTask(int uid) async {
return await client.getTask(uid);
}
}