通过ARM模板更新CosmosDb索引策略

时间:2020-02-13 21:00:29

标签: azure-cosmosdb arm-template

我正在尝试使用ARM模板来更新cosmos容器的索引策略。我尝试了两种方法,一种方法是在ARM中声明容器时简单地声明索引策略。

{
      "apiVersion": "[variables('cosmosDbApiVersion')]",
      "type": "Microsoft.DocumentDB/databaseAccounts/apis/databases/containers",
      "dependsOn": [ /* resourceId */ ],
      "name": "/* containerName */",
      "properties": {
        "resource": {
          "id": "/* id */",
          "partitionKey": {
            "paths": [
              "/partitionKey"
            ],
            "kind": "Hash"
          },
          "indexes": [
            {
              "indexingMode": "consistent",
              "automatic": true,
              "includedPaths": [
                {
                  "path": "/*",
                  "indexes": [
                    {
                      "kind": "Range",
                      "dataType": "Number",
                      "precision": -1
                    },
                    {
                      "kind": "Hash",
                      "dataType": "String",
                      "precision": 3
                    }
                  ]
                }
              ]
            }
          ],
          "defaultTtl": "[variables('defaultTtlValueToEnableTtl')]"
        }
      }
    },

第二个是使用ARM这样部署容器设置:

{
      "apiVersion": "[variables('cosmosDbApiVersion')]",
      "type": "Microsoft.DocumentDB/databaseAccounts/apis/databases/containers/settings",
      "name": "[/* name */",
      "dependsOn": [ " /* container name */" ],
      "properties": {
        "resource": {
          "throughput": "/* some throughput */",
          "indexes": [
            {
              "indexingMode": "consistent",
              "automatic": true,
              "includedPaths": [
                {
                  "path": "/*",
                  "indexes": [
                    {
                      "kind": "Range",
                      "dataType": "Number",
                      "precision": -1
                    },
                    {
                      "kind": "Hash",
                      "dataType": "String",
                      "precision": 3
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    },

这两种技术都不会使部署失败,但索引策略不会更改。

不胜感激。

2 个答案:

答案 0 :(得分:0)

这是模板参考中的示例(看起来与您所做的略有不同):

"resource": {
  "id": "string",
  "indexingPolicy": {
    "automatic": "boolean",
    "indexingMode": "string",
    "includedPaths": [
      {
        "path": "string",
        "indexes": [
          {
            "dataType": "string",
            "precision": "integer",
            "kind": "string"
          }
        ]
      }
    ],
    "excludedPaths": [
      {
        "path": "string"
      }
    ],
    "spatialIndexes": [
      {
        "path": "string",
        "types": [
          "string"
        ]
      }
    ]
  },
  xxx
}

https://docs.microsoft.com/en-us/azure/templates/microsoft.documentdb/2019-08-01/databaseaccounts/sqldatabases/containers

答案 1 :(得分:0)

对于新容器或过去一年左右创建的容器,现在Cosmos资源提供程序将忽略范围和哈希索引类型。 ARM不验证索引策略,这就是模板成功部署的原因。

不推荐使用这些较新容器的哈希索引,因为新索引器中范围索引的性能超过了哈希索引提供的值,因此不再需要。

要创建/修改索引策略,请参阅下面的文章。索引策略有很多示例,它们可以实现从非常简单到复杂的所有策略,包括复合索引,空间索引和唯一键。

https://docs.microsoft.com/en-us/azure/cosmos-db/manage-sql-with-resource-manager#create-resource