通过continue令牌进行的DocumentDB分页工作不正常

时间:2017-06-22 15:15:07

标签: c# azure azure-cosmosdb

我使用documentDB Microsoft.Azure.Documents.Client版本1.11.0.0。 我尝试在我的查询中进行分页,每次都取下20个最佳结果。 但是,如果我在我的数据库中只有25个元素,在第一个调用中我收到了前20个,在下一次调用时,当我使用continue标记调用时,我再次收到20个结果而不是5个结果。 我的代码是:

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;
    }
}

1 个答案:

答案 0 :(得分:1)

我不能用你提到的代码重新解决这个问题。您提到的代码似乎没有问题。

我使用Microsoft.Azure.Documents版本 1.14.1 测试您的代码,它可以正常运行。

enter image description here

如果可能,请尝试将Microsoft.Azure.Documents更新为最新版本。似乎代码中存在一些冗余。我们可以使用以下代码替换它。

 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);
         }