Skip to content
Draft
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
24 changes: 24 additions & 0 deletions src/Meilisearch/Index.Documents.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -413,6 +414,29 @@ public async Task<ResourceResults<IEnumerable<T>>> GetDocumentsAsync<T>(Document
}
}

/// <summary>
/// Gets documents by list of Ids
/// </summary>
/// <param name="ids">Ids to be searched</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the list of documents.</returns>
public async Task<ResourceResults<IEnumerable<T>>> GetDocumentsAsync<T>(List<string> ids, CancellationToken cancellationToken = default)
{
if (ids == null || ids.Count == 0)
throw new ArgumentException("At least one ID must be provided.", nameof(ids));

var query = new DocumentsQuery { Ids = ids };

var uri = $"indexes/{Uid}/documents/fetch";
var result = await _http.PostAsJsonAsync(uri, query, Constants.JsonSerializerOptionsRemoveNulls,
cancellationToken: cancellationToken).ConfigureAwait(false);

return await result.Content
.ReadFromJsonAsync<ResourceResults<IEnumerable<T>>>(cancellationToken: cancellationToken)
.ConfigureAwait(false);
}


/// <summary>
/// Delete one document.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Meilisearch/QueryParameters/DocumentsQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ public class DocumentsQuery
/// </summary>
[JsonPropertyName("filter")]
public object Filter { get; set; }

/// <summary>
/// Gets or sets the list of Ids
/// </summary>
[JsonPropertyName("ids")]
public List<string> Ids { get; set; }
}
}
26 changes: 26 additions & 0 deletions tests/Meilisearch.Tests/DocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -758,5 +758,31 @@ public async Task DeleteAllExistingDocuments()
var docs = await index.GetDocumentsAsync<Movie>();
docs.Results.Should().BeEmpty();
}

[Fact]
public async Task CanFetchMultipleDocumentsByIds()
{
var indexUID = nameof(CanFetchMultipleDocumentsByIds);
var index = _client.Index(indexUID);

var documents = new[]
{
new Movie { Id = "1", Name = "The Matrix" },
new Movie { Id = "2", Name = "Inception" },
new Movie { Id = "3", Name = "Arrival" }
};

var task = await index.AddDocumentsAsync(documents);
await index.WaitForTaskAsync(task.TaskUid);

// Fetch by IDs
var idsToFetch = new List<string> { "1", "3" };
var fetched = await index.GetDocumentsAsync<Movie>(idsToFetch);
var resultDocs = fetched.Results.ToList();

Assert.Equal(2, resultDocs.Count);
Assert.Contains(resultDocs, d => d.Id == "1" && d.Name == "The Matrix");
Assert.Contains(resultDocs, d => d.Id == "3" && d.Name == "Arrival");
}
}
}