【问题标题】:Different type of indexings on same field of a document in DocumentDbDocumentDb中文档同一字段的不同类型的索引
【发布时间】:2017-04-07 12:27:03
【问题描述】:

我有一个集合,其中文档具有不同类型的属性,例如字符串、数字、日期时间。

我的索引策略如下:

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

现在我想要的是在字符串字段上添加范围类型索引。 所以我编辑了我的索引策略:

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

但它没有工作:(

我知道可以在一个字段上添加多种类型的索引。谁能知道该怎么做?

【问题讨论】:

  • it did not work 您要进行范围查询还是按顺序查询?你能告诉我们什么不起作用吗?
  • 我需要按顺序进行排序,这就是我在字符串上添加 Range 类型索引的原因。但是这个甚至没有被天蓝色服务器验证。当我将此作为索引策略提交时,它显示错误。 @FredHan-MSFT

标签: azure azure-cosmosdb


【解决方案1】:

对于给定的数据类型,指定路径只能有一种索引类型(哈希或范围)。就支持的查询而言,Range 也是 Hash 的超集。 Hash 仅支持相等查询,但 Range 支持相等、范围和 order by 查询。

所以你只需要删除字符串上哈希的 JSON 块,例如如下所示,索引策略更新将成功:

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

【讨论】:

  • 我知道它有效。但问题是:Range 索引中的相等性并不像 Hash 索引那样快。这就是我尝试添加 Range 和 Hash 类型索引的原因。
  • Range 的问题是,ARRAY_CONTAINS 不再使用索引!您找到解决方法了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
相关资源
最近更新 更多