Skip to content

Commit 99c9678

Browse files
authored
add return id flag on import documents (#258)
1 parent c16b8f8 commit 99c9678

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

src/Typesense/ITypesenseClient.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ public interface ITypesenseClient
342342
/// Using lower amount will lower timeout risk, but increase number of requests made.
343343
/// If not specified, typesense server will default to 200.
344344
/// </param>
345+
/// <param name="returnId">Eanble to return the id fo the document in the response.</param>
345346
/// <returns>A collection of import responses.</returns>
346347
/// <exception cref="ArgumentNullException"></exception>
347348
/// <exception cref="ArgumentException"></exception>
@@ -356,7 +357,8 @@ Task<List<ImportResponse>> ImportDocuments(
356357
string documents,
357358
int batchSize = 40,
358359
ImportType importType = ImportType.Create,
359-
int? remoteEmbeddingBatchSize = null);
360+
int? remoteEmbeddingBatchSize = null,
361+
bool? returnId = null);
360362

361363
/// <summary>
362364
/// Batch import documents.
@@ -370,6 +372,7 @@ Task<List<ImportResponse>> ImportDocuments(
370372
/// Using lower amount will lower timeout risk, but increase number of requests made.
371373
/// If not specified, typesense server will default to 200.
372374
/// </param>
375+
/// <param name="returnId">Eanble to return the id fo the document in the response.</param>
373376
/// <returns>A collection of import responses.</returns>
374377
/// <exception cref="ArgumentNullException"></exception>
375378
/// <exception cref="ArgumentException"></exception>
@@ -384,7 +387,8 @@ Task<List<ImportResponse>> ImportDocuments(
384387
IEnumerable<string> documents,
385388
int batchSize = 40,
386389
ImportType importType = ImportType.Create,
387-
int? remoteEmbeddingBatchSize = null);
390+
int? remoteEmbeddingBatchSize = null,
391+
bool? returnId = null);
388392

389393
/// <summary>
390394
/// Batch import documents.
@@ -398,6 +402,7 @@ Task<List<ImportResponse>> ImportDocuments(
398402
/// Using lower amount will lower timeout risk, but increase number of requests made.
399403
/// If not specified, typesense server will default to 200.
400404
/// </param>
405+
/// <param name="returnId">Eanble to return the id fo the document in the response.</param>
401406
/// <returns>A collection of import responses.</returns>
402407
/// <exception cref="ArgumentNullException"></exception>
403408
/// <exception cref="ArgumentException"></exception>
@@ -412,7 +417,8 @@ Task<List<ImportResponse>> ImportDocuments<T>(
412417
IEnumerable<T> documents,
413418
int batchSize = 40,
414419
ImportType importType = ImportType.Create,
415-
int? remoteEmbeddingBatchSize = null);
420+
int? remoteEmbeddingBatchSize = null,
421+
bool? returnId = null);
416422

417423
/// <summary>
418424
/// Export all documents in a given collection.

src/Typesense/ImportResponse.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ public record ImportResponse
1313
[JsonPropertyName("document")]
1414
public string? Document { get; init; }
1515

16+
[JsonPropertyName("id")]
17+
public string? Id { get; init; }
18+
1619
[JsonConstructor]
17-
public ImportResponse(bool success, string? error = null, string? document = null)
20+
public ImportResponse(bool success, string? error = null, string? document = null, string? id = null)
1821
{
1922
Success = success;
2023
Error = error;
2124
Document = document;
25+
Id = id;
2226
}
2327
}

src/Typesense/TypesenseClient.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,20 +322,22 @@ public async Task<List<ImportResponse>> ImportDocuments(
322322
string documents,
323323
int batchSize = 40,
324324
ImportType importType = ImportType.Create,
325-
int? remoteEmbeddingBatchSize = null)
325+
int? remoteEmbeddingBatchSize = null,
326+
bool? returnId = null)
326327
{
327328
if (string.IsNullOrWhiteSpace(documents))
328329
throw new ArgumentException("cannot be null empty or whitespace", nameof(documents));
329330
using var stringContent = GetTextPlainStringContent(documents);
330-
return await ImportDocuments(collection, stringContent, batchSize, importType, remoteEmbeddingBatchSize).ConfigureAwait(false);
331+
return await ImportDocuments(collection, stringContent, batchSize, importType, remoteEmbeddingBatchSize, returnId).ConfigureAwait(false);
331332
}
332333

333334
private async Task<List<ImportResponse>> ImportDocuments(
334335
string collection,
335336
HttpContent documents,
336337
int batchSize = 40,
337338
ImportType importType = ImportType.Create,
338-
int? remoteEmbeddingBatchSize = null)
339+
int? remoteEmbeddingBatchSize = null,
340+
bool? returnId = null)
339341
{
340342
ArgumentNullException.ThrowIfNull(documents);
341343
if (string.IsNullOrWhiteSpace(collection))
@@ -355,6 +357,11 @@ private async Task<List<ImportResponse>> ImportDocuments(
355357
_ => throw new ArgumentException($"Could not handle {nameof(ImportType)} with name '{Enum.GetName(importType)}'", nameof(importType)),
356358
};
357359

360+
if (returnId is not null)
361+
{
362+
path += $"&return_id={returnId}";
363+
}
364+
358365
using var response = await _httpClient.PostAsync(path, documents).ConfigureAwait(false);
359366
if (!response.IsSuccessStatusCode)
360367
await GetException(response, default).ConfigureAwait(false);
@@ -373,7 +380,8 @@ public async Task<List<ImportResponse>> ImportDocuments(
373380
IEnumerable<string> documents,
374381
int batchSize = 40,
375382
ImportType importType = ImportType.Create,
376-
int? remoteEmbeddingBatchSize = null)
383+
int? remoteEmbeddingBatchSize = null,
384+
bool? returnId = null)
377385
{
378386
ArgumentNullException.ThrowIfNull(documents);
379387

@@ -386,7 +394,8 @@ public async Task<List<ImportResponse>> ImportDocuments<T>(
386394
IEnumerable<T> documents,
387395
int batchSize = 40,
388396
ImportType importType = ImportType.Create,
389-
int? remoteEmbeddingBatchSize = null)
397+
int? remoteEmbeddingBatchSize = null,
398+
bool? returnId = null)
390399
{
391400
ArgumentNullException.ThrowIfNull(documents);
392401

0 commit comments

Comments
 (0)