azure - Range Index pre-existing collection programmatically -
i've created database collection. collection has thousands of pre-existing documents looks below example.
{ "town": "hull", "easting": 364208, "northing": 176288, "longitude": -2.5168477762, "latitude": 51.4844052488, }
i'm aware need index database range type can use range query & orderby function data.
so, how can range index pre-existing data programmatically using .net sdk?
i've come below code. however, seems fail @ querying collection. when i've inserted breakpoint 'database' contains null @ point of querying collection.
// create instance of documentclient. using (dbclient = new documentclient(new uri(properties.settings.default.endpointurl), properties.settings.default.authorizationkey)) { database database = dbclient.createdatabasequery().where (db => db.id == properties.settings.default.databaseid).asenumerable().firstordefault(); documentcollection collection = dbclient.createdocumentcollectionquery(database.selflink).where (c => c.id == properties.settings.default.collectionid).toarray().firstordefault(); // if database type not null continue range index collection if (collection != null) { stopscollection.indexingpolicy.includedpaths.add( new includedpath { path = "/*", indexes = new system.collections.objectmodel.collection<index> { new rangeindex(datatype.string) {precision = 6}, new rangeindex(datatype.number) {precision = 6} } } ); } else { console.writeline(">> unable retrieve requested collection."); } }
today, indexing policies immutable; need re-create collection change index policy (e.g. add range index).
if wanted create collection custom index policy programatically, code this:
var rangedefault = new documentcollection { id = "rangecollection" }; rangedefault.indexingpolicy.includedpaths.add( new includedpath { path = "/*", indexes = new collection<index> { new rangeindex(datatype.string) { precision = -1 }, new rangeindex(datatype.number) { precision = -1 } } }); await client.createdocumentcollectionasync(database.selflink, rangedefault);
and write code reads data existing collection , writes data on new collection.
but bit cumbersome...
as alternative solution... highly suggest using documentdb data migration tool create new collection new index policy , move data old collection new collection. can delete old collection once migration completes successfully.
you can download data migration tool here.
step 1: define documentdb source:
step 2: define documentdb target, , use new indexing policy:
hint: can right click in indexing policy input box choose indexing policy
which give indexing policy looks this:
{ "indexingmode": "consistent", "automatic": true, "includedpaths": [ { "path": "/*", "indexes": [ { "kind": "range", "datatype": "number", "precision": -1 }, { "kind": "range", "datatype": "string", "precision": -1 } ] }, { "path": "/_ts/?", "indexes": [ { "kind": "range", "datatype": "number", "precision": -1 } ] } ], "excludedpaths": [] }
step 3: run import job...
reminder: delete old collection after import finishes successfully.
Comments
Post a Comment