如何在Python中为document_client对象发送的Cosmos DB查询设置延续令牌? [英] How do I set continuation tokens for Cosmos DB queries sent by document_client objects in Python?
问题描述
我有一个API,该API根据文档字段中显示的关键字来检索文档.我想对结果进行分页,以便可以将文档返回给发送请求的客户端,并允许他们在需要时请求更多文档.当我在Azure数据资源管理器中时,查询本身在浏览器中只需要一秒钟左右的时间,但是当我使用Python DocumentDB库进行查询时,查询只需要一分钟的时间.
I have an API that retrieves documents based on keywords that appear in document fields. I would like to paginate results so that I can return documents to a client sending a request, as well as allowing them to request more documents if they want. The query itself only takes a second or so in the browser when I am in the Azure Data Explorer, but it takes about a minute when I query using the Python DocumentDB library.
Looking at the Microsoft Cosmos DB REST API, it appears as if there are two tokens, x-ms_continuation and x-ms-max-item-count that are used.
似乎没有将这些作为条目放入document_client.QueryDocuments()
的 options 字典中的方法.
It doesn't appear that putting these as entries in the options dictionary of document_client.QueryDocuments()
does the trick.
在GitHub存储库中,阅读()方法引用 options 参数:
In the GitHub repository, the Read() method references the options parameter:
headers = base.GetHeaders(self,
initial_headers,
'get',
path,
id,
type,
options)
# Read will use ReadEndpoint since it uses GET operation
url_connection = self._global_endpoint_manager.ReadEndpoint
result, self.last_response_headers = self.__Get(url_connection,
path,
headers)
查看 base.py ,文件所在的位置,我看到了这两个代码块
Looking in base.py, where the file is located, I saw these two blocks of code
if options.get('continuation'):
headers[http_constants.HttpHeaders.Continuation] = (
options['continuation'])
if options.get('maxItemCount'):
headers[http_constants.HttpHeaders.PageSize] = options['maxItemCount']
这似乎与上面的两个参数相对应.但是,当我将它们设置为查询({'continuation':True,'maxItemCount':10}
)中的选项时,没有任何变化.
This would appear to correspond to the two parameters above. However, when I set them as options in the query ({'continuation':True,'maxItemCount':10}
), nothing changes.
最终查询看起来像
client.QueryDocuments(collection_link, query, {'continuation':True,'maxItemCount':10})
我还尝试使用字符串代替maxItemCount
的int.
I have also tried using a string instead of an int for maxItemCount
.
我在这里做错了什么?
The headers are the same as the two from the documentation above, from http_constants.py:
# Our custom DocDB headers
Continuation = 'x-ms-continuation'
PageSize = 'x-ms-max-item-count'
推荐答案
事实证明,需要从结果对象本身处理查询结果,并且应调用方法_fetch_function(options)
:
It turns out that the query results needed to be handled from the results object itself, and the method _fetch_function(options)
should be called:
q = client.QueryDocuments(collection_link, query, {'maxItemCount':10})
results_1 = q._fetch_function({'maxItemCount':10})
#this is a string representing a JSON object
token = results_1[1]['x-ms-continuation']
results_2 = q._fetch_function({'maxItemCount':10,'continuation':token})
数据包含在results_[n][0]
中,并且从调用返回的标头信息在results_[n][1]
中返回.
The data is contained in results_[n][0]
and header information returned from the call is returned in results_[n][1]
.
这篇关于如何在Python中为document_client对象发送的Cosmos DB查询设置延续令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!