DocumentDb:没有索引的查询
DocumentDb: Querying without an index
在排除所有路径建立索引的情况下,为什么我仍然能够对ID以外的字段执行成功的查询?
排除所有路径:
collection.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath()
{
Path = "/*"
});
查询:
SELECT * FROM c where c.Key = "117dfd49-a71d-413b-a9b1-841e88db06e8"
When indexing is turned off, documents can be accessed only through their self-links or by queries using ID.
编辑:
在查看索引策略时,我发现我的路径没有被排除在外:
{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
{
"path": "/*",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Hash",
"dataType": "String",
"precision": 3
}
]
},
{
"path": "/\"_ts\"/?",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Hash",
"dataType": "String",
"precision": 3
}
]
}
],
"excludedPaths": []
}
我做错了什么?
我使用的是一致性索引,所以如果我理解正确的话,这肯定不是索引传播的最终一致性问题?
这是全部代码:
var collectionSpec = new DocumentCollection { Id = collectionId };
var requestOptions = new RequestOptions { OfferType = "S1" };
collection = Client.CreateDocumentCollectionAsync(databaseLink, collectionSpec, requestOptions).Result;
collection.IndexingPolicy.IndexingMode = IndexingMode.Consistent;
collection.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath()
{
Path = "/*"
});
设置新索引策略后,我没有看到任何替换文档集合的调用。
您需要调用 Client.ReplaceDocumentCollectionAsync()
以使您的索引策略更改生效。
方法文档是here。
这里是DocumentDB团队写的an article关于在线更新索引策略(指的是需要调用ReplaceDocumentCollectionAsync()
)。
您需要更改代码片段以定义集合规范的索引策略 prior 到 client.CreateDocumentCollectionAsync()
- 以便它包含在网络请求中为 DocumentDB 创建一个集合。
在创建集合时设置自定义索引策略
var collection = new DocumentCollection { Id = "myCollection" };
collection.IndexingPolicy.IndexingMode = IndexingMode.Consistent;
collection.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, collection);
更新现有集合的索引政策
As - 您还可以使用 Client.ReplaceDocumentCollectionAsync()
.
更新现有集合的索引策略
在排除所有路径建立索引的情况下,为什么我仍然能够对ID以外的字段执行成功的查询?
排除所有路径:
collection.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath()
{
Path = "/*"
});
查询:
SELECT * FROM c where c.Key = "117dfd49-a71d-413b-a9b1-841e88db06e8"
When indexing is turned off, documents can be accessed only through their self-links or by queries using ID.
编辑:
在查看索引策略时,我发现我的路径没有被排除在外:
{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
{
"path": "/*",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Hash",
"dataType": "String",
"precision": 3
}
]
},
{
"path": "/\"_ts\"/?",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Hash",
"dataType": "String",
"precision": 3
}
]
}
],
"excludedPaths": []
}
我做错了什么?
我使用的是一致性索引,所以如果我理解正确的话,这肯定不是索引传播的最终一致性问题?
这是全部代码:
var collectionSpec = new DocumentCollection { Id = collectionId };
var requestOptions = new RequestOptions { OfferType = "S1" };
collection = Client.CreateDocumentCollectionAsync(databaseLink, collectionSpec, requestOptions).Result;
collection.IndexingPolicy.IndexingMode = IndexingMode.Consistent;
collection.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath()
{
Path = "/*"
});
设置新索引策略后,我没有看到任何替换文档集合的调用。
您需要调用 Client.ReplaceDocumentCollectionAsync()
以使您的索引策略更改生效。
方法文档是here。
这里是DocumentDB团队写的an article关于在线更新索引策略(指的是需要调用ReplaceDocumentCollectionAsync()
)。
您需要更改代码片段以定义集合规范的索引策略 prior 到 client.CreateDocumentCollectionAsync()
- 以便它包含在网络请求中为 DocumentDB 创建一个集合。
在创建集合时设置自定义索引策略
var collection = new DocumentCollection { Id = "myCollection" };
collection.IndexingPolicy.IndexingMode = IndexingMode.Consistent;
collection.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, collection);
更新现有集合的索引政策
As Client.ReplaceDocumentCollectionAsync()
.