Question
How to pass ORDER BY value to @com.azure.spring.data.cosmos.repository.Query?
@Repository
public interface UserRepository extends CosmosRepository<User, String> {
@Query("SELECT * FROM c ORDER BY c.name DESC")
List<User> query();
works fine, but
@Query("SELECT * FROM c ORDER BY @orderBy DESC")
List<User> query(@Param("order") String orderBy); // orderBy = "c.name";
fails with
com.azure.spring.data.cosmos.exception.CosmosAccessException: Failed to find items;
nested exception is CosmosException{userAgent=azsdk-java-cosmos/4.7.1 Windows10/10.0 JRE/11.0.8, error=
{"code":"BadRequest","message":"Gateway Failed to Retrieve Query Plan:
Message: {\"errors\":[{\"severity\":\"Error\",\"message\":\"Unsupported ORDER BY clause.
ORDER BY item expression could not be mapped to a document path.\"}]}
Setup:
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @kushagraThapar, @anfeldma-ms
@mbhaskar - please take a look at this.
@Repository
public interface UserRepository extends CosmosRepository<User, String> {
@Query("SELECT * FROM c ORDER BY c.name DESC")
List<User> query();
This is the correct way of doing orderby. If you want to sort on a different property, then that should be part of the query text. Parameters should be used only for values of the document paths
Is there any way to avoid a bunch of similar hardcoded queries with different ordering?
@mbhaskar, @kushagraThapar
Guys, any suggestions?
Currently specifying order by path / sort order as query parameters is not supported.
Most helpful comment
Is there any way to avoid a bunch of similar hardcoded queries with different ordering?