From 7e20f04731b8602f8068070e79635fb7164a6306 Mon Sep 17 00:00:00 2001 From: arashdann Date: Mon, 21 Apr 2025 13:01:14 +0200 Subject: [PATCH 1/2] feat: add GetDocumentsAsync overload to fetch documents by IDs --- src/Meilisearch/Index.Documents.cs | 24 +++++++++++++++++++ .../QueryParameters/DocumentsQuery.cs | 6 +++++ 2 files changed, 30 insertions(+) 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; } } } From 9a9e49340fa2f1004287976ab0ad913e7afa966c Mon Sep 17 00:00:00 2001 From: arashdann Date: Mon, 21 Apr 2025 13:18:18 +0200 Subject: [PATCH 2/2] Added a unit test for fetching documents using multiple Ids --- tests/Meilisearch.Tests/DocumentTests.cs | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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"); + } } }