0
votes

I am trying to exclude all and include only certain indexes on a collection in CosmosDb.

Here is my code:

// A property in the class...
public override IEnumerable<IncludedPath> CollectionIndexes => new List<IncludedPath>
{
     new IncludedPath{ Path= "/\"CreatedOnUtc\""  }, // I have tried "/CreatedOnUtc" as well
     // few more indexes...    
};

// Code in the CreateCollection method...
collection.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/*" }); // I have tried the code without this line as well
foreach (var path in CollectionIndexes) // Add indexes over certain properties only.
{
     collection.IndexingPolicy.IncludedPaths.Add(path);
}

When I run the code, I get this very exception ever single time:

Message: {"Errors":["The indexing path '/\"CreatedOnUtc\"' could not be accepted, failed near position '5'. Please ensure that the path is a valid path. Common errors include invalid characters or absence of quotes around labels."]} ActivityId: 2627fe24-64a0-46b3-bf9a-13cb160c17a7, Request URI: /apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: , SDK: Microsoft.Azure.Documents.Common/1.22.0.0, documentdb-dotnet-sdk/1.22.0 Host/64-bit MicrosoftWindowsNT/10.0.16299.0

When I change the path to "/CreatedOnUtc", I still get to see the same message:

Message: {"Errors":["The indexing path '/CreatedOnUtc' could not be accepted, failed near position '3'. Please ensure that the path is a valid path. Common errors include invalid characters or absence of quotes around labels."]} ActivityId: 30dc7429-df87-4080-912e-60aa731ae809, Request URI: /apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: , SDK: Microsoft.Azure.Documents.Common/1.22.0.0, documentdb-dotnet-sdk/1.22.0 Host/64-bit MicrosoftWindowsNT/10.0.16299.0

What am I missing out on? It seems to be something very basic but I have wasted 24 hours on this trying out various combinations!

I am using the C# Sql API to do so. I am trying this out on the CosmosDb Emulator on my local PC.

1
Why did you do "/\"id\"" and not simply "/id"?Nick Chapsas
I have tried "/id" as well..bit
I don't see you specifying the index type in the example. I am talking about something like this: Indexes = new Collection<Index> { new RangeIndex(DataType.String) { Precision = 20 } } });Nick Chapsas
I think I have tried that as well, I will try again and get back herebit
I see you have absolute paths. Have you tried adding the special characters needed at the end? For example the path for CreatedOnUtc should be /CreatedOnUtc/?Nick Chapsas

1 Answers

2
votes

It looks like you are missing the a necessary wildcard character ? at the end of the index path.

The ? character at the end of the index path is required to serve queries that query on this property. The character * should be used if you are planing to query on sub properties of the property for this path.

You can read more about this on the indexing policies documentation page