Skip to content

Commit a4a2f2a

Browse files
committed
#665: Add support for sort in documents api
1 parent c29f7dd commit a4a2f2a

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
MEILISEARCH_VERSION=v1.10.0
1+
MEILISEARCH_VERSION=v1.16
22
PROXIED_MEILISEARCH=http://nginx/api/
33
MEILISEARCH_URL=http://meilisearch:7700

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,17 @@ if (results is PaginatedSearchResult<T> paginatedResults)
260260
}
261261
```
262262

263+
#### Search with sort
264+
265+
To get results sorted by attributes and preferred sorting order, the [Sort](https://www.meilisearch.com/docs/reference/api/search#sort) property must be defined.
266+
267+
```c#
268+
var results = await index.SearchAsync<T>(query, new SearchQuery()
269+
{
270+
Sort = new List<string> { "genre:asc", "name:desc" }
271+
});
272+
```
273+
263274
## 🤖 Compatibility with Meilisearch
264275

265276
This package guarantees compatibility with [version v1.x of Meilisearch](https://github.com/meilisearch/meilisearch/releases/latest), but some features may not be present. Please check the [issues](https://github.com/meilisearch/meilisearch-dotnet/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22+label%3Aenhancement) for more info.

src/Meilisearch/QueryParameters/DocumentsQuery.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,11 @@ public class DocumentsQuery
3131
/// </summary>
3232
[JsonPropertyName("filter")]
3333
public object Filter { get; set; }
34+
35+
/// <summary>
36+
/// An optional sort to apply
37+
/// </summary>
38+
[JsonPropertyName("sort")]
39+
public List<string> Sort { get; set; }
3440
}
3541
}

tests/Meilisearch.Tests/DocumentTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,70 @@ await index.GetDocumentsAsync<Movie>(new DocumentsQuery()
643643
documents.Results.Last().Id.Should().Be("11");
644644
}
645645

646+
[Fact]
647+
public async Task GetMultipleExistingDocumentWithAscSort()
648+
{
649+
var index = await _fixture.SetUpBasicIndexWithIntId("GetOneExistingDocumentWithIntegerIdTest");
650+
var updateSortable = await index.UpdateSortableAttributesAsync(new[] { "id" });
651+
updateSortable.TaskUid.Should().BeGreaterOrEqualTo(0);
652+
await index.WaitForTaskAsync(updateSortable.TaskUid);
653+
654+
var documents =
655+
await index.GetDocumentsAsync<MovieWithIntId>(new DocumentsQuery()
656+
{
657+
Sort = new List<string> { "id:asc" }
658+
});
659+
660+
Assert.Equal(7, documents.Results.Count());
661+
documents.Results.Should().BeInAscendingOrder(x => x.Id);
662+
}
663+
664+
[Fact]
665+
public async Task GetMultipleExistingDocumentWithDescSort()
666+
{
667+
var index = await _fixture.SetUpBasicIndexWithIntId("GetOneExistingDocumentWithIntegerIdTest");
668+
var updateSortable = await index.UpdateSortableAttributesAsync(new[] { "id" });
669+
updateSortable.TaskUid.Should().BeGreaterOrEqualTo(0);
670+
await index.WaitForTaskAsync(updateSortable.TaskUid);
671+
672+
var documents =
673+
await index.GetDocumentsAsync<MovieWithIntId>(new DocumentsQuery()
674+
{
675+
Sort = new List<string> { "id:desc" }
676+
});
677+
678+
Assert.Equal(7, documents.Results.Count());
679+
documents.Results.Should().BeInDescendingOrder(x => x.Id);
680+
}
681+
682+
[Fact]
683+
public async Task GetMultipleExistingDocumentWithMultiSort()
684+
{
685+
var index = await _fixture.SetUpBasicIndexWithIntId("GetOneExistingDocumentWithIntegerIdTest");
686+
var updateFilterable = await index.UpdateFilterableAttributesAsync(new[] { "genre" });
687+
updateFilterable.TaskUid.Should().BeGreaterOrEqualTo(0);
688+
await index.WaitForTaskAsync(updateFilterable.TaskUid);
689+
690+
var updateSortable = await index.UpdateSortableAttributesAsync(new[] { "genre", "name" });
691+
updateSortable.TaskUid.Should().BeGreaterOrEqualTo(0);
692+
await index.WaitForTaskAsync(updateSortable.TaskUid);
693+
694+
var documents =
695+
await index.GetDocumentsAsync<MovieWithIntId>(new DocumentsQuery()
696+
{
697+
Filter = "genre IN ['SF','Action']",
698+
Sort = new List<string> { "genre:asc", "name:desc" }
699+
});
700+
701+
Assert.Equal(4, documents.Results.Count());
702+
var first = documents.Results.First();
703+
first.Genre.Should().Be("Action");
704+
first.Name.Should().Be("Spider-Man");
705+
var last = documents.Results.Last();
706+
last.Genre.Should().Be("SF");
707+
last.Name.Should().Be("Harry Potter");
708+
}
709+
646710
[Fact]
647711
public async Task DeleteOneExistingDocumentWithStringId()
648712
{

tests/Meilisearch.Tests/ObjectExtensionsTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
using Meilisearch.Extensions;
56
using Meilisearch.QueryParameters;
@@ -157,5 +158,19 @@ public void QueryStringsWithListAreEqualsForDocumentsQuery(int? offset, int? lim
157158
Assert.Contains(String.Join(",", dq.Fields), actualQuery);
158159
}
159160
}
161+
162+
[Theory]
163+
[InlineData(null, new string[] { "id:asc", "title:desc" })]
164+
public void QueryStringsWithSortAreEqualForDocumentsQuery(int? limit, string[] sort)
165+
{
166+
var uri = "indexes/myindex/documents";
167+
var dq = new DocumentsQuery { Limit = limit, Sort = sort.ToList() };
168+
var actualQuery = dq.ToQueryString(uri: uri);
169+
170+
Assert.NotEmpty(actualQuery);
171+
Assert.NotNull(actualQuery);
172+
Assert.Contains("sort", actualQuery);
173+
Assert.Contains(String.Join(",", dq.Sort), actualQuery);
174+
}
160175
}
161176
}

0 commit comments

Comments
 (0)