Azure/azure-cosmos-python

Ability to get/set continuation token

Closed this issue · 3 comments

Greetings. I am currently developing an API that queries documents in a collection, and returns the results. I would like to add functionality for obtaining the next page. From what I understand about DocumentDB, a continuation token is returned, in addition to the queried documents, if there are more results.

My thoughts are: obtain the token after the results are retrieved and return the token to the client. The client may then use the token in the next request to obtain the next page.

After doing some digging, I found a section of the source code that sets the execution context's continuation token. Working backwards, I ended up writing code like this:

query = {'query': 'select * from c '}
options = {'continuation': continuation} if continuation else {}

query_iterable = client.QueryDocuments(collection_link, query, options)
results = query_iterable.fetch_next_block()
continuation = query_iterable._options.get('continuation')

Unfortunately this does not seem to work for several reasons:

1.) First off, the value for continuation is always None, unless I make another fetch_next_block() call after the first one. (I have several thousand documents so there is an expected next page of results)
2.) Even though I set continuation in the options, I still get the same results as before, and the value obtained for the continuation token is the same as the value that I originally sent.

My questions:

1.) What am I doing wrong in my code? It seems fairly straightforward, and yet it doesn't accomplish what I am trying to do.
2.) Clearly this library handles this behavior internally, so are there any plans for exposing this functionality for general use?

Any suggestions/help is greatly appreciated. Thanks!

@george-deamont You can get the results in pages using fetch_next_block however that requires keeping an instance of QueryIterator in memory since we currently do not expose the continuation token to the user's code.

Exposing continuation token to the user's code for queries against partitioned collection is in our roadmap, but for the time being please keep an instance of QueryIterator and use fetch_next_block to the results in pages.

I have the same expectations as @george-deamont .
Reading and updating the SDK code on my side, I have been able to achieve what I wanted, modifying this line:

if 'continuation' in options and self._is_change_feed:

options["continuation"] is overridden by default. I don't really understand why this behavior is not allowed to the end user.

My use-case is to write a paginated API endpoint which rely on cosmos-db for pagination purpose. The continuation token is a good candidate for my use-case.
@moderakh, keeping the QueryIterator is not possible in my case as I am writing a stateless service which will be deployed on several instances.
Any clue to help me implement my use-case, a paginated endpoint using markers ?

Closing this issue due to age. Active development of our Python SDK has moved to https://github.com/Azure/azure-sdk-for-python. We have also released 4.0 which includes many updates and new features. If this issue still needs our attention, please reopen it in the new repository.