执行.net核心客户端CreateDocumentQuery <t>时,Cosmos db sporadic'需要范围索引'异常

时间:2017-11-30 10:48:33

标签: asp.net-mvc linq azure-cosmosdb

从.NET客户端执行查询时,我从CosmosDb得到零星/不一致的行为。

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

这是一个带有where和order by子句的简单查询。我没有对此集合上的索引进行任何显式配置,它是指定了特定分区键的“开箱即用”配置。

不幸的是,我看不出任何导致这种情况发生的模式。完全相同的代码可以执行数十次而不会出现错误但随机会抛出此异常:

  

发生了System.AggregateException     的HResult = 0x80131500     消息=发生了一个或多个错误。     来源=     堆栈跟踪:      在System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at Microsoft.Azure.Documents.Linq.DocumentQuery 1.d__31.MoveNext()      在System.Collections.Generic.List 1.AddEnumerable(IEnumerable 1可枚举)      在System.Linq.Enumerable.ToList [TSource](IEnumerable`1 source)      在C:\ Users \ Matty \ source \ repos \ Compass \ CompassDomain \ Services \ TravelRequestService.cs中的CompassDomain.Services.TravelRequestService.GetTravelRequests(TravelRequestSearchModel搜索):第542行      at Compass.Controllers.TravelRequestController.Search(TravelRequestSearchModel search)在C:\ Users \ Matty \ source \ repos \ Compass \ Compass \ Controllers \ TravelRequestController.cs:第242行      在Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target,Object []参数)      在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()

     

内部例外1:   DocumentClientException:Message:{“Errors”:[“Order-by item需要在相应的索引路径上定义范围索引。”]}   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

关于可能是什么原因的任何想法或指示,甚至是报告此问题的最佳途径?

仅供参考,这是集合的索引策略,与默认配置相同:

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

1 个答案:

答案 0 :(得分:1)

根据您的内部例外情况,我建议您更改收藏集的索引政策,因为&#34; Hash&#34;您的集合上的索引类型将无法使用order by子句,因此请将其更改为范围,如下所示,然后检查

| level 1 | level 2 | level 3 |
+---------+---------+---------+
|    2    |    3    |    1    |