By default, DocumentDB automatically indexes each belongings in a report as quickly as the report is brought to the database. However, you may take manage and high-quality track your personal indexing policy that reduces garage and processing overhead when there are unique files and/or residences that never needs to be indexed.
The default indexing policy that tells DocumentDB to index every belongings mechanically is appropriate for plenty common eventualities. But you could also put into effect a custom coverage that sporting events pleasant control over precisely what gets listed and what doesn't and other capability almost about indexing.
DocumentDB supports the following kinds of indexing −
- Hash
- Range
Hash
Hash index permits green querying for equality, i.E., whilst looking for files in which a given belongings equals an specific value, rather than matching on a range of values like much less than, more than or between.
You can perform range queries with a hash index, however DocumentDB will now not be able to use the hash index to discover matching documents and could instead need to sequentially experiment each document to decide if it should be decided on by means of the variety question.
You may not be able to sort your documents with an ORDER BY clause on a property that has only a hash index.
Range
Range index described for the belongings, DocumentDB allows to correctly question for files in opposition to more than a few values. It additionally allows you to type the query effects on that assets, using ORDER BY.
DocumentDB lets in you to outline both a hash and a selection index on any or all properties, which permits green equality and variety queries, as well as ORDER BY.
Indexing Policy
Every collection has an indexing coverage that dictates which sorts of indexes are used for numbers and strings in each property of every record.
- You also can manage whether or not or not files get indexed robotically as they're introduced to the gathering.
- Automatic indexing is enabled with the aid of default, but you can override that behavior whilst including a file, telling DocumentDB no longer to index that precise file.
- You can disable automatic indexing so that through default, files are not indexed whilst introduced to the gathering. Similarly, you may override this at the report degree and educate DocumentDB to index a particular file when including it to the gathering. This is known as guide indexing.
Include / Exclude Indexing
An indexing policy also can define which path or paths need to be included or excluded from the index. This is useful if you recognise that there are sure elements of a document which you by no means query in opposition to and certain components which you do.
In these instances, you could lessen indexing overhead by telling DocumentDB to index just the ones precise quantities of each file introduced to the collection.
Automatic Indexing
Let’s check a simple example of computerized indexing.
Step 1 − First we create a set called autoindexing and without explicitly imparting a coverage, this series uses the default indexing policy, which means that that automated indexing is enabled on this collection.
Here we are the use of ID-primarily based routing for the database self-link so we don't want to realize its aid ID or question for it before developing the gathering. We can simply use the database ID, that's mydb.
Step 2 − Now allow’s create documents, each with the remaining name of Upston.
private async static Task AutomaticIndexing(DocumentClient client) {
Console.WriteLine();
Console.WriteLine("**** Override Automatic Indexing ****");
// Create collection with automatic indexing
var collectionDefinition = new DocumentCollection {
Id = "autoindexing"
};
var collection = await client.CreateDocumentCollectionAsync("dbs/mydb",
collectionDefinition);
// Add a document (indexed)
dynamic indexedDocumentDefinition = new {
id = "MARK",
firstName = "Mark",
lastName = "Upston",
addressLine = "123 Main Street",
city = "Brooklyn",
state = "New York",
zip = "11229",
};
Document indexedDocument = await client
.CreateDocumentAsync("dbs/mydb/colls/autoindexing", indexedDocumentDefinition);
// Add another document (request no indexing)
dynamic unindexedDocumentDefinition = new {
id = "JANE",
firstName = "Jane",
lastName = "Upston",
addressLine = "123 Main Street",
city = "Brooklyn",
state = "New York",
zip = "11229",
};
Document unindexedDocument = await client
.CreateDocumentAsync("dbs/mydb/colls/autoindexing", unindexedDocumentDefinition,
new RequestOptions { IndexingDirective = IndexingDirective.Exclude });
//Unindexed document won't get returned when querying on non-ID (or selflink) property
var doeDocs = client.CreateDocumentQuery("dbs/mydb/colls/autoindexing", "SELECT *
FROM c WHERE c.lastName = 'Doe'").ToList();
Console.WriteLine("Documents WHERE lastName = 'Doe': {0}", doeDocs.Count);
// Unindexed document will get returned when using no WHERE clause
var allDocs = client.CreateDocumentQuery("dbs/mydb/colls/autoindexing",
"SELECT * FROM c").ToList();
Console.WriteLine("All documents: {0}", allDocs.Count);
// Unindexed document will get returned when querying by ID (or self-link) property
Document janeDoc = client.CreateDocumentQuery("dbs/mydb/colls/autoindexing",
"SELECT * FROM c WHERE c.id = 'JANE'").AsEnumerable().FirstOrDefault();
Console.WriteLine("Unindexed document self-link: {0}", janeDoc.SelfLink);
// Delete the collection
await client.DeleteDocumentCollectionAsync("dbs/mydb/colls/autoindexing");
}
look ahead to client.DeleteDocumentCollectionAsync("dbs/mydb/colls/autoindexing");
This first one, for Mark Upston, gets added to the gathering and is then straight away indexed mechanically based at the default indexing coverage.
But while the second report for Mark Upston is added, we've got passed the request alternatives with IndexingDirective.Exclude which explicitly instructs DocumentDB no longer to index this file, no matter the collection's indexing coverage.
We have specific varieties of queries for both the documents at the give up.
Step 3 − Let’s call the AutomaticIndexing challenge from CreateDocumentClient.
private static async Task CreateDocumentClient() {
// Create a new instance of the DocumentClient
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
await AutomaticIndexing(client);
}
}
When the above code is compiled and accomplished, you'll get hold of the following output.
**** Override Automatic Indexing ****
Documents WHERE lastName = 'Upston': 1
All documents: 2
Unindexed document self-link: dbs/kV5oAA==/colls/kV5oAOEkfQA=/docs/kV5oAOEkfQACA
AAAAAAAAA==/
As you can see we have two such files, however the question returns most effective the only for Mark because the only for Mark isn't always listed. If we query once more, without a WHERE clause to retrieve all the files within the collection, then we get a end result set with each files and that is because unindexed documents are continually back by way of queries that have no WHERE clause.
We also can retrieve unindexed files by using their ID or self-hyperlink. So while we question for Mark's document via his ID, MARK, we see that DocumentDB returns the report even though it is not indexed within the series.
Manual Indexing
Let’ take a look at a simple instance of guide indexing by using overriding automatic indexing.
Step 1 − First we'll create a set called manualindexing and override the default policy by means of explicitly disabling automatic indexing. This means that, until we request otherwise, new documents added to this series will now not be indexed.
private async static Task ManualIndexing(DocumentClient client) {
Console.WriteLine();
Console.WriteLine("**** Manual Indexing ****");
// Create collection with manual indexing
var collectionDefinition = new DocumentCollection {
Id = "manualindexing",
IndexingPolicy = new IndexingPolicy {
Automatic = false,
},
};
var collection = await client.CreateDocumentCollectionAsync("dbs/mydb",
collectionDefinition);
// Add a document (unindexed)
dynamic unindexedDocumentDefinition = new {
id = "MARK",
firstName = "Mark",
lastName = "Doe",
addressLine = "123 Main Street",
city = "Brooklyn",
state = "New York",
zip = "11229",
};
Document unindexedDocument = await client
.CreateDocumentAsync("dbs/mydb/colls/manualindexing", unindexedDocumentDefinition);
// Add another document (request indexing)
dynamic indexedDocumentDefinition = new {
id = "JANE",
firstName = "Jane",
lastName = "Doe",
addressLine = "123 Main Street",
city = "Brooklyn",
state = "New York",
zip = "11229",
};
Document indexedDocument = await client.CreateDocumentAsync
("dbs/mydb/colls/manualindexing", indexedDocumentDefinition, new RequestOptions {
IndexingDirective = IndexingDirective.Include });
//Unindexed document won't get returned when querying on non-ID (or selflink) property
var doeDocs = client.CreateDocumentQuery("dbs/mydb/colls/manualindexing",
"SELECT * FROM c WHERE c.lastName = 'Doe'").ToList();
Console.WriteLine("Documents WHERE lastName = 'Doe': {0}", doeDocs.Count);
// Unindexed document will get returned when using no WHERE clause
var allDocs = client.CreateDocumentQuery("dbs/mydb/colls/manualindexing",
"SELECT * FROM c").ToList();
Console.WriteLine("All documents: {0}", allDocs.Count);
// Unindexed document will get returned when querying by ID (or self-link) property
Document markDoc = client
.CreateDocumentQuery("dbs/mydb/colls/manualindexing",
"SELECT * FROM c WHERE c.id = 'MARK'")
.AsEnumerable().FirstOrDefault();
Console.WriteLine("Unindexed document self-link: {0}", markDoc.SelfLink);
await client.DeleteDocumentCollectionAsync("dbs/mydb/colls/manualindexing");
}
Step 2 − Now we will once more create the same two files as earlier than. We will not deliver any special request options for Mark's report this time, due to the gathering's indexing coverage, this record will not get listed.
Step 3− Now while we upload the second one file for Mark, we use RequestOptions with IndexingDirective.Include to inform DocumentDB that it need to index this report, which overrides the gathering's indexing coverage that says that it shouldn't.
We have one of a kind styles of queries for both the files at the give up.
Step 4 − Let’s call the ManualIndexing project from CreateDocumentClient.
private static async Task CreateDocumentClient() {
// Create a new instance of the DocumentClient
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
await ManualIndexing(client);
}
}
When the above code is compiled and achieved you will get hold of the following output.
**** Manual Indexing ****
Documents WHERE lastName = 'Upston': 1
All documents: 2
Unindexed document self-link: dbs/kV5oAA==/colls/kV5oANHJPgE=/docs/kV5oANHJPgEBA
AAAAAAAAA==/
Again, the question returns handiest one of the documents, however this time, it returns Jane Doe, which we explicitly asked to be indexed. But again as earlier than, querying with out a WHERE clause retrieves all the documents inside the series, inclusive of the unindexed document for Mark. We also can question for the unindexed report by its ID, which DocumentDB returns although it's no longer listed.