top of page

Data Searching in Ruby on Rails using SearchKick Gem

  • Writer: Mischelle L
    Mischelle L
  • May 3, 2018
  • 1 min read

What is SearchKick Gem?

Searchkick Gem is a Ruby on Rails gem that runs on top of Elasticsearch and makes it easy searches in a Rails-friendly fashion. Searchkick supports the complete Elasticsearch Search API, as by using it our search becomes more advanced. In addition, it allows you to add more features including analytics, auto suggestions, and personalized results.

The steps are required:

  1. Add the gem “gem ‘searchkick’“.

  2. Then run the bundle install command.

Then we will generate the complete model by using Scaffold generator

rails g scaffold Article name:string description:text

After running the above command it will generate all the necessary files then we will run the database migration command.

rake db:migrate

The controller code will be modified like that

Def index
	Query=params[:q].presence || “*”
	@artivles=Article.search(
		query,
		page: params[:page}, per_page: 25
	)
end

The model code will be modified like that

searchkick
def search_data
	{
title: title
}
end

The view code for display the articles will be modified like these.



Before starting the application we need to create the indexes

rake searchkick:reindex CLASS=Article

The ouput of the application look like these.


 
 
 

Recent Posts

See All

Comentários


© 2023 by Walkaway. Proudly created with Wix.com

  • Facebook Black Round
  • Google+ - Black Circle
  • Twitter Black Round
bottom of page