1
votes

I am trying to return paging results the request from CosmosDB. I saw this example from here but I am not sure what to do with the response variable.

// Fetch query results 10 at a time.
var queryable = client.CreateDocumentQuery<Book>(collectionLink, new FeedOptions { MaxItemCount = 10 });
while (queryable.HasResults)
{
    FeedResponse<Book> response = await queryable.ExecuteNext<Book>();
}

Am I suppose to return it directly? Or do I have to do something further with the response variable? I tried to return the response variable directly and it's not working. Here's my code:

 public async Task<IEnumerable<T>> RunQueryAsync(string queryString)
        {

            var feedOptions = new FeedOptions { MaxItemCount = 3 };
            IQueryable<T> filter = _client.CreateDocumentQuery<T>(_collectionUri, queryString, feedOptions);
            IDocumentQuery<T> query = filter.AsDocumentQuery();
            var response = new FeedResponse<T>();
            while (query.HasMoreResults)
            {
                response = await query.ExecuteNextAsync<T>();
            }
            return response;
        }

Update: After reading @Evandro Paula's answer, I followed the URL and changed my implementation to below. But it is still giving me 500 status code:

 public async Task<IEnumerable<T>> RunQueryAsync(string queryString)
        {
            var feedOptions = new FeedOptions { MaxItemCount = 1 };
            IQueryable<T> filter = _client.CreateDocumentQuery<T>(_collectionUri, queryString, feedOptions);
            IDocumentQuery<T> query = filter.AsDocumentQuery();
            List<T> results = new List<T>();
            while (query.HasMoreResults)
            {
                foreach (T t in await query.ExecuteNextAsync())
                {
                    results.Add(t);
                }
            }
            return results;
        }

And here's the exception message:

Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception., Windows/10.0.17134 documentdb-netcore-sdk/1.9.1

Update 2: I added the EnableCrossPartitionQuery to true and I am able to get the response from CosmosDB. But I am not able to get the 1 item that I defined. Instead, I got 11 items.

2

2 Answers

3
votes

Find below a simple example on how to use the CosmosDB/SQL paged query:

    private static async Task Query()
    {
        Uri uri = new Uri("https://{CosmosDB/SQL Account Name}.documents.azure.com:443/");
        DocumentClient documentClient = new DocumentClient(uri, "{CosmosDB/SQL Account Key}");

        int currentPageNumber = 1;
        int documentNumber = 1;

        IDocumentQuery<Book> query = documentClient.CreateDocumentQuery<Book>("dbs/{CosmoDB/SQL Database Name}/colls/{CosmoDB/SQL Collection Name}", new FeedOptions { MaxItemCount = 10 }).AsDocumentQuery();

        while (query.HasMoreResults)
        {
            Console.WriteLine($"----- PAGE {currentPageNumber} -----");

            foreach (Book book in await query.ExecuteNextAsync())
            {
                Console.WriteLine($"[{documentNumber}] {book.Id}");
                documentNumber++;
            }

            currentPageNumber++;
        }
    }

Per exception described in your question Cross partition query is required but disabled, update the feed options as follows:

var feedOptions = new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true};

Find a more comprehensive example at https://github.com/Azure/azure-documentdb-dotnet/blob/d17c0ca5be739a359d105cf4112443f65ca2cb72/samples/code-samples/Queries/Program.cs#L554-L576.

1
votes

you are not specifying any where criteria for your specific item...so you are getting all results..try specifying criteria for the item (id , name etc) you are looking for. And keep in mind cross partition queries consume much more RUs n time, you can revisit architecture of your data model..Ideally always do queries with in same partition