1
votes

I have created a partitioned collection where I need a Range index on a DateTime field (as ISO-8601) and Hash indexes on a few other fields. These are the fields I will only filter on, so I do not need indexes on any other fields.

I have defined my indexes like so (using extension methods):

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 = "/*" });

The strange thing is when I then check the indexing policy I can see a lot of extra indexes created that make no sense to me. See below the definitions and my comments.

[{
    "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?

1

1 Answers

0
votes

Why would it create these indexes?

We may get the answer from the Azure official document.

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

According to my understanding, if we want to create index for properties,it will make sure that there is a string Hash Index or and Range number if we not create by oursevies.

For example:

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

The index would be:

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