Skip to content

Commit

Permalink
refactor PagedSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-nitehawk committed Jul 14, 2021
1 parent 6f4d859 commit 91ed07a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
28 changes: 14 additions & 14 deletions MongoDB.Entities/Builders/PagedSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ public PagedSearch<T, TProjection> IgnoreGlobalFilters()
}

/// <summary>
/// Run the aggregation search command in MongoDB server and get a page of results and total count
/// Run the aggregation search command in MongoDB server and get a page of results and total + page count
/// </summary>
/// <param name="cancellation">An optional cancellation token</param>
public async Task<(IReadOnlyList<TProjection> Results, int PageCount)> ExecuteAsync(CancellationToken cancellation = default)
public async Task<(IReadOnlyList<TProjection> Results, long TotalCount, int PageCount)> ExecuteAsync(CancellationToken cancellation = default)
{
var filterDef = Logic.MergeWithGlobalFilter(ignoreGlobalFilters, globalFilters, filter);

Expand Down Expand Up @@ -355,22 +355,22 @@ public PagedSearch<T, TProjection> IgnoreGlobalFilters()
? await DB.Collection<T>().Aggregate(options).Match(filterDef).Facet(countFacet, resultsFacet).SingleAsync(cancellation).ConfigureAwait(false)
: await DB.Collection<T>().Aggregate(session, options).Match(filterDef).Facet(countFacet, resultsFacet).SingleAsync(cancellation).ConfigureAwait(false);

var count = facetResult.Facets
.Single(x => x.Name == "_count")
.Output<AggregateCountResult>().FirstOrDefault()?.Count;

int totalPages =
count == null
? 0
: count <= pageSize
? 1
: (int)Math.Ceiling((double)(count / pageSize));
long matchCount = (
facetResult.Facets
.Single(x => x.Name == "_count")
.Output<AggregateCountResult>().FirstOrDefault()?.Count
) ?? 0;

int pageCount =
matchCount > 0 && matchCount <= pageSize
? 1
: (int)Math.Ceiling((double)(matchCount / pageSize));

var results = facetResult.Facets
.First(x => x.Name == "_results")
.Output<TProjection>();

return (results, totalPages);
return (results, matchCount, pageCount);
}
}
}
8 changes: 6 additions & 2 deletions MongoDB.Entities/MongoDB.Entities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
<Description>A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management.</Description>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://mongodb-entities.com</PackageProjectUrl>
<Version>20.19.1</Version>
<Version>20.19.2</Version>
<Copyright>Đĵ ΝιΓΞΗΛψΚ</Copyright>
<PackageReleaseNotes>- fixed innacurate page count when count &lt; page size in PagedSearch</PackageReleaseNotes>
<PackageReleaseNotes>- added DB.PagedSearch() builder for easy paging with $facet
- added IsInitialized property to watcher class
- added resume token support for watcher.ReStart()
- added more overloads to .Match() methods
- increased test coverage</PackageReleaseNotes>
<PackageId>MongoDB.Entities</PackageId>
<Product>MongoDB.Entities</Product>
<RepositoryUrl>https://github.com/dj-nitehawk/MongoDB.Entities</RepositoryUrl>
Expand Down
2 changes: 2 additions & 0 deletions Tests/TestIndexes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class Indexes
[TestMethod]
public async Task full_text_search_with_index_returns_correct_result()
{
await DB.DropCollectionAsync<Author>();

await DB.Index<Author>()
.Option(o => o.Background = false)
.Key(a => a.Name, KeyType.Text)
Expand Down
12 changes: 6 additions & 6 deletions Tests/TestPagedSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async Task empty_results()
{
var guid = Guid.NewGuid().ToString();

var (Results, PageCount) = await DB
var (Results, _, PageCount) = await DB
.PagedSearch<Book>()
.Match(b => b.ID == guid)
.Sort(b => b.ID, Order.Ascending)
Expand Down Expand Up @@ -44,7 +44,7 @@ public async Task got_results()

await SeedData(guid);

var (Results, PageCount) = await DB
var (Results, _, PageCount) = await DB
.PagedSearch<Book>()
.Match(b => b.Title == guid)
.Sort(b => b.ID, Order.Ascending)
Expand All @@ -69,7 +69,7 @@ public async Task with_projection()

await SeedData(guid);

var (Results, PageCount) = await DB
var (Results, _, PageCount) = await DB
.PagedSearch<Book, BookResult>()
.Match(b => b.Title == guid)
.Sort(b => b.ID, Order.Ascending)
Expand Down Expand Up @@ -101,7 +101,7 @@ await DB.Index<Genre>()

await list.SaveAsync();

var (Results, PageCount) = await DB
var (Results, _, PageCount) = await DB
.PagedSearch<Genre>()
.Match(Search.Full, "one eight nine")
.Project(p => new Genre { Name = p.Name, Position = p.Position })
Expand Down Expand Up @@ -135,7 +135,7 @@ await DB.Index<Genre>()

await list.SaveAsync();

var (Results, PageCount) = await DB
var (Results, _, PageCount) = await DB
.PagedSearch<Genre>()
.Match(Search.Full, "one eight nine")
.SortByTextScore()
Expand All @@ -158,7 +158,7 @@ public async Task exclusion_projection_works()
};
await author.SaveAsync();

var (res, _) = await DB.PagedSearch<Author>()
var (res, _, _) = await DB.PagedSearch<Author>()
.Match(a => a.ID == author.ID)
.Sort(a => a.ID, Order.Ascending)
.ProjectExcluding(a => new { a.Age, a.Name })
Expand Down

0 comments on commit 91ed07a

Please sign in to comment.