【问题标题】:Indexing policy is not being created along with collection in Cosmos DB未与 Cosmos DB 中的集合一起创建索引策略
【发布时间】:2019-05-27 07:55:34
【问题描述】:

我正在使用 Java SDK 编写数据库创建脚本,但未按预期创建索引策略(和 documented)。

  • 使用的 JAVA SDK:com.microsoft.azure:azure-documentdb:2.4.0
  • 适用于 Windows 的 Azure Cosmos DB 模拟器 2.2.2
  • 当前使用 SQL 帐户在 Azure 门户中安装 Cosmos DB

我使用 JAVA 库构建集合创建请求,结果(在实际请求之前)如下所示 (DocumentCollection::toJson()):

{
    "uniqueKeyPolicy": {},
    "partitionKey":
    {
        "kind": "Hash",
        "paths": ["/playerId"]
    },
    "indexingPolicy":
    {
        "indexingMode": "Consistent",
        "automatic": true,
        "includedPaths": [
            {
                "path": "/gameId/?",
                "indexes": [
                    {
                        "kind": "Range",
                        "dataType": "String"
                    }
                ]
            },
            {
                "path": "/playerId/?",
                "indexes": [
                    {
                        "kind": "Range",
                        "dataType": "String"
                    }
                ]
            },
            {
                "path": "/date/*",
                "indexes": [
                    {
                        "kind": "Range",
                        "dataType": "String"
                    }
                ]
            }
        ],
        "excludedPaths": [
            {
                "path": "/*"
            }
        ]
    },
    "id": "Games"
}

请求成功完成,但如果我使用数据浏览器或DocumentClient.readCollection 检查实际的索引策略,它看起来像这样:

{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
        {
            "path": "/gameId/?",
            "indexes": []
        },
        {
            "path": "/playerId/?",
            "indexes": []
        },
        {
            "path": "/date/*",
            "indexes": []
        }
    ],
    "excludedPaths": [
        {
            "path": "/*"
        },
        {
            "path": "/\"_etag\"/?"
        }
    ]
}

如您所见,索引定义的数组为空。 然后,如果我从 SDK 生成的输出中复制索引策略并将其手动粘贴到模拟器或门户“缩放和设置”窗口中以创建集合,则更新结果如下:

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

因此正在创建索引(尽管有额外的Number 条目,如提到的here)。 我的创建脚本有问题吗?

【问题讨论】:

    标签: indexing azure-cosmosdb


    【解决方案1】:

    请关注github source code 并参考下面对我有用的代码。

    import com.microsoft.azure.documentdb.*;
    
    import java.util.Collection;
    import java.util.List;
    
    public class CreateCollectionTest {
        //
        static private String YOUR_COSMOS_DB_ENDPOINT = "https://***.documents.azure.com:443/";
        static private String YOUR_COSMOS_DB_MASTER_KEY = "***";
    
        public static void main(String[] args) throws DocumentClientException {
    
            DocumentClient client = new DocumentClient(
                    YOUR_COSMOS_DB_ENDPOINT,
                    YOUR_COSMOS_DB_MASTER_KEY,
                    new ConnectionPolicy(),
                    ConsistencyLevel.Session);
    
            DocumentCollection collection = new DocumentCollection();
            collection.set("id", "game");
    
    
            IndexingPolicy indexingPolicy = new IndexingPolicy();
            Collection<IncludedPath> includedPaths = new ArrayList<IncludedPath>();
            IncludedPath includedPath = new IncludedPath();
            includedPath.setPath("/gameId/?");
            Collection<Index> indexes = new ArrayList<Index>();
    
            Index stringIndex = Index.Range(DataType.String);
            stringIndex.set("precision", -1);
            indexes.add(stringIndex);
    
            Index numberIndex = Index.Range(DataType.Number);
            numberIndex.set("precision", -1);
            indexes.add(numberIndex);
    
            includedPath.setIndexes(indexes);
            includedPaths.add(includedPath);
            indexingPolicy.setIncludedPaths(includedPaths);
    
            collection.setIndexingPolicy(indexingPolicy);
    
            ResourceResponse<DocumentCollection> createColl = client.createCollection("dbs/db", collection, null);
    
    
        }
    }
    

    【讨论】:

    • 1.请添加 excludedPath 策略,因为没有它,您的示例将失败。 2. 在您使用sn-p 创建您的集合后,能否请您粘贴DB explorer 的Scale and Settings 的结果?你的例子也给了我一个空数组:(
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多