avenmia
avenmia

Reputation: 2605

How do I define a correct indexing path in cosmos db using the nodejs sdk?

I'm currently running into issues trying to define the indexing policy for one of my cosmosdb containers. I have a container in cosmosdb that keeps data related to the user's session. I'm using the node sdk to define my containers, partition keys, and indexing policy. Here is how I'm defining the indexing policy and partition key for the sessions container.

else if(containerId.includes("sessions"))
    {
        indexingPolicy = {
            includedPaths: [ {"path" : "/startTime/"} ]
        };
        partitionKey = { paths: ["/id"] };
    }

When I make a POST request and the container does not exist, it will create the container based on the policies I defined in the configuration file. However, when I try to post a new session I get a variety of errors based on the indexing policy. Here is what I've tried so far and the errors I've gotten.

{"path" : "/startTime/"} --> The indexing path '\/startTime\/' could not be accepted, failed near position '11'. Please ensure that the path is a valid path. Common errors include invalid characters or absence of quotes around labels.

{"path" : "/startTime/*"} --> "The special mandatory indexing path \"\/\" is not provided in any of the path type sets. Please provide this path in one of the sets."

{"path" : "/startTime/?"} --> "The special mandatory indexing path \"\/\" is not provided in any of the path type sets. Please provide this path in one of the sets."

{"path" : "/startTime"} --> "The indexing path '\/startTime' could not be accepted, failed near position '10'. Please ensure that the path is a valid path. Common errors include invalid characters or absence of quotes around labels."

Any help for on how to correctly define the indexing policy would be greatly appreciated!

Upvotes: 2

Views: 1536

Answers (1)

Steve Johnson
Steve Johnson

Reputation: 8660

Any indexing policy has to include the root path /* as either an included or an excluded path

a path leading to a scalar value (string or number) ends with /?

Just for your code working, you can try this:

const indexingPolicy = {
    includedPaths: [ {"path" : "/*"},{"path" : "/startTime/?"} ]
}

await container.replace({
    id: containerId,
    partitionKey: { paths: ["/id"] },
    indexingPolicy: indexingPolicy
});

But as far as I know, if you has {"path" : "/*"} in your includedPaths, there is no need to add {"path" : "/startTime/?"}. Because /* can include "/startTime/?".

Ref: Indexing policies in Azure Cosmos DB

Upvotes: 3

Related Questions