1

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.

bit
  • 4,407
  • 1
  • 28
  • 50
  • Why did you do `"/\"id\""` and not simply `"/id"`? – Nick Chapsas Jul 04 '18 at 15:21
  • I have tried `"/id"` as well.. – bit Jul 04 '18 at 15:23
  • I don't see you specifying the index type in the example. I am talking about something like this: `Indexes = new Collection { new RangeIndex(DataType.String) { Precision = 20 } } });` – Nick Chapsas Jul 04 '18 at 15:26
  • I think I have tried that as well, I will try again and get back here – bit Jul 04 '18 at 15:27
  • Oh wait a second. Did you try removing the `id`? As you can see [here](https://stackoverflow.com/a/33989880/7523974) "the `id` is implicitly treated as the document's primary key - in which, id will always be indexed with uniqueness enforced." – Nick Chapsas Jul 04 '18 at 15:29
  • Yes I have tried that as well!, I will just update the question again... Give me few minutes – bit Jul 04 '18 at 15:33
  • 1
    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 Jul 04 '18 at 15:47
  • It worked! Why did the question mark help? – bit Jul 04 '18 at 16:22
  • Because it is a necessary wildcard character. It’s actually in the documentation. – Nick Chapsas Jul 04 '18 at 17:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174376/discussion-between-bit-and-nick-chapsas). – bit Jul 05 '18 at 03:02

1 Answers1

2

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

Nick Chapsas
  • 6,872
  • 1
  • 20
  • 29
  • Thank you! I had been through numerous articles and videos; yet I wonder how did I miss the `?` – bit Jul 05 '18 at 03:01