ransack vs searchkick: which works better to build a search feature in Rails? They’re both tools we can use to build a search function, however, they’re completely different beasts and not directly comparable. Each come with their own advantages and disadvantages, so which one is right for your use case?
Lets’s dive in and build something with each to see how they compare, starting with ransack.
ransack
After installing the gem, we need to make a slight adjustment to our controller
def index @q = User.ransack(params[:q]) @users = @q.resul end
Now all that is left to do is build the search form in the view
<%= search_form_for @q do |f| %> # Search if the name field contains... <%= f.label :name_cont %> <%= f.search_field :name_cont %> # Search if an associated articles.title starts with... <%= f.label :articles_title_start %> <%= f.search_field :articles_title_start %> # Attributes may be chained. Search multiple attributes for one value... <%= f.label :name_or_description_or_email_or_articles_title_cont %> <%= f.search_field :name_or_description_or_email_or_articles_title_cont %> <%= f.submit %> <% end %>
As you can see, ransack is pretty much plug and play. It makes it incredibly easy to build new filters, and you can get quite creative with the input names to build any search or filter you would like. The input name is in the form attribute_name[_or_attribute_name]..._predicate
, so any number of attributes can be chained, and supports attributes through associations. The list of available predicates is also expansive.
Things get a little more complex if we want to search across all attributes. Although ransack does offer aliases, so we can chain many attributes without dealing with a ridiculously long URL parameter.
You could create that alias programmatically too, but you’d have to filter out some columns that cannot be searched:
class User ransack_alias :any_column, User.column_names.excluding('id', 'created_at', 'updated_at').join('_or_') end
Another advantage of ransack is it provides a sort_link
so we can quickly turn our table headers into sortable links. Again, it works with associations too.
<%= sort_link(@q, :name) %>
And for the times when ransack’s conventions don’t quite cut it, ransackers are a way to implement your own custom search functions using Arel.
So, ransack is a straightforward way to add sorting, filtering, and search to your Rails apps. If you only need filtering and sorting or only care for a more basic search, it is the way to go. But searching with ransack is achieved with SQL like
and is therefore pretty basic. As soon as you need any intelligent search functionality, such as miss-spellings, synonyms, and ordering results by closest match, That is where searchkick comes into play.
searchkick
searchkick is built on elasticsearch and will require a bit of setup before you can begin, but if you are using something like Heroku to manage your app, adding an elasticsearch server to your tech stack is pretty much painless. For local development, you can install elasticsearch with brew and be good to go.
Unlike ransack, searchkick is not a full-stack solution. So it will give us the ability to search and filter on the backend, but exactly how we wire this up to our search form on the front end is up to us. I won’t go into that in this article, but we have a great post from 2018 covering just that if you want to check it out.
That doesn’t cover how to sort our data, however. If we wanted to do something similar to ransack’s sort_link
we’d have to implement that ourselves, which can be tricky. However, if you were using searchkick, you’d likely be more interested in ordering results by score to see the most relevant result first. This is perfect for a general search and less suitable for a searchable table with sortable table headings.
Once we have everything set up, the power of searchkick becomes clear. We search for a single string and, it searches all the data that we have indexed. It handles all the things where ransack falls short, most notably fuzzy searching and scoring.
searchkick vs ransack – which is better?
In conclusion, ransack is a powerful full-stack solution that allows us to add search, filters, and sorting to our Rails up in minutes. It is a massive productivity winner f you don’t require the capabilities of a fully-fledged intelligent search. It is perfect for an admin interface but falls short for much else. When building a search on the public-facing part of your app, for example, a product search in an e-commerce store, searchkick is the obvious choice.