【发布时间】:2019-05-01 22:24:26
【问题描述】:
空间索引似乎不适用于包含具有 GeoJson 坐标的文档的集合。我已经尝试使用默认索引策略,它固有地为所有字段提供空间索引。
我尝试从头开始创建新的 Cosmos Db 帐户、数据库和集合,但没有成功让空间索引与 ST_DISTANCE 查询一起使用。
我已经使用以下索引策略设置了一个简单的集合:
{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
{
"path": "/\"location\"/?",
"indexes": [
{
"kind": "Spatial",
"dataType": "Point"
},
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Range",
"dataType": "String",
"precision": -1
}
]
}
],
"excludedPaths": [
{
"path": "/*",
},
{
"path": "/\"_etag\"/?"
}
]
}
我插入到集合中的文档:
{
"id": "document1",
"type": "Type1",
"location": {
"type": "Point",
"coordinates": [
-50,
50
]
},
"name": "TestObject"
}
应该返回集合中单个文档的查询:
SELECT * FROM f WHERE f.type = "Type1" and ST_DISTANCE(f.location, {'type': 'Point', 'coordinates':[-50,50]}) < 200000
没有返回任何结果。如果我像这样显式查询而不使用空间索引:
SELECT * FROM f WHERE f.type = "Type1" and ST_DISTANCE({'type': 'Point', 'coordinates':[f.location.coordinates[0],f.location.coordinates[1]]}, {'type': 'Point', 'coordinates':[-50,50]}) < 200000
它按原样返回文档,但没有利用我需要的索引,因为我将存储大量坐标。
这似乎与引用 here 的问题相同。如果我在远处添加第二个文档并在第一个查询中将“”,它会起作用!
我应该提到这仅在 Azure 上发生。当我使用 Azure Cosmos Db Emulator 时,它可以完美运行!这里发生了什么?!非常感谢任何提示或建议。
更新:我发现了查询在模拟器而不是 Azure 上工作的原因 - 模拟器上的数据库在其集合之间没有预配(共享)吞吐量,而我做了Azure 中的数据库,预配吞吐量以降低成本(即 4 个集合共享 400 RU/s)。我在 Azure 中创建了一个非预配吞吐量数据库,并且该查询适用于空间索引!我将向 Microsoft 记录此问题,看看是否有原因造成这种情况?
【问题讨论】:
标签: azure-cosmosdb