Azure 以编程方式对预先存在的集合进行范围索引

Azure 以编程方式对预先存在的集合进行范围索引,azure,azure-cosmosdb,Azure,Azure Cosmosdb,我已经用一个集合创建了一个数据库。该集合有数千个预先存在的文档,下面是一个示例 { "Town": "Hull", "Easting": 364208, "Northing": 176288, "Longitude": -2.5168477762, "Latitude": 51.4844052488, } 我知道我需要使用范围类型对数据库进行索引,这样我就可以对数据使用范围查询&OrderBy函数 那么,如何使用.NET SDK以编程方式对预先存在的数据进行范围索引呢 我想出了下面的

我已经用一个集合创建了一个数据库。该集合有数千个预先存在的文档,下面是一个示例

{
 "Town": "Hull",
 "Easting": 364208,
 "Northing": 176288,
 "Longitude": -2.5168477762,
 "Latitude": 51.4844052488,
}
我知道我需要使用范围类型对数据库进行索引,这样我就可以对数据使用范围查询&OrderBy函数

那么,如何使用.NET SDK以编程方式对预先存在的数据进行范围索引呢

我想出了下面的代码。但是,它似乎无法查询集合。当我插入断点时,“数据库”在查询集合时包含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.");
        }
    }
//创建DocumentClient的实例。
使用(dbClient=newdocumentclient(新Uri(Properties.Settings.Default.EndpointUrl)、Properties.Settings.Default.AuthorizationKey))
{
Database=dbClient.CreateDatabaseQuery()。其中
(db=>db.Id==Properties.Settings.Default.databaseID).AsEnumerable().FirstOrDefault();
DocumentCollection collection=dbClient.CreateDocumentCollectionQuery(database.SelfLink)。其中
(c=>c.Id==Properties.Settings.Default.collectionID).ToArray().FirstOrDefault();
//如果数据库类型不为null,则继续对集合进行范围索引
if(集合!=null)
{
stopsCollection.IndexingPolicy.IncludedPath.Add(
新包含路径
{
Path=“/*”,
索引=新System.Collections.ObjectModel.Collection
{
新的RangeIndex(DataType.String){Precision=6},
新的范围索引(DataType.Number){Precision=6}
}
}
);
}
其他的
{
Console.WriteLine(“>>无法检索请求的集合。”);
}
}

如今,索引策略是不变的;因此,您需要重新创建集合以更改索引策略(例如,添加范围索引)

如果希望以编程方式创建具有自定义索引策略的集合,则执行此操作的代码如下所示:

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);   
步骤3:运行导入作业…


提示:导入成功后删除旧集合

我将使用该工具来帮助我编写索引策略,即使我不需要进行迁移。
{
  "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": []
}