Algolia rails gem for back-end/InstantSearch for front-end

Hello. I’m using Algolia gem for back-end search. When user make a search, he is redirected to another view to display the results. So in my method for this view, I have @results whom contains an array with the results.
But I want to permit to the user to sort the results. I didn’t find anything to do it in back-end.
So I tought to use InstantSearch, but How can I use the results (they are in my variable @results) as hits and then permit to the user to sort those results ?
Thanks for your answers :slight_smile:

Hi @mouss, welcome to the forum!

In Algolia, in order to have different sort orders (e.g. sort by price (asc), sort by price (desc), sort by ratings, etc.) it is necessary to have replica indices. This is because to make our search fast, we sort your data at indexing time. Therefore, each of your indices can only be sorted in one way.

A replica is a copy of one of your indices, that has the same data, synchronizes data updates, but can have unique settings (such as sort order). The index from which you copy a replica’s data is the replica’s primary index. For each attribute you want to sort by, you will need to create a replica index, which will contain a copy of all the data in your primary index. Making a replica will add a new index to your application with the same number of records as the primary.

For more about backend implementation please see: https://www.algolia.com/doc/guides/managing-results/refine-results/sorting/#backend-implementation-replicas.

There is also information about front-end implementation (if you want to go that route) right below. Our InstantSearch libraries have a sort-by widget that make toggling between replicas very easy.

I hope this helps!