MIP1983
MIP1983

Reputation: 1374

Cosmos db sporadic 'requires a range index' exception when executing .net core client CreateDocumentQuery<T>

I'm getting sporadic/inconsistent behaviour from CosmosDb when executing queries from the .NET client.

return this.client.CreateDocumentQuery<T>(this.documentCollectionUri, options).Where(t => ...).Orderby(t => ...);

It's a simple query with a where and order by clause. I have done no explicit configuration of anything like indexes on this collection, it's the 'out of the box' configuration with a particular partition key specified.

Unfortunately I cannot see any pattern with what causes this to occur. The exact same code can execute dozens of times without fault but randomly it will throw this exception:

System.AggregateException occurred HResult=0x80131500 Message=One or more errors occurred. Source= StackTrace: at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at Microsoft.Azure.Documents.Linq.DocumentQuery1.d__31.MoveNext() at System.Collections.Generic.List1.AddEnumerable(IEnumerable1 enumerable) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at CompassDomain.Services.TravelRequestService.GetTravelRequests(TravelRequestSearchModel search) in C:\Users\Matty\source\repos\Compass\CompassDomain\Services\TravelRequestService.cs:line 542 at Compass.Controllers.TravelRequestController.Search(TravelRequestSearchModel search) in C:\Users\Matty\source\repos\Compass\Compass\Controllers\TravelRequestController.cs:line 242 at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()

Inner Exception 1: DocumentClientException: Message: {"Errors":["Order-by item requires a range index to be defined on the corresponding index path."]} ActivityId: 7307a098-6158-4437-be0d-cfbd6d1f8d23, Request URI: /apps/d6be8788-3942-411f-8d07-38c2e713953f/services/62737d1f-9d78-40ad-bf02-3852da0fa6ba/partitions/287201ef-5e4c-4276-9145-230e01c38d3d/replicas/131555532121431180s, RequestStats: ResponseTime: 2017-11-30T10:26:53.0446055Z, StoreReadResult: StorePhysicalAddress: rntbd://10.0.0.216:16700/apps/d6be8788-3942-411f-8d07-38c2e713953f/services/62737d1f-9d78-40ad-bf02-3852da0fa6ba/partitions/287201ef-5e4c-4276-9145-230e01c38d3d/replicas/131555532121431180s, LSN: 160, GlobalCommittedLsn: 159, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 0, IsGone: False, IsNotFound: False, RequestCharge: 1, ItemLSN: -1, ResourceType: Document, OperationType: Query , SDK: Microsoft.Azure.Documents.Common/1.17.101.1

Any thoughts or pointers on what might be the cause, or even just the best path to report this issue?

FYI, this is the indexing policy on the collection, it's unchanged from the default configuration:

{
  "indexingMode": "consistent",
  "automatic": true,
  "includedPaths": [
    {
      "path": "/*",
      "indexes": [
        {
          "kind": "Range",
          "dataType": "Number",
          "precision": -1
        },
        {
          "kind": "Hash",
          "dataType": "String",
          "precision": 3
        }
      ]
    }
  ],
  "excludedPaths": []
}

Upvotes: 1

Views: 433

Answers (1)

Nadeem Shaikh
Nadeem Shaikh

Reputation: 362

According to your inner-exception I will suggest you to make changes to indexing policy of your collection because with "Hash" kind of index on your collection you will not be able to use order by clause so change it to range as an example below and then check

{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
       {
            "path": "/*",
            "indexes": [
                {
                    "kind": "Range",
                    "dataType": "String",
                    "precision": -1
                },
                {
                    "kind": "Range",
                    "dataType": "Number",
                    "precision": -1
                }
            ]
        }
    ],
    "excludedPaths": []
}

Upvotes: 1

Related Questions