Azure Cosmos DB’s geospatial capabilities just got even better! We’re excited to announce that you can now sort query results by distance using an ORDER BY ST_DISTANCE(…) clause in the NoSQL query language while leveraging a spatial index.
Why is this important?
Previously, you could use ST_DISTANCE in projections or WHERE clauses to filter results within a radius, for example, finding all restaurants within 5000 meters. But if you wanted to sort those results by the distance, you had to pull the data client-side and handle sorting manually. Now you can now do it directly in your query! Azure Cosmos DB will return results already ordered by how far they are from a given point (or any GeoJSON object). No workarounds or extra code needed. Just a single query that’s fast and simple.
Imagine you’re building an app to help users discover nearby restaurants. You’ve always been able to filter results distance, but now, you can show them the closest ones first with a single query.
Define a Spatial Indexing Policy
Before you can sort by distance, ensure your container has a spatial index configured on the JSON path that contains your location data. The index type should match the geometry you’re working within this example we use “Point”, but you can also use Polygon or MultiPolygon, or other types depending on your data model. The spatial index definition would look like this:
"spatialIndexes": [
{
"path": "/Location/?",
"types": [
"Point",
"LineString",
"Polygon",
"MultiPolygon"
]
}
]
This tells Azure Cosmos DB to index the /location property as a Point type so that distance functions and ordering work efficiently. Learn more about spatial indexes.
Write and execute the query
Once your container has a spatial index, you can directly add the ORDER BY ST_DISTANCE clause to the end of the query.
Query
SELECT TOP 5 c.id, c.name, ST_DISTANCE(c.location, { "type": "Point", "coordinates": [ -74.0052, 40.7401 ] }) AS distance
FROM c
ORDER BY ST_DISTANCE(c.location, { "type": "Point", "coordinates": [ -74.0052, 40.7401 ] }) ASC
Results
[
{
"Name": "Pier 63 Pizzeria",
"Description": "Wood-fired pizzas with creative toppings and classic Neapolitan crusts.",
"Address": "63 Jane St, New York, NY 10014",
"FoodType": "Italian",
"Rating": 4.5,
"DistanceInMeters": 142.15
},
{
"Name": "Velvet Taco & Tamale",
"Description": "Modern Mexican eats blending classic tamales and creative taco fillings.",
"Address": "234 W 14th St, New York, NY 10011",
"FoodType": "Mexican",
"Rating": 4.1,
"DistanceInMeters": 237.07
},
{
"Name": "Lighthouse Lobster",
"Description": "Classic New England-style lobster rolls, chowders, and fried seafood.",
"Address": "112 Jane St, New York, NY 10014",
"FoodType": "Seafood",
"Rating": 4,
"DistanceInMeters": 289.42
},
{
"Name": "Blue Ribbon Deli",
"Description": "Classic New York deli sandwiches, matzo ball soup and pickles.",
"Address": "102 W 14th St, New York, NY 10011",
"FoodType": "Deli",
"Rating": 4.2,
"DistanceInMeters": 444.43
},
{
"Name": "Bistro on 7th",
"Description": "Neighborhood French-American bistro with a rotating chalkboard menu.",
"Address": "223 7th Ave, New York, NY 10011",
"FoodType": "Bistro",
"Rating": 4,
"DistanceInMeters": 456.39
}
]
Summary
Azure Cosmos DB now supports sorting query results by distance using ORDER BY ST_DISTANCE(…), enabling developers to easily build location-aware applications without extra client-side processing. This long-awaited enhancement streamlines geospatial queries by allowing results to be both filtered and ordered by proximity directly within the database. Azure Cosmos DB continues to make building intelligent, globally distributed, and geospatially aware applications simpler than ever.
Get started with geospatial queries
- ST_DISTANCE system function documentation
- Geospatial / GeoJSON support in Azure Cosmos DB
- Defining and using spatial indexes
About Azure Cosmos DB
Azure Cosmos DB is a fully managed and serverless NoSQL and vector database for modern app development, including AI applications. With its SLA-backed speed and availability as well as instant dynamic scalability, it is ideal for real-time NoSQL and MongoDB applications that require high performance and distributed computing over massive volumes of NoSQL and vector data.
Try Azure Cosmos DB for free here. To stay in the loop on Azure Cosmos DB updates, follow us on X, YouTube, and LinkedIn.
0 comments
Be the first to start the discussion.