DocumentDB 创建额外的索引

DocumentDB creating extra indexes

我创建了一个分区集合,我需要在日期时间字段(如 ISO-8601)上使用范围索引,在其他几个字段上使用哈希索引。这些是我将仅过滤的字段,因此我不需要任何其他字段的索引。

我已经这样定义了我的索引(使用扩展方法):

indexingPolicy.IncludedPaths.Add("/playerId/?", new HashIndex(DataType.Number));
indexingPolicy.IncludedPaths.Add("/category/?", new HashIndex(DataType.String));
indexingPolicy.IncludedPaths.Add("/dateTime/?", new RangeIndex(DataType.String));
indexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/*" });

奇怪的是,当我检查索引策略时,我可以看到创建了很多对我来说毫无意义的额外索引。请参阅下面的定义和我的评论。

[{
    "path" : "/playerId/?",
    "indexes" : [{
            "kind" : "Hash",
            "dataType" : "Number",
            "precision" : 3
        }, {
            "kind" : "Hash",
            "dataType" : "String", <<< It created an extra String Hash index
            "precision" : 3
        }
    ]
}, {
    "path" : "/category/?",
    "indexes" : [{
            "kind" : "Hash",
            "dataType" : "String",
            "precision" : 3
        }, {
            "kind" : "Range",
            "dataType" : "Number", <<< It created an extra Number Range index, this field is not numeric and I wanted a String Hash index only
            "precision" : -1
        }
    ]
}, {
    "path" : "/dateTime/?",
    "indexes" : [{
            "kind" : "Range",
            "dataType" : "String",
            "precision" : -1
        }, {
            "kind" : "Range",
            "dataType" : "Number", <<< It created an extra Number Range index, this field is not numeric and I wanted a String Range index only
            "precision" : -1
        }
    ]
}, {

为什么要创建这些索引?

Why would it create these indexes?

我们或许可以从Azure官方那里得到答案document

By default, DocumentDB indexes all string properties within documents consistently with a Hash index, and numeric properties with a Range index.

根据我的理解,如果我们要为属性创建索引,如果我们不自己创建,它会确保有一个字符串哈希索引或和范围编号。

例如:

IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.Number) 

索引为:

"indexes" : [{
            "kind" : "Hash",
            "dataType" : "String",
            "precision" : 3
        }, {
            "kind" : "Range",
            "dataType" : "Number",
            "precision" : -1
        }