@Viktor
Thank you for the question and for using Microsoft Q&A platform.
You're correct that sorting on multiple fields in Azure Cosmos DB requires a composite index.
To achieve sorting by Price and then by vector distance, you can try the below steps:
- Define a composite index on the Price field. This will allow you to sort by Price efficiently.
2)Instead of calculating the vector distance on the fly, you can precompute the vector distance and store it in a separate field. This way, you can include this precomputed field in your composite index.
- Use the below query:
SELECT TOP 5 c.Name, c.Price
FROM c
WHERE c.Price >= 35.02
ORDER BY c.Price DESC, c.PrecomputedVectorDistance
By following these steps, you can effectively sort by Price
and then by the precomputed vector distance.
You can refer to the below links for more help:
https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/vectordistance
https://devblogs.microsoft.com/cosmosdb/new-ways-to-use-composite-indexes/
https://dev.to/willvelida/understanding-indexing-in-azure-cosmos-db-21kc
Hope this helps. Please let us know if you have any further questions.