Order your results

You can use the "orderings" parameters to sort your query results in a particular order. The "after" parameter allows you to only show documents before or after a certain document in the results.

The orderings parameter

The orderings parameter orders the results by the specified field(s). You can specify as many fields as you want.

Simply use the orderings parameter in your query options as shown in the examples below.

Order by a field - lowest to highest

When ordering your results by a certain field, it will sort the field from the lowest value to the highest by default.

The following examples shows how to sort the results of a product by the "price" field from lowest to highest.


Order by a field - highest to lowest

To sort a field from highest to lowest, add "desc" next to the field name. The following shows an example of this.

orderings=[my.product.price desc]

Order by Multiple Fields

You can specify multiple fields to sort the order of your results by. The results will be ordered first by the first field specified. If any of those values are equal, then those results will be sorted by the next specified field. And so on.

Here is an example that will order the results first by the price of the products from low to high. It will then sort any products that have the same price by their title.


Order by First or Last Publication Dates

It is also possible to order documents by their first or last publication dates.

The first publication date is the date that the document was originally published for the first time.


The last publication date is the most recent date that the document has been published after editing.


The after parameter

The after parameter can be used along with the orderings parameter. It will remove all the documents except for those after the specified document in the list.

To clarify, let’s say you have a query that return the following documents in this order:

  • V9Zt3icAAAl8Uzob (Page 1)
  • PqZtvCcAALuRUzmO (Page 2)
  • VkRmhykAAFA6PoBj (Page 3)
  • V4Fs8rDbAAH9Pfow (Page 4)
  • G8ZtxQhAALuSix6R (Page 5)
  • Ww9yuAvdAhl87wh6 (Page 6)

If you add the after parameter and specify page 3, “VkRmhykAAFA6PoBj”, your query will return the following:

  • V4Fs8rDbAAH9Pfow (Page 4)
  • G8ZtxQhAALuSix6R (Page 5)
  • Ww9yuAvdAhl87wh6 (Page 6)

By reversing the orderings in your query, you can use this same method to retrieve all the documents before the specified document.

Simply use the after parameter in your query options as shown below.


Was this article helpful?
Not really
Yes, Thanks

Can't find what you're looking for? Get in touch with us on our Community Forum.