-
Notifications
You must be signed in to change notification settings - Fork 58
/
Index.cs
747 lines (680 loc) · 39.7 KB
/
Index.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
namespace Meilisearch
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading;
using System.Threading.Tasks;
using Meilisearch.Extensions;
/// <summary>
/// Meilisearch index to search and manage documents.
/// </summary>
public class Index
{
private HttpClient http;
private TaskEndpoint _taskEndpoint;
/// <summary>
/// Initializes a new instance of the <see cref="Index"/> class.
/// Initializes with the UID (mandatory) and the primary key.
/// </summary>
/// <param name="uid">Unique index identifier.</param>
/// <param name="primaryKey">Documents primary key.</param>
/// <param name="createdAt">The creation date of the index.</param>
/// <param name="updatedAt">The latest update of the index.</param>
public Index(string uid, string primaryKey = default, DateTimeOffset? createdAt = default, DateTimeOffset? updatedAt = default)
{
this.Uid = uid;
this.PrimaryKey = primaryKey;
this.CreatedAt = createdAt;
this.UpdatedAt = updatedAt;
this._taskEndpoint = null;
}
/// <summary>
/// Gets unique identifier of the index.
/// </summary>
public string Uid { get; internal set; }
/// <summary>
/// Gets primary key of the documents.
/// </summary>
public string PrimaryKey { get; internal set; }
/// <summary>
/// Gets the latest update date of the index.
/// </summary>
public DateTimeOffset? UpdatedAt { get; internal set; }
/// <summary>
/// Gets the creation date of the index.
/// </summary>
public DateTimeOffset? CreatedAt { get; internal set; }
/// <summary>
/// Gets raw index call response.
/// </summary>
/// <param name="http">HTTP client to make the call.</param>
/// <param name="uid">Uid of the index to retrieve.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Call response.</returns>
public static async Task<HttpResponseMessage> GetRawAsync(HttpClient http, string uid, CancellationToken cancellationToken = default)
{
return await http.GetAsync($"indexes/{uid}", cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Fetch the info of the index.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>An instance of the index fetch.</returns>
public async Task<Index> FetchInfoAsync(CancellationToken cancellationToken = default)
{
var response = await GetRawAsync(this.http, this.Uid, cancellationToken).ConfigureAwait(false);
var content = await response.Content.ReadFromJsonAsync<Index>(cancellationToken: cancellationToken).ConfigureAwait(false);
this.PrimaryKey = content.PrimaryKey;
this.CreatedAt = content.CreatedAt;
this.UpdatedAt = content.UpdatedAt;
return this;
}
/// <summary>
/// Fetch the primary key of the index.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Primary key of the fetched index.</returns>
public async Task<string> FetchPrimaryKey(CancellationToken cancellationToken = default)
{
return (await this.FetchInfoAsync(cancellationToken).ConfigureAwait(false)).PrimaryKey;
}
/// <summary>
/// Changes the primary key of the index.
/// </summary>
/// <param name="primarykeytoChange">Primary key set.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Index with the updated Primary Key.</returns>
public async Task<TaskInfo> UpdateAsync(string primarykeytoChange, CancellationToken cancellationToken = default)
{
var responseMessage =
await this.http.PutAsJsonAsync($"indexes/{this.Uid}", new { primaryKey = primarykeytoChange }, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Deletes the index.
/// It's not a recovery delete. You will also lose the documents within the index.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> DeleteAsync(CancellationToken cancellationToken = default)
{
var responseMessage = await this.http.DeleteAsync($"/indexes/{this.Uid}", cancellationToken).ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Add documents.
/// </summary>
/// <param name="documents">Documents to add.</param>
/// <param name="primaryKey">Primary key for the documents.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <typeparam name="T">Type of the document. Even though documents are schemaless in Meilisearch, making it typed helps in compile time.</typeparam>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> AddDocumentsAsync<T>(IEnumerable<T> documents, string primaryKey = default, CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage;
string uri = $"/indexes/{this.Uid}/documents";
if (primaryKey != default)
{
uri = $"{uri}?{new { primaryKey = primaryKey }.ToQueryString()}";
}
responseMessage = await this.http.PostJsonCustomAsync(uri, documents, cancellationToken: cancellationToken).ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Adds documents in batches with size specified with <paramref name="batchSize"/>.
/// </summary>
/// <param name="documents">Documents to add.</param>
/// <param name="batchSize">Size of documents batches while adding them.</param>
/// <param name="primaryKey">Primary key for the documents.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <typeparam name="T">Type of the document. Even though documents are schemaless in Meilisearch, making it typed helps in compile time.</typeparam>
/// <returns>Returns the task list.</returns>
public async Task<IEnumerable<TaskInfo>> AddDocumentsInBatchesAsync<T>(IEnumerable<T> documents, int batchSize = 1000, string primaryKey = default, CancellationToken cancellationToken = default)
{
var tasks = new List<TaskInfo>();
foreach (var chunk in documents.GetChunks(batchSize))
{
tasks.Add(await this.AddDocumentsAsync(chunk, primaryKey, cancellationToken).ConfigureAwait(false));
}
return tasks;
}
/// <summary>
/// Update documents.
/// </summary>
/// <param name="documents">Documents to update.</param>
/// <param name="primaryKey">Primary key for the documents.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <typeparam name="T">Type of document. Even though documents are schemaless in Meilisearch, making it typed helps in compile time.</typeparam>
/// <returns>Returns the task list.</returns>
public async Task<TaskInfo> UpdateDocumentsAsync<T>(IEnumerable<T> documents, string primaryKey = default, CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage;
string uri = $"/indexes/{this.Uid}/documents";
if (primaryKey != default)
{
uri = $"{uri}?{new { primaryKey = primaryKey }.ToQueryString()}";
}
var filteredDocuments = documents.RemoveNullValues();
responseMessage = await this.http.PutJsonCustomAsync(uri, filteredDocuments, cancellationToken: cancellationToken).ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Updates documents in batches with size specified with <paramref name="batchSize"/>.
/// </summary>
/// <param name="documents">Documents to update.</param>
/// <param name="batchSize">Size of documents batches while updating them.</param>
/// <param name="primaryKey">Primary key for the documents.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <typeparam name="T">Type of the document. Even though documents are schemaless in Meilisearch, making it typed helps in compile time.</typeparam>
/// <returns>Returns the task list.</returns>
public async Task<IEnumerable<TaskInfo>> UpdateDocumentsInBatchesAsync<T>(IEnumerable<T> documents, int batchSize = 1000, string primaryKey = default, CancellationToken cancellationToken = default)
{
var tasks = new List<TaskInfo>();
foreach (var chunk in documents.GetChunks(batchSize))
{
tasks.Add(await this.UpdateDocumentsAsync(chunk, primaryKey, cancellationToken).ConfigureAwait(false));
}
return tasks;
}
/// <summary>
/// Get document by its ID.
/// </summary>
/// <param name="documentId">Document identifier.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <typeparam name="T">Type of the document.</typeparam>
/// <returns>Returns the document, with the according type if the object is available.</returns>
public async Task<T> GetDocumentAsync<T>(string documentId, CancellationToken cancellationToken = default)
{
return await this.http.GetFromJsonAsync<T>($"/indexes/{this.Uid}/documents/{documentId}", cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Get document by its ID.
/// </summary>
/// <param name="documentId">Document Id for query.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <typeparam name="T">Type to return for document.</typeparam>
/// <returns>Type if the object is availble.</returns>
public async Task<T> GetDocumentAsync<T>(int documentId, CancellationToken cancellationToken = default)
{
return await this.GetDocumentAsync<T>(documentId.ToString(), cancellationToken);
}
/// <summary>
/// Get documents with the allowed Query Parameters.
/// </summary>
/// <param name="query">Query parameters. Supports limit, offset and attributes to retrieve.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <typeparam name="T">Type of the document.</typeparam>
/// <returns>Returns the list of documents.</returns>
public async Task<IEnumerable<T>> GetDocumentsAsync<T>(DocumentQuery query = default, CancellationToken cancellationToken = default)
{
string uri = $"/indexes/{this.Uid}/documents";
if (query != null)
{
uri = $"{uri}?{query.ToQueryString()}";
}
return await this.http.GetFromJsonAsync<IEnumerable<T>>(uri, cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Delete one document.
/// </summary>
/// <param name="documentId">Document identifier.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> DeleteOneDocumentAsync(string documentId, CancellationToken cancellationToken = default)
{
var httpresponse = await this.http.DeleteAsync($"/indexes/{this.Uid}/documents/{documentId}", cancellationToken).ConfigureAwait(false);
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Delete one document by its ID.
/// </summary>
/// <param name="documentId">document ID.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> DeleteOneDocumentAsync(int documentId, CancellationToken cancellationToken = default)
{
return await this.DeleteOneDocumentAsync(documentId.ToString(), cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Delete documents in batch.
/// </summary>
/// <param name="documentIds">List of documents identifier.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> DeleteDocumentsAsync(IEnumerable<string> documentIds, CancellationToken cancellationToken = default)
{
var httpresponse =
await this.http.PostAsJsonAsync($"/indexes/{this.Uid}/documents/delete-batch", documentIds, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Delete documents in batch.
/// </summary>
/// <param name="documentIds">List of document Id.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Return the task info.</returns>
public async Task<TaskInfo> DeleteDocumentsAsync(IEnumerable<int> documentIds, CancellationToken cancellationToken = default)
{
var docIds = documentIds.Select(id => id.ToString());
return await this.DeleteDocumentsAsync(docIds, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Delete all the documents in the index.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info.</returns>
public async Task<TaskInfo> DeleteAllDocumentsAsync(CancellationToken cancellationToken = default)
{
var httpresponse = await this.http.DeleteAsync($"/indexes/{this.Uid}/documents", cancellationToken)
.ConfigureAwait(false);
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the tasks.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns a list of the operations status.</returns>
public async Task<Result<IEnumerable<TaskInfo>>> GetTasksAsync(CancellationToken cancellationToken = default)
{
return await this.TaskEndpoint().GetIndexTasksAsync(this.Uid, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Get on task.
/// </summary>
/// <param name="taskUid">Uid of the task.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the tasks.</returns>
public async Task<TaskInfo> GetTaskAsync(int taskUid, CancellationToken cancellationToken = default)
{
return await this.TaskEndpoint().GetIndexTaskAsync(this.Uid, taskUid, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Search documents according to search parameters.
/// </summary>
/// <param name="query">Query Parameter with Search.</param>
/// <param name="searchAttributes">Attributes to search.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <typeparam name="T">Type parameter to return.</typeparam>
/// <returns>Returns Enumerable of items.</returns>
public async Task<SearchResult<T>> SearchAsync<T>(string query, SearchQuery searchAttributes = default(SearchQuery), CancellationToken cancellationToken = default)
{
SearchQuery body;
if (searchAttributes == null)
{
body = new SearchQuery { Q = query };
}
else
{
body = searchAttributes;
body.Q = query;
}
var responseMessage = await this.http.PostAsJsonAsync<SearchQuery>($"/indexes/{this.Uid}/search", body, MeilisearchClient.JsonSerializerOptions, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<SearchResult<T>>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Waits until the asynchronous task was done.
/// </summary>
/// <param name="taskUid">Unique identifier of the asynchronous task.</param>
/// <param name="timeoutMs">Timeout in millisecond.</param>
/// <param name="intervalMs">Interval in millisecond between each check.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of finished task.</returns>
public async Task<TaskInfo> WaitForTaskAsync(
int taskUid,
double timeoutMs = 5000.0,
int intervalMs = 50,
CancellationToken cancellationToken = default)
{
return await this.TaskEndpoint().WaitForTaskAsync(taskUid, timeoutMs, intervalMs, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets all the settings of an index.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns all the settings.</returns>
public async Task<Settings> GetSettingsAsync(CancellationToken cancellationToken = default)
{
return await this.http.GetFromJsonAsync<Settings>($"/indexes/{this.Uid}/settings", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Updates all the settings of an index.
/// The settings that are not passed in parameter are not overwritten.
/// </summary>
/// <param name="settings">Settings object.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> UpdateSettingsAsync(Settings settings, CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage =
await this.http.PostAsJsonAsync<Settings>($"/indexes/{this.Uid}/settings", settings, MeilisearchClient.JsonSerializerOptions, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Resets all the settings to their default values.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> ResetSettingsAsync(CancellationToken cancellationToken = default)
{
var httpResponse = await this.http.DeleteAsync($"/indexes/{this.Uid}/settings", cancellationToken).ConfigureAwait(false);
return await httpResponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the displayed attributes setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the displayed attributes setting.</returns>
public async Task<IEnumerable<string>> GetDisplayedAttributesAsync(CancellationToken cancellationToken = default)
{
return await this.http.GetFromJsonAsync<IEnumerable<string>>($"/indexes/{this.Uid}/settings/displayed-attributes", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Updates the displayed attributes setting.
/// </summary>
/// <param name="displayedAttributes">Collection of displayed attributes.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> UpdateDisplayedAttributesAsync(IEnumerable<string> displayedAttributes, CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage =
await this.http.PostAsJsonAsync<IEnumerable<string>>($"/indexes/{this.Uid}/settings/displayed-attributes", displayedAttributes, MeilisearchClient.JsonSerializerOptions, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Resets the displayed attributes setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> ResetDisplayedAttributesAsync(CancellationToken cancellationToken = default)
{
var httpresponse = await this.http.DeleteAsync($"/indexes/{this.Uid}/settings/displayed-attributes", cancellationToken)
.ConfigureAwait(false);
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the distinct attribute setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the distinct attribute setting.</returns>
public async Task<string> GetDistinctAttributeAsync(CancellationToken cancellationToken = default)
{
return await this.http.GetFromJsonAsync<string>($"/indexes/{this.Uid}/settings/distinct-attribute", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Updates the distinct attribute setting.
/// </summary>
/// <param name="distinctAttribute">Name of distinct attribute.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> UpdateDistinctAttributeAsync(string distinctAttribute, CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage =
await this.http.PostAsJsonAsync<string>($"/indexes/{this.Uid}/settings/distinct-attribute", distinctAttribute, MeilisearchClient.JsonSerializerOptions, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Resets the distinct attribute setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task Uid of the asynchronous task.</returns>
public async Task<TaskInfo> ResetDistinctAttributeAsync(CancellationToken cancellationToken = default)
{
var httpresponse = await this.http.DeleteAsync($"/indexes/{this.Uid}/settings/distinct-attribute", cancellationToken)
.ConfigureAwait(false);
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the filterable attributes setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the filterable attributes setting.</returns>
public async Task<IEnumerable<string>> GetFilterableAttributesAsync(CancellationToken cancellationToken = default)
{
return await this.http.GetFromJsonAsync<IEnumerable<string>>($"/indexes/{this.Uid}/settings/filterable-attributes", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Updates the filterable attributes setting.
/// </summary>
/// <param name="filterableAttributes">Collection of filterable attributes.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> UpdateFilterableAttributesAsync(IEnumerable<string> filterableAttributes, CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage =
await this.http.PostAsJsonAsync<IEnumerable<string>>($"/indexes/{this.Uid}/settings/filterable-attributes", filterableAttributes, MeilisearchClient.JsonSerializerOptions, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Resets the filterable attributes setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> ResetFilterableAttributesAsync(CancellationToken cancellationToken = default)
{
var httpresponse = await this.http.DeleteAsync($"/indexes/{this.Uid}/settings/filterable-attributes", cancellationToken)
.ConfigureAwait(false);
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the ranking rules setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the ranking rules setting.</returns>
public async Task<IEnumerable<string>> GetRankingRulesAsync(CancellationToken cancellationToken = default)
{
return await this.http.GetFromJsonAsync<IEnumerable<string>>($"/indexes/{this.Uid}/settings/ranking-rules", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Updates the ranking rules setting.
/// </summary>
/// <param name="rankingRules">Collection of ranking rules.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> UpdateRankingRulesAsync(IEnumerable<string> rankingRules, CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage =
await this.http.PostAsJsonAsync<IEnumerable<string>>($"/indexes/{this.Uid}/settings/ranking-rules", rankingRules, MeilisearchClient.JsonSerializerOptions, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Resets the ranking rules setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> ResetRankingRulesAsync(CancellationToken cancellationToken = default)
{
var httpresponse = await this.http.DeleteAsync($"/indexes/{this.Uid}/settings/ranking-rules", cancellationToken)
.ConfigureAwait(false);
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the searchable attributes setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the searchable attributes setting.</returns>
public async Task<IEnumerable<string>> GetSearchableAttributesAsync(CancellationToken cancellationToken = default)
{
return await this.http.GetFromJsonAsync<IEnumerable<string>>($"/indexes/{this.Uid}/settings/searchable-attributes", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Updates the searchable attributes setting.
/// </summary>
/// <param name="searchableAttributes">Collection of searchable attributes.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> UpdateSearchableAttributesAsync(IEnumerable<string> searchableAttributes, CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage =
await this.http.PostAsJsonAsync<IEnumerable<string>>($"/indexes/{this.Uid}/settings/searchable-attributes", searchableAttributes, MeilisearchClient.JsonSerializerOptions, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Resets the searchable attributes setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> ResetSearchableAttributesAsync(CancellationToken cancellationToken = default)
{
var httpresponse = await this.http.DeleteAsync($"/indexes/{this.Uid}/settings/searchable-attributes", cancellationToken)
.ConfigureAwait(false);
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the sortable attributes setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the sortable attributes setting.</returns>
public async Task<IEnumerable<string>> GetSortableAttributesAsync(CancellationToken cancellationToken = default)
{
return await this.http.GetFromJsonAsync<IEnumerable<string>>($"/indexes/{this.Uid}/settings/sortable-attributes", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Updates the sortable attributes setting.
/// </summary>
/// <param name="sortableAttributes">Collection of sortable attributes.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> UpdateSortableAttributesAsync(IEnumerable<string> sortableAttributes, CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage =
await this.http.PostAsJsonAsync<IEnumerable<string>>($"/indexes/{this.Uid}/settings/sortable-attributes", sortableAttributes, MeilisearchClient.JsonSerializerOptions, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Resets the sortable attributes setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> ResetSortableAttributesAsync(CancellationToken cancellationToken = default)
{
var httpresponse = await this.http.DeleteAsync($"/indexes/{this.Uid}/settings/sortable-attributes", cancellationToken)
.ConfigureAwait(false);
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Gets the stop words setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the stop words setting.</returns>
public async Task<IEnumerable<string>> GetStopWordsAsync(CancellationToken cancellationToken = default)
{
return await this.http.GetFromJsonAsync<IEnumerable<string>>($"/indexes/{this.Uid}/settings/stop-words", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Updates the stop words setting.
/// </summary>
/// <param name="stopWords">Collection of stop words.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> UpdateStopWordsAsync(IEnumerable<string> stopWords, CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage =
await this.http.PostAsJsonAsync<IEnumerable<string>>($"/indexes/{this.Uid}/settings/stop-words", stopWords, MeilisearchClient.JsonSerializerOptions, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Resets the stop words setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> ResetStopWordsAsync(CancellationToken cancellationToken = default)
{
var httpresponse = await this.http.DeleteAsync($"/indexes/{this.Uid}/settings/stop-words", cancellationToken)
.ConfigureAwait(false);
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the synonyms setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the synonyms setting.</returns>
public async Task<Dictionary<string, IEnumerable<string>>> GetSynonymsAsync(CancellationToken cancellationToken = default)
{
return await this.http.GetFromJsonAsync<Dictionary<string, IEnumerable<string>>>($"/indexes/{this.Uid}/settings/synonyms", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Updates the synonyms setting.
/// </summary>
/// <param name="synonyms">Collection of synonyms.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> UpdateSynonymsAsync(Dictionary<string, IEnumerable<string>> synonyms, CancellationToken cancellationToken = default)
{
HttpResponseMessage responseMessage =
await this.http.PostAsJsonAsync<Dictionary<string, IEnumerable<string>>>($"/indexes/{this.Uid}/settings/synonyms", synonyms, MeilisearchClient.JsonSerializerOptions, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Resets the synonyms setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> ResetSynonymsAsync(CancellationToken cancellationToken = default)
{
var httpresponse = await this.http.DeleteAsync($"/indexes/{this.Uid}/settings/synonyms", cancellationToken)
.ConfigureAwait(false);
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Get stats.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Return index stats.</returns>
public async Task<IndexStats> GetStatsAsync(CancellationToken cancellationToken = default)
{
return await this.http.GetFromJsonAsync<IndexStats>($"/indexes/{this.Uid}/stats", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Initializes the Index with HTTP client. Only for internal usage.
/// </summary>
/// <param name="http">HttpRequest instance used.</param>
/// <returns>The same object with the initialization.</returns>
// internal Index WithHttpClient(HttpClient client)
internal Index WithHttpClient(HttpClient http)
{
this.http = http;
return this;
}
/// <summary>
/// Create a local reference to a task, without doing an HTTP call.
/// </summary>
/// <returns>Returns a TaskEndpoint instance.</returns>
private TaskEndpoint TaskEndpoint()
{
if (this._taskEndpoint == null)
{
this._taskEndpoint = new TaskEndpoint();
this._taskEndpoint.WithHttpClient(this.http);
}
return this._taskEndpoint;
}
}
}