简体   繁体   English

我可以在 Azure Cosmos DB 的复合索引中索引数组吗?

[英]Can I index an array in a composite index in Azure Cosmos DB?

I have a problem indexing an array in Azure Cosmos DB我在索引 Azure Cosmos DB 中的数组时遇到问题

I am trying to save this indexing policy via the portal我正在尝试通过门户保存此索引策略

{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
        {
            "path": "/*"
        }
    ],
    "excludedPaths": [
        {
            "path": "/\"_etag\"/?"
        }
    ],
    "compositeIndexes": [
        [
            {
                "path": "/DeviceId",
                "order": "ascending"
            },
            {
                "path": "/TimeStamp",
                "order": "ascending"
            },
            {
                "path": "/Items/[]/Name/?",
                "order": "ascending"
            },
            {
                "path": "/Items/[]/DoubleValue/?",
                "order": "ascending"
            }
        ]
    ]
}

I get the error "Failed to update container DeviceEvents: Message: {"code":"BadRequest","message":"Message: {"Errors":["The indexing path '\/Items\/[]\/Name\/?'我收到错误“无法更新容器 DeviceEvents:消息:{“code”:“BadRequest”,“message”:“消息:{“Errors”:[“索引路径 '\/Items\/[]\/Name \/?' could not be accepted, failed near position '8'."无法接受,在 position '8' 附近失败。”

This seems to be the array [] syntax that is giving an error.这似乎是给出错误的数组 [] 语法。

On a side note I am not sure what I am doing makes sense at all but I have a query that looks like this在旁注中,我不确定我所做的一切是否有意义,但我有一个看起来像这样的查询

SELECT SUM(de0["DoubleValue"])
FROM root JOIN de0 IN root["Items"]
WHERE root["ApplicationId"] = 57 AND root["DeviceId"] = 126 AND root["TimeStamp"] >= "2021-02-21T17:55:29.7389397Z" AND de0["Name"] = "Use Case"

Where ApplicationId is the partition key and the item saved looks like this其中 ApplicationId 是分区键,保存的项目如下所示

{
    "id": "59ab9323-26ca-436f-8d29-e1ddd826f025",
    "DeviceId": 3,
    "ApplicationId": 3,
    "RawData": "640F7A000A00E30142000000",
    "TimeStamp": "2021-02-20T18:36:52.833174Z",
    "Items": [
        {
            "Name": "Battery Status",
            "StringValue": "Full",
            "DoubleValue": null
        },
        {
            "Name": "Use Case",
            "StringValue": null,
            "DoubleValue": 12
        },
        {
            "Name": "Battery Voltage",
            "StringValue": null,
            "DoubleValue": 3.962
        },
        {
            "Name": "Rain Gauge Count",
            "StringValue": null,
            "DoubleValue": 10
        }
    ],
    "_rid": "CgdVAO7B0DNkAAAAAAAAAA==",
    "_self": "dbs/CgdVAA==/colls/CgdVAO7B0DM=/docs/CgdVAO7B0DNkAAAAAAAAAA==/",
    "_etag": "\"61008771-0000-0d00-0000-603156c50000\"",
    "_attachments": "attachments/",
    "_ts": 1613846213
}

I need to aggregate on some of these items in the array like say get MAX on temperature or something like this (using Use Case for test although it doesn't make sense).我需要汇总数组中的一些这些项目,比如在温度上获取 MAX 或类似的东西(使用用例进行测试,尽管它没有意义)。 I reasoned that if all the data in the query is in a single composite index the database would be able to do the aggregation without reading the documents themselves.我推断,如果查询中的所有数据都在单个复合索引中,那么数据库将能够在不读取文档本身的情况下进行聚合。 However I can't seem to add a composite index containing an array at all.但是我似乎根本无法添加包含数组的复合索引。

Yes, composite index can't contain an array path.是的,复合索引不能包含数组路径。 It should be a scalar value.它应该是一个标量值。

Unlike with included or excluded paths, you can't create a path with the /* wildcard.与包含或排除路径不同,您不能使用 /* 通配符创建路径。 Every composite path has an implicit /?每个复合路径都有一个隐含的 /? at the end of the path that you don't need to specify.在您不需要指定的路径的末尾。 Composite paths lead to a scalar value and this is the only value that is included in the composite index.复合路径导致一个标量值,这是复合索引中包含的唯一值。

Reference: https://docs.microsoft.com/en-us/azure/cosmos-db/index-policy#composite-indexes参考: https://docs.microsoft.com/en-us/azure/cosmos-db/index-policy#composite-indexes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM