skip to Main Content

I am trying to create a container in which the only property that should be indexed is the /id property.

The documentation states:

If the indexing mode is set to consistent, the system properties id
and _ts are automatically indexed.

The documentation also states:

You can use this policy where the Time-to-Live (TTL) feature is active
but no other indexes are necessary to use Azure Cosmos DB as a pure
key-value store

… and shows the following indexing policy:

{
    "indexingMode": "consistent",
    "includedPaths": [],
    "excludedPaths": [{
        "path": "/*"
    }]
}

I am using the following C# code to set the indexing policy on a container at the time the container is created:

Microsoft.Azure.Cosmos.ContainerProperties container_props;

Microsoft.Azure.Cosmos.IndexingPolicy indexing_policy;


// Set container properties ...

container_props = new();

container_props.Id = "mycontainer";

container_props.PartitionKeyPath = "/id";


// Set indexing policy ...

indexing_policy = new IndexingPolicy();

indexing_policy.IndexingMode = IndexingMode.Consistent;

indexing_policy.IncludedPaths.Add( new IncludedPath { Path = "[]" } );

indexing_policy.ExcludedPaths.Add( new ExcludedPath { Path = ""/*"" } );

container_props.IndexingPolicy = indexing_policy;


// Check if container exists, and if not create it ...

await db.CreateContainerIfNotExistsAsync( container_props );

When the code is executed the following exception is raised:

Unable to initialize container: Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: ba5a76a0-e4ee-4aa5-9dd8-1e2bf8313c84; Reason: (Message: {"Errors":["The indexing path '[]' could not be accepted, failed near position '0'. Please ensure that the path is a valid path. Common errors include invalid characters or absence of quotes around labels."]}

I have also tried using:

indexing_policy.IncludedPaths.Add( new IncludedPath { Path = ""[]"" } );

… which also results in the following exception:

Unable to initialize container: Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 5dfe0a6a-7b76-4955-9bf5-f4f248cfc115; Reason: (Message: {"Errors":["The indexing path '"[]"' could not be accepted, failed near position '0'. Please ensure that the path is a valid path. Common errors include invalid characters or absence of quotes around labels."]}

Clearly, I am doing something wrong with regard to encoding the path. Any suggestions would be appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    The following code works:

    Microsoft.Azure.Cosmos.ContainerProperties container_props;
    
    Microsoft.Azure.Cosmos.IndexingPolicy indexing_policy;
    
    
    // Set container properties ...
    
    container_props = new();
    
    container_props.Id = "mycontainer";
    
    container_props.PartitionKeyPath = "/id";
    
    
    // Set indexing policy ...
    
    indexing_policy = new IndexingPolicy();
    
    indexing_policy.IndexingMode = IndexingMode.Consistent;
    
    indexing_policy.IncludedPaths.Clear();
    
    indexing_policy.ExcludedPaths.Clear();
    
    indexing_policy.ExcludedPaths.Add( new ExcludedPath { Path = "/*" } );
    
    container_props.IndexingPolicy = indexing_policy;
    
    
    // Check if container exists, and if not create it ...
    
    await db.CreateContainerIfNotExistsAsync( container_props );
    

    When executed, the indexing policy on the container shows as follows:

    {
        "indexingMode": "consistent",
        "automatic": true,
        "includedPaths": [],
        "excludedPaths": [
            {
                "path": "/*"
            },
            {
                "path": "/"_etag"/?"
            }
        ]
    }
    

    ... not sure why the _etag path is present (seems Azure is adding this path regardless).

    I was incorrectly assuming the "[]" was a JSON path, but the "includePaths" and "excludePaths" properties are collections of JSON paths - so the JSON "[]" shown in the examples means an empty collection.


  2. indexing_policy.IncludedPaths is already an empty collection. You don’t need to add anything to that. When it is serialized into a json the value of IncludedPaths will be translated to [].

    So simply remove the next line:

    indexing_policy.IncludedPaths.Add( new IncludedPath { Path = "[]" } );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search