Search code examples
ruby-on-railsransack

Ransack search: one input field for two fields with relevance


I have this piece of code

<%= search_form_for @search, url: "/venues/search", method: "post", id: "frm_search_venue", class: "col-md-6" do |f| %>
                <%= f.text_field :name_or_description_cont, class: "form-control" %>
<% end %>

in a RoR app with Ransack. It works perfect, it searches in the "name" and "description" field from my model (Venue).

The thing is: can I define with Ransack some kind of relevance in the search? In other words, the result search array come with the models that the "name" attribute matches the parameter, later the description?

Or is it better to use other search pattern/approach, such as with Elastic search?


Solution

  • You are on the right track - I would use Elastic search (or something similar) to add relevancy to this search query.

    The approach could involve giving a weight to the name and description (name would be 0.9 or 1.0 whereas description would be 0.6 or 0.5)

    You might also want to take a look at Solr (http://lucene.apache.org/solr/).

    There's a version of Solr for Rails applications call Sunspot. It should provide the functionality that you're looking for.

    http://www.github.com/sunspot/sunspot