Sometimes you might want to ensure that your in stock products are always above products that are out of stock in your search results.
The easiest way to rank "in stock" 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 we have our guide on Using Optional Filters. This approach requires all records have a boolean attribute like in_stock:true.
Another approach to achieve this would be to essentially divide your search results in two sections, the first section would be all items in stock, and then the second section would be all of your out of stock items and 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. in_stock 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 an attribute for in_stock then it will rank after those that are out of stock. 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 which is what I understand you to be aiming for.
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.
Of course this same approach will 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). Of course you will need to take care not to use this too much, as it will split your results intro two groups each time you sort them.