1
votes

Would a query like

SELECT TOP 1 * ST_DISTANCE etc 

or more specifically

SELECT TOP 1 * FROM c WHERE ST_DISTANCE(c.AssetLocation, { 'type': 'Point', 'coordinates': [115.58, 21.84] }) < 49999

return the closest object, and if not, how could I order by distance?

Thanks,

Ian

1
Closest to a point that I'd supplied to the ST_DISTANCE function - Ian Short
no, it will return distance between some arbitrary point in database and yours supplied point, you could order by distance though - Iłya Bursov
OK thanks for that - Ian Short
"If you include spatial indexing in your indexing policy, then "distance queries" will be served efficiently through the index." It's easiest to just add a policy on all paths /* and let it find spatial data automatically { "kind":"Spatial", "dataType":"Point" } - Larry Maccherone
That said, I've only used it in a situation like ST_DISTANCE(...) < 20. Never tried using ORDER BY and TOP on spatial indexes. - Larry Maccherone

1 Answers

3
votes

DocumentDB does not natively support nearest or ORDER BY using Geo-Spatial index yet.

The best approach for ORDER BY is to project (SELECT) the distance in the query and sort it either client-side or via a stored procedure (reduces the latency cost of multiple round trips).

For a large data set, you can optimize nearest by adding a filter (WHERE) on distance beginning with a small value, and iterating / stepping through increasing distance values until the first points are found.