Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add return id flag on import documents #258

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Typesense/ITypesenseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ public interface ITypesenseClient
/// Using lower amount will lower timeout risk, but increase number of requests made.
/// If not specified, typesense server will default to 200.
/// </param>
/// <param name="returnId">Eanble to return the id fo the document in the response.</param>
/// <returns>A collection of import responses.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
Expand All @@ -356,7 +357,8 @@ Task<List<ImportResponse>> ImportDocuments(
string documents,
int batchSize = 40,
ImportType importType = ImportType.Create,
int? remoteEmbeddingBatchSize = null);
int? remoteEmbeddingBatchSize = null,
bool? returnId = null);

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

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

/// <summary>
/// Export all documents in a given collection.
Expand Down
6 changes: 5 additions & 1 deletion src/Typesense/ImportResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ public record ImportResponse
[JsonPropertyName("document")]
public string? Document { get; init; }

[JsonPropertyName("id")]
public string? Id { get; init; }

[JsonConstructor]
public ImportResponse(bool success, string? error = null, string? document = null)
public ImportResponse(bool success, string? error = null, string? document = null, string? id = null)
{
Success = success;
Error = error;
Document = document;
Id = id;
}
}
19 changes: 14 additions & 5 deletions src/Typesense/TypesenseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,20 +322,22 @@ public async Task<List<ImportResponse>> ImportDocuments(
string documents,
int batchSize = 40,
ImportType importType = ImportType.Create,
int? remoteEmbeddingBatchSize = null)
int? remoteEmbeddingBatchSize = null,
bool? returnId = null)
{
if (string.IsNullOrWhiteSpace(documents))
throw new ArgumentException("cannot be null empty or whitespace", nameof(documents));
using var stringContent = GetTextPlainStringContent(documents);
return await ImportDocuments(collection, stringContent, batchSize, importType, remoteEmbeddingBatchSize).ConfigureAwait(false);
return await ImportDocuments(collection, stringContent, batchSize, importType, remoteEmbeddingBatchSize, returnId).ConfigureAwait(false);
}

private async Task<List<ImportResponse>> ImportDocuments(
string collection,
HttpContent documents,
int batchSize = 40,
ImportType importType = ImportType.Create,
int? remoteEmbeddingBatchSize = null)
int? remoteEmbeddingBatchSize = null,
bool? returnId = null)
{
ArgumentNullException.ThrowIfNull(documents);
if (string.IsNullOrWhiteSpace(collection))
Expand All @@ -355,6 +357,11 @@ private async Task<List<ImportResponse>> ImportDocuments(
_ => throw new ArgumentException($"Could not handle {nameof(ImportType)} with name '{Enum.GetName(importType)}'", nameof(importType)),
};

if (returnId is not null)
{
path += $"&return_id={returnId}";
}

using var response = await _httpClient.PostAsync(path, documents).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
await GetException(response, default).ConfigureAwait(false);
Expand All @@ -373,7 +380,8 @@ public async Task<List<ImportResponse>> ImportDocuments(
IEnumerable<string> documents,
int batchSize = 40,
ImportType importType = ImportType.Create,
int? remoteEmbeddingBatchSize = null)
int? remoteEmbeddingBatchSize = null,
bool? returnId = null)
{
ArgumentNullException.ThrowIfNull(documents);

Expand All @@ -386,7 +394,8 @@ public async Task<List<ImportResponse>> ImportDocuments<T>(
IEnumerable<T> documents,
int batchSize = 40,
ImportType importType = ImportType.Create,
int? remoteEmbeddingBatchSize = null)
int? remoteEmbeddingBatchSize = null,
bool? returnId = null)
{
ArgumentNullException.ThrowIfNull(documents);

Expand Down