范围索引以编程方式预先存在的集合
Range Index pre-existing collection programmatically
我创建了一个包含集合的数据库。该集合有数千个预先存在的文档,如下所示。
{
"Town": "Hull",
"Easting": 364208,
"Northing": 176288,
"Longitude": -2.5168477762,
"Latitude": 51.4844052488,
}
我知道我需要使用范围类型为数据库编制索引,以便我可以对我的数据使用范围查询和 OrderBy 函数。
那么,如何使用 .NET SDK 以编程方式对预先存在的数据进行范围索引?
我想出了下面的代码。但是,它似乎无法查询集合。当我插入断点时,'database' 在查询集合时包含 null。
// Create an instance of the 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 is not null then continue to range index the 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 to retrieve requested collection.");
}
}
今天,索引策略是一成不变的;所以你需要 re-create a collection 来更改索引策略(例如添加范围索引)。
如果您想以编程方式创建一个带有自定义索引策略的 collection,执行此操作的代码如下所示:
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);
然后编写一些代码以从现有的 collection 读取数据并将数据写入新的 collection。
不过这样有点麻烦...
作为替代解决方案...我强烈建议使用 DocumentDB Data Migration Tool 使用新的索引策略创建新的 collection 并将数据从旧的 collection 移动到新 collection。迁移成功完成后,您可以删除旧的 collection。
您可以下载数据迁移工具here。
步骤 1:将 DocumentDB 定义为源:
第 2 步:将 DocumentDB 定义为目标,并使用新的索引策略:
提示:可以在索引策略输入框右击选择索引策略
这将为您提供如下所示的索引策略:
{
"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": []
}
第 3 步:运行 导入作业...
温馨提示:导入成功后删除旧的collection。
我创建了一个包含集合的数据库。该集合有数千个预先存在的文档,如下所示。
{
"Town": "Hull",
"Easting": 364208,
"Northing": 176288,
"Longitude": -2.5168477762,
"Latitude": 51.4844052488,
}
我知道我需要使用范围类型为数据库编制索引,以便我可以对我的数据使用范围查询和 OrderBy 函数。
那么,如何使用 .NET SDK 以编程方式对预先存在的数据进行范围索引?
我想出了下面的代码。但是,它似乎无法查询集合。当我插入断点时,'database' 在查询集合时包含 null。
// Create an instance of the 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 is not null then continue to range index the 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 to retrieve requested collection.");
}
}
今天,索引策略是一成不变的;所以你需要 re-create a collection 来更改索引策略(例如添加范围索引)。
如果您想以编程方式创建一个带有自定义索引策略的 collection,执行此操作的代码如下所示:
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);
然后编写一些代码以从现有的 collection 读取数据并将数据写入新的 collection。
不过这样有点麻烦...
作为替代解决方案...我强烈建议使用 DocumentDB Data Migration Tool 使用新的索引策略创建新的 collection 并将数据从旧的 collection 移动到新 collection。迁移成功完成后,您可以删除旧的 collection。
您可以下载数据迁移工具here。
步骤 1:将 DocumentDB 定义为源:
第 2 步:将 DocumentDB 定义为目标,并使用新的索引策略:
提示:可以在索引策略输入框右击选择索引策略
这将为您提供如下所示的索引策略:
{
"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": []
}
第 3 步:运行 导入作业...
温馨提示:导入成功后删除旧的collection。