/// <summary> /// Create a collection with the required indexing policies for Order By against specific properties. /// </summary> /// <param name="database">The database to create the collection in.</param> /// <returns>The created collection.</returns> private async Task <DocumentCollection> CreateCollectionForOrderBySinglePath(Database database) { IndexingPolicy orderByPolicy = new IndexingPolicy(); // Index the createdAt property for Order By orderByPolicy.IncludedPaths.Add(new IndexingPath { Path = "/\"createdAt\"/?", IndexType = IndexType.Range, NumericPrecision = -1 }); // Index all numeric paths under "user" for Order By orderByPolicy.IncludedPaths.Add(new IndexingPath { Path = "/\"user\"/*", IndexType = IndexType.Range, NumericPrecision = -1 }); // Use the default (Hash) for everything else. orderByPolicy.IncludedPaths.Add(new IndexingPath { Path = "/" }); // Here we create as a S1. DocumentCollection collection = await DocumentClientHelper.CreateNewCollection( this.client, database, "tweetsCollectionSinglePath", new DocumentCollectionInfo { IndexingPolicy = orderByPolicy, OfferType = "S1" }); return(collection); }
/// <summary> /// Import data into a DocumentDB collection using a "Bulk Import" stored procedure. /// </summary> /// <param name="collection">The collection to run queries against.</param> /// <param name="sourceDirectory">The source directory to read files from.</param> /// <returns></returns> private async Task ImportData(DocumentCollection collection, string sourceDirectory) { Console.WriteLine("Importing data ..."); await DocumentClientHelper.RunBulkImport(this.client, collection, sourceDirectory); Console.WriteLine(); }
/// <summary> /// Create a collection with the required indexing policies for Order By against any numeric or string property. /// Note that the default policy allows Order By only against numbers. /// </summary> /// <param name="database">The database to create the collection in.</param> /// <returns>The created collection.</returns> private async Task <DocumentCollection> CreateCollectionForOrderBy(Database database) { IndexingPolicy orderByPolicy = new IndexingPolicy(); orderByPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*", Indexes = new System.Collections.ObjectModel.Collection <Index>() { new RangeIndex(DataType.String) { Precision = -1 }, new RangeIndex(DataType.Number) { Precision = -1 } } }); // Here we create as a S1. DocumentCollection collection = await DocumentClientHelper.CreateNewCollection( this.client, database, "tweetsCollection", new DocumentCollectionSpec { IndexingPolicy = orderByPolicy, OfferType = "S1" }); return(collection); }
/// <summary> /// Create a collection with the required indexing policies for Order By against specific properties. /// </summary> /// <param name="database">The database to create the collection in.</param> /// <returns>The created collection.</returns> private async Task <DocumentCollection> CreateCollectionForOrderBySinglePath(Database database) { IndexingPolicy orderByPolicy = new IndexingPolicy(); // Index the createdAt property for Order By orderByPolicy.IncludedPaths.Add(new IncludedPath { Path = "/createdAt/?", Indexes = new System.Collections.ObjectModel.Collection <Index>() { new RangeIndex(DataType.Number) { Precision = -1 } } }); // Index all string and numeric paths under "user" for Order By orderByPolicy.IncludedPaths.Add(new IncludedPath { Path = "/user/*", Indexes = new System.Collections.ObjectModel.Collection <Index>() { new RangeIndex(DataType.Number) { Precision = -1 }, new RangeIndex(DataType.String) { Precision = -1 }, } }); // Use the default (Hash) for everything else. orderByPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*" }); // Here we create as a S1. DocumentCollection collection = await DocumentClientHelper.CreateNewCollection( this.client, database, "tweetsCollectionSinglePath", new DocumentCollectionSpec { IndexingPolicy = orderByPolicy, OfferType = "S1" }); return(collection); }
/// <summary> /// Create a collection with the required indexing policies for Order By against any numeric property. /// </summary> /// <param name="database">The database to create the collection in.</param> /// <returns>The created collection.</returns> private async Task <DocumentCollection> CreateCollectionForOrderBy(Database database) { IndexingPolicy orderByPolicy = new IndexingPolicy(); orderByPolicy.IncludedPaths.Add(new IndexingPath { Path = "/", IndexType = IndexType.Range, NumericPrecision = -1 }); // Here we create as a S1. DocumentCollection collection = await DocumentClientHelper.CreateNewCollection( this.client, database, "tweetsCollection", new DocumentCollectionInfo { IndexingPolicy = orderByPolicy, OfferType = "S1" }); return(collection); }
/// <summary> /// Run samples for Order By queries. /// </summary> /// <returns>a Task object.</returns> private async Task RunAsync() { Database database = await DocumentClientHelper.GetNewDatabaseAsync(this.client, DatabaseId); // Create collection with the required indexing policies for Order By DocumentCollection collection = await this.CreateCollectionForOrderBy(database); // By default, we use a fake dataset that looks like Twitter's status updates to illustrate how // you can query using Order By. If you include your own Twitter Developer credentials in // App.config, you can download real tweets, then query against them. if (bool.Parse(ConfigurationManager.AppSettings["ShouldDownload"])) { DownloadDataFromTwitter(); await this.ImportData(collection, DataDirectory); } else { await this.ImportData(collection, FakeDataDirectory); } this.RunOrderByQuery(collection); this.RunOrderByQueryNestedProperty(collection); this.RunOrderByQueryWithFilters(collection); await this.RunOrderByQueryAsyncWithPaging(collection); // Create a collection with indexing policy for just order by against specific paths. // With this configuration, // RunOrderByQuery will throw an exception since RetweetCount is not indexed for Order By. // RunOrderByQueryNestedProperty will run successfully. // RunOrderByQueryWithFilters will run successfully. // RunOrderByQueryAsyncWithPaging will run successfully. // You'll also notice that the overall storage for "customIndexingCollection" is a little lower than for "collection". DocumentCollection customIndexingCollection = await this.CreateCollectionForOrderBySinglePath(database); }