Join Chander Dhall for an in-depth discussion in this video Manual indexing at collection level, part of Cosmos DB: Import, Manipulate, Index and Query.
- [Narrator] Now that you've seen indexing at the document level, we'll go ahead and show you indexing at the collection level. We're going to use Control + dot and have Visual Studio create the function for us. We'll get the collectionUrl using UriFactory, CreateDocumentCollectionUri, databaseId, collectionId.
And now we're going to take the collectionSpecifications, and it's just a new object of type documentCollection. First, it needs the collectionId, and then we can add other values. First thing to do would be to make sure that we turn off the automatic indexing at the collection level. Then we're going to create the collection.
And I could reuse the code we had earlier, where I could say client.CreateDocumentCollectionAsync, use the UriFactory, and add the collectionSpecifications here. And we don't need this. We can also add request options. Again, there's no need to change anything at this moment. We're going to create a Document very similar to the previous example.
CreateDocumentAsync and we're going to use collection.Resource.SelfLink. And pretty much the same exact document we created last time with the same id which is 123, name, and then company.
Now we're going to create the document query, add the SelfLink, and then add our query, same query as last time. So docExists is going to result in a false simply because we have turned off automatic indexing.
Now we'll read the document just by using the id. And in this case, we already have the SelfLink to the document, so we don't need to do anything more than that. We can say created.SelfLink, and we should get the document back. Now this is quite simple, and you could have guessed this code from what we did last time. However, if you were to manually add another document and make sure that you want to index that document, here's a cool trick that can do this for you.
So while you do client.CreateDocumentAsync, you're going to use the SelfLink of the collection. The next thing you could do is copy the same exact document. I want to give it a different id so we know, let's say the id is 100. So we've got the SelfLink. We also have the document. And if you remember, another thing we can do at this point is add the requestOptions.
And inside requestOptions, you can actually add the IndexingDirective and say IndexingDirective.Include. And that's all it is. That's what does the magic. Now we're going to run a very similar query, and we're going to use the same exact code we had earlier and look for this particular value. And in this case, we will get a response back that actually works.
Just a typo here, remove the extra equals sign. And then finally, we can clean this up by deleting this DocumentCollection. There are a couple of things you should keep in mind. One is that these id's need to be very distinct. So what we could do is use a completely different id. But at the same time, if we're doing the query on company, it needs to be very distinct too because otherwise you might get a response just because there's another record with the same company there.
We can just go change some of these names and then do the searches on those. Then if I were to go back up here, I don't think we need this code anymore. So we're just going to comment this. And also make sure that we have code like this. We use IfNotExists just because we might have that collection already created in Cosmos DB Azure instance, and it might create an error.
So I think we are good to go, and what we need to do is add a break point here so we can see what's going on. Hit F5 and then do F10. We can see the collectionUrl, see the collectionSpecifications. And then at this point of time, we're going to automatically index it to false. And then we create the collection with these specifications. And the specification only has one specification which is automatic indexing is false.
As you can see, the collection got created. You can hover over this and look into what got created. Next thing we want to do is we want to look for anything in the database that has company with the name Apple that says false. And the reason is simple because we were able to remove the indexing on it so this document did not get indexed. Next what we're doing is we are going to create the document.
And then we'll also make sure that we create a manually indexed document in this case. So as you can see, we add a request option that has IndexingDirective.Include. Now we will search the document. And we're going to look for a similar property. In this case, it's company and the value is Microsoft. And you see that docExists is true because this time we were able to index that particular document.
And finally, we're going to delete this collection.
- Data import scenarios
- Creating a database
- Creating a partitioned collection
- Data manipulation
- Importing documents with a stored procedure
- User-defined functions
- Excluding indexing at a document level
- Range indexing on strings
- Querying with SQL parameters
- Range operations