commercetools / commercetools-sdk-java-v2

The e-commerce SDK from commercetools for Java.
https://commercetools.github.io/commercetools-sdk-java-v2/javadoc/index.html
Apache License 2.0
35 stars 15 forks source link

`QueryAll` with sort order other than by id returns duplicate and incomplete search results. #341

Open tomwscott opened 2 years ago

tomwscott commented 2 years ago

I'm not sure if this is an issue or a feature request but if the query passed to QueryUtils applies a sort order other than by id the search results after the first page will be incomplete and likely include duplicate results.

Whilst it is hinted at in the code documentation, there is nothing that explicitly prevents a sort order being added as part of the query which then breaks the assumption here: https://github.com/commercetools/commercetools-sdk-java-v2/blob/615f3c4e0f3f2166a4699c61751ab46bee30db78/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/client/QueryAll.java#L160

For my use case it would be great to be able fetch all results ordered by key.

If the only supported mechanism in the library for querying all entities is when ordered by id it would be great to have that as part of the function name to make it explicit.

jenschude commented 2 years ago

The QueryAll is a helper class to conveniently get all elements of a query without the limitations from using offset & limit query parameters. As a matter of fact it has limitations itself which means that the result must be sorted by ID too else the algorithm it not working correctly as described here: https://docs.commercetools.com/api/general-concepts#iterating-over-all-elements

If you already sort by key you may have to add an additional sort by ID in your request cause the sort by ID would not be added as can be seen here:

https://github.com/commercetools/commercetools-sdk-java-v2/blob/74e2a9ac3fa0cc250df3f6d95dbfdd0312599e10/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/client/QueryAll.java#L41

So to have a different option for paging the result is a feature request and I don't know if or when we could work on it.

Besides you are still free to use the implementation as a starting point to adjust it to your needs.