Sometimes you might want to ensure that your in stock products are always displayed above products that are out of stock in your search results.
The easiest way to rank "in stock" items above products that are not in stock is to use a condition-less rule. You can create a condition-less rule that adds default search parameters with an optional filter that boosts products with in_stock: true. For more on this you can refer to our guide on Using Optional Filters. This approach requires all records have a boolean attribute like in_stock.
Another approach would be to divide your search results in to two sections, the first section would be all items in stock, and the second section would be all of the out of stock items. We can then rank results within these sections as usual.
How to rank in-stock items first
Firstly, you will need an attribute to store if the product is in stock, for the purposes for this explanation lets call this attribute in_stock. This attribute should be true when a product is in stock and false when it is out of stock.
Once this is done we will make in_stock your top ranking criteria and set is as descending:
You should then see all the in_stock records at the top of your search results.
There are two important caveats with this approach:
Caveat 1 - We need to make sure that all products have this attribute
If a product does not have the in_stock attribute then it will rank after those that have in_stock set to false. Of course this is not desirable. So you will need to make sure that this attribute is always set on all records.
Caveat 2 We need to be very careful when ranking an attribute before the other ranking criteria
Ranking an attribute first means that it is the most important ranking criteria and will be considered before anything else.
With a Boolean attribute such as in_stock, this will segment the results into two halves. However, with attributes that have more values than that, placing such a high priority in an attribute will override Algolia's ranking. Therefore it is important that only boolean values are used in this way across all searches, and even then there will be situations where better matching items that are out of stock will rank lower than poor matches that are in stock which may be counter intuitive for some users.
Here is a background article on sorting that we highly recommend reading if you have not already https://www.algolia.com/doc/guides/managing-results/refine-results/sorting.
If you are looking for a milder form of this, you can consider making in_stock your Custom ranking attribute. This will not achieve what you are looking for with regards to always seeing out of stock items at the bottom, but it also interferes less with the ranking of your results.
Final note
This approach will also work for any other boolean attributes you want to use to split your results (such as if a product is available for delivery for example). You should take care not to use this too much, as it will split your results into two groups each time you sort them.