【问题标题】:Composite index for optional field in CosmosCosmos 中可选字段的复合索引
【发布时间】:2020-04-27 16:25:09
【问题描述】:

我在 Cosmos DB 中有一个集合,其中包含不同类型(和模式)的文档:

{
"partKey": "...",
"type": "type1",
"data": {
    "field1": 123,
    "field2": "sdfsdf"
  }
}
{
"partKey": "...",
"type": "type2",
"data": {
    "field3": ["123", "456", "789"]
  }
}

我正在尝试创建一个复合索引[/type, /data/field3/[]/?],但遇到了一个问题:

The indexing path '\\/data\\/field3\\/[]\\/?' could not be accepted, failed near position '15'. Please ensure that the path is a valid path. Common errors include invalid characters or absence of quotes around labels

【问题讨论】:

    标签: azure-cosmosdb composite-index


    【解决方案1】:

    我们不支持 Cosmos DB 中复合索引的通配符。这是一个composite index sample 作为参考。

    我们将更新我们的文档以使其更加清晰。我查看了这些内容,但我们今天没有记录。

    谢谢。

    【讨论】:

    • 不确定我是否理解这一点。在我的示例中,通配符到底是什么? docs.microsoft.com/en-us/azure/cosmos-db/index-policy - 位置数组的示例
    • 那个?在您的索引策略中。有关如何定义这些的详细信息,请参阅我们关于复合索引的文档。谢谢
    【解决方案2】:

    在复合索引中,您只需要指定要索引的路径,而不是值,因此对于您的示例:

    "compositeIndexes":[  
            [  
                {  
                    "path":"/type",
                    "order":"ascending"
                },
                {  
                    "path":"/data/field3",
                    "order":"descending"
                }
            ]
        ]
    

    只需指定查询所需的订单类型(我只是以这些为例)。

    对于在数据属性下具有不同属性的不同文档,我相信您必须为所需的每个用例添加每个复合索引,因为复合索引不支持通配符,因此您需要添加:

    /data/field1 /data/field2 etc etc
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      相关资源
      最近更新 更多