我有一个包含大约1500个文档的文档db集合。我最终的目标是使用上面的持久层构建一个api。我阅读了提及通过Feed选项使用请求延续令牌进行分页的文档(https://azure.microsoft.com/en-us/blog/documentdb-paging-support-with-top-and-more-query-improvements/)。
在控制台应用程序上使用以下方法来理解相同的问题:这里的问题或问题是,第一次执行查询时,continuation token = null但第二次执行查询时,continuation token =&# 34;某些价值"
所以我的问题是不应该在第一次设置延续令牌吗?或者我错过了一些明显的东西
//field
private static readonly FeedOptions DefaultOptions = new FeedOptions { EnableCrossPartitionQuery = true,MaxItemCount = 1};
//method
private static async Task<bool> QueryItemDocuments(string collectionLink)
{
IDocumentQuery<Item> query = client.CreateDocumentQuery<Item>(collectionLink, new SqlQuerySpec()
{
QueryText = "SELECT * FROM items",
},DefaultOptions).AsDocumentQuery();
var loopNumber = 0;
while (query.HasMoreResults)
{
loopNumber++;
var results=(await query.ExecuteNextAsync<Item>()).ToList();
Console.WriteLine("Count {0}",results.Count);
Console.WriteLine("Loopnumber :{0} Token {1}", loopNumber, DefaultOptions.RequestContinuation ?? "Null");
Console.WriteLine("Id: {0},Name: {1}", results[0].Id, results[0].Name);
}
return query.HasMoreResults;
}
sample results from console :
Count 1
Loopnumber :1 Token Null //why is this null?
Id: 1dbaf1d0-0125-11e0-88a8-005056957023,Name:Energy Drink
Count 1
Loopnumber :2 Token - RID:jq4gAIZqMwACBBBBBBB==#RT:1#TRC:1#PKRID:0
Id: 244a8230-0231-11e0-8c8b-005056957023,Name: Gluten Free Dish
编辑:理想情况下,我会在响应标头上发回延续令牌,允许客户端根据需要获取更多内容(在我的网络API上) - 所以我的方法需要
public Get(int? page=1,int? size=20)
所以当我用前20个发回响应时,我需要发回一个继续令牌,让客户端请求更多:所以如果第一次没有设置继续令牌 - 这意味着当我检索第一批20时,我该怎么做?
答案 0 :(得分:1)
你走在正确的轨道上。两个想法:
使用HasMoreResults
和ExecuteNext
时,无需手动处理延续令牌。 HasMoreResults
检查是否有续订令牌,当您致电ExecuteNext
时,会自动添加。
我怀疑你没有在第一轮看到延续的原因是它是传出的DefaultOptions
并且它在第二轮ExecuteNext
再次出局之前不会更新。我认为你在第二个循环上的输出实际上是第一个延续令牌。
答案 1 :(得分:0)
continuation token
查询结果时 - private static async Task QueryItemDocumentsUsingRestApi()
{
//About rest end points to query documents here https://msdn.microsoft.com/en-us/library/azure/mt670897.aspx
IEnumerable<string> continuationTokens = null;
var continToken = string.Empty;
var verb = "POST";
var resourceType = "docs";
var resourceLink = string.Format("dbs/{0}/colls/{1}/docs", DatabaseName, CollectionName);
var resourceId = string.Format("dbs/{0}/colls/{1}", DatabaseName, CollectionName)
var authHeader = GenerateAuthSignature(verb, resourceId, resourceType, authorizationKey, "master", "1.0"); // look here for how this is generated https://github.com/Azure/azure-documentdb-dotnet/blob/master/samples/rest-from-.net/Program.cs
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("x-ms-date", _utcDate);
client.DefaultRequestHeaders.Add("x-ms-version", "2015-08-06");
client.DefaultRequestHeaders.Remove("authorization");
client.DefaultRequestHeaders.Add("authorization", authHeader);
client.DefaultRequestHeaders.Add("x-ms-documentdb-isquery", "True");
client.DefaultRequestHeaders.Add("x-ms-max-item-count", "5");
client.DefaultRequestHeaders.Add("x-ms-continuation", string.Empty);
var qry = new RestSqlQuery { query = "SELECT * FROM items" };
var result = client.PostWithNoCharSetAsync(new Uri(new Uri(endpointUrl), resourceLink), qry).Result;
var content = await result.Content.ReadAsStringAsync();
var pagedList = JsonConvert.DeserializeObject<PagedList>(content);
if (result.Headers.TryGetValues("x-ms-continuation", out continuationTokens))
{
continToken = continuationTokens.FirstOrDefault();
}
}
}
确实设置在第一批的回复。以下是使用的示例代码:
<div class='form-group'>
<label class='control-label col-md-2 col-md-offset-2' for='id_email'>Contact</label>
<div class='col-md-6'>
<div class='form-group'>
<div class='col-md-11'>
<input class='form-control' id='id_email' placeholder='E-mail' type='text'>
</div>
</div>
<div class='form-group internal'>
<div class='col-md-11'>
<input class='form-control' id='id_phone' placeholder='Phone: (xxx) - xxx xxxx' type='text'>
</div>
</div>
</div>
</div>