Reputation: 615
I working with documentDB Microsoft.Azure.Documents.Client
version 1.11.0.0.
I try to make paging in my query, every time take the next 20 best results.
But if I have for example only 25 elements in my db, In the first call I received the first 20, in the next call when I call with the continue token I received again 20 results instead of 5 results.
My code is:
public static async Task<Tuple<List<Student>, string, int>> StudentSearch(string text, string continuationToken)
{
List<Student> results = new List<Student>();
try
{
if (string.IsNullOrEmpty(continuationToken))
{
FeedOptions options = new FeedOptions { MaxItemCount = 20 };
var userQuery = _client.CreateDocumentQuery<Student>(
_uriStudentCollection, options).Where(x => x.StudentName.ToLower().Contains(text) || x.Description.ToLower().Contains(text)).OrderBy(x => x.Rank.TotalScore).AsDocumentQuery();
var feedResponse = await userQuery.ExecuteNextAsync<Student>();
foreach (var ad in feedResponse.AsEnumerable())
{
results.Add(ad);
}
string continuation = feedResponse.ResponseContinuation;
return new Tuple<Student>, string, int>(results, continuation, results.Count);
}
else
{
FeedOptions feedOptions = new FeedOptions { MaxItemCount = 20, RequestContinuation = continuationToken };
var userQuery = _client.CreateDocumentQuery<Student>(
_uriStudentCollection, feedOptions).Where(x => x.StudentName.ToLower().Contains(text) || x.Description.ToLower().Contains(text)).OrderBy(x => x.Rank.TotalScore).AsDocumentQuery();
var feedResponse = await userQuery.ExecuteNextAsync<Student>();
foreach (var ad in feedResponse.AsEnumerable())
{
results.Add(ad);
}
string continuation = feedResponse.ResponseContinuation;
return new Tuple<List<Student>, string, int>(results, continuation, results.Count);
}
}
catch (Exception ex)
{
return null;
}
}
Upvotes: 0
Views: 334
Reputation: 24569
I can't repro the issue on my side with you mentioned code. It seems that there is no issue in you mentioned code.
I test your code with Microsoft.Azure.Documents Version 1.14.1 on my side, it works correctly.
If it is possible please have a try to update the Microsoft.Azure.Documents to the lastest one. It seems that there is some redundance in your code. We could use the following code to replace it.
public static async Task<Tuple<List<Student>, string, int>> StudentSearch( string text, string continuationToken = null)
{
var results = new List<Student>();
FeedOptions options = new FeedOptions
{
MaxItemCount = 20
};
if (!string.IsNullOrEmpty(continuationToken))
{
options.RequestContinuation = continuationToken;
}
var userQuery = _client.CreateDocumentQuery<Student>(
_uriStudentCollection, feedOptions).Where(x => x.StudentName.ToLower().Contains(text) || x.Description.ToLower().Contains(text)).OrderBy(x => x.Rank.TotalScore).AsDocumentQuery();
var feedResponse = await userQuery.ExecuteNextAsync<Student>();
foreach (var ad in feedResponse.AsEnumerable())
{
results.Add(ad);
}
string continuation = feedResponse.ResponseContinuation;
return new Tuple<List<Student>, string, int>(results, continuation, results.Count);
}
Upvotes: 1