diff --git a/src/Meilisearch/Index.Documents.cs b/src/Meilisearch/Index.Documents.cs index e21c0097..b5ee8c73 100644 --- a/src/Meilisearch/Index.Documents.cs +++ b/src/Meilisearch/Index.Documents.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; @@ -413,6 +414,29 @@ public async Task>> GetDocumentsAsync(Document } } + /// + /// Gets documents by list of Ids + /// + /// Ids to be searched + /// The cancellation token for this call. + /// Returns the list of documents. + public async Task>> GetDocumentsAsync(List 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>>(cancellationToken: cancellationToken) + .ConfigureAwait(false); + } + + /// /// Delete one document. /// diff --git a/src/Meilisearch/QueryParameters/DocumentsQuery.cs b/src/Meilisearch/QueryParameters/DocumentsQuery.cs index d5e2a191..7f637026 100644 --- a/src/Meilisearch/QueryParameters/DocumentsQuery.cs +++ b/src/Meilisearch/QueryParameters/DocumentsQuery.cs @@ -31,5 +31,11 @@ public class DocumentsQuery /// [JsonPropertyName("filter")] public object Filter { get; set; } + + /// + /// Gets or sets the list of Ids + /// + [JsonPropertyName("ids")] + public List Ids { get; set; } } } diff --git a/tests/Meilisearch.Tests/DocumentTests.cs b/tests/Meilisearch.Tests/DocumentTests.cs index e6f00076..9bdc6686 100644 --- a/tests/Meilisearch.Tests/DocumentTests.cs +++ b/tests/Meilisearch.Tests/DocumentTests.cs @@ -758,5 +758,31 @@ public async Task DeleteAllExistingDocuments() var docs = await index.GetDocumentsAsync(); 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 { "1", "3" }; + var fetched = await index.GetDocumentsAsync(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"); + } } }